Solved

setAutoCommit(false) in constructor

Posted on 2002-04-11
15
279 Views
Last Modified: 2010-03-31
I have a java application,which does some database transactions.The connection object is initialized in the constructor.I have a transaction set in which i am setting the AutoCommit(false) in the constructor.The problem is in my main method even if i dont explicitly commit(),the transaction is getting commited.But if i set the AutoCommit(false) in the main other than the constructor then it is working fine.Can anyone help me with the reason behind the same.
The bottomline ---If i set autocommit(false) in constructor ,it does not take efect.
But does if the autocommit is set in main().
I am using jdk1.2.2,oracle8.1.6,thin driver
0
Comment
Question by:suman_m
15 Comments
 
LVL 1

Expert Comment

by:jose_tijo
ID: 6936048
Hi
  Can u post your piece of code.
Tijo
0
 

Author Comment

by:suman_m
ID: 6936066
0
 

Author Comment

by:suman_m
ID: 6936072
0
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 92

Expert Comment

by:objects
ID: 6936077
Sounds like the connection is being made in main() before you setAutoCommit(false), or your turning off auto commit in a different connection than the one used in main.
0
 

Author Comment

by:suman_m
ID: 6936090
Heres the code


import java.io.*;
import java.net.*;
import java.util.*;
import java.sql.*;
public class archiveTest {
     static Connection newCon = null;
     static int commitcheck=0;
     archiveTest()
     {
          try
          {
 Class.forName("oracle.jdbc.driver.OracleDriver");
                newCon  = DriverManager.getConnection("jdbc:oracle:thin:@xxxx:1521:xxx","xxx","xxx");
//newCon.setAutoCommit(false);
     
          }
          catch(Exception E)
          {
             System.out.println(E);
               return;
             }
     }
     public static void main(String args[])
     {
          archiveTest newObj=new archiveTest();
               int flag=0;
    try{
          newCon.setAutoCommit(false);
     try
     {
     newObj.archiveData();
     newObj.deleteData();
     flag=0;
}
catch(Exception main)
{
System.out.println("ErrorMain"+main);
newCon.rollback();
     commitcheck++;
}
if(commitcheck==0)
          {
          //newCon.commit();
          System.out.println("Transaction Commited");
          }
          else
          {
System.out.println("Transaction RollBacked");
          }
     }
     catch(Exception ee)
     {
          System.out.println("Erroree"+ee);
     try{    
          newCon.rollback();
          commitcheck++;
     }
     catch(Exception aa)
     {
     System.out.println("Erroraa"+aa);
     }
}
void archiveData()
          {
     try{
     PreparedStatement ps1=null,ps3=null;
     ResultSet rr1=null,rr3=null;
          try
          {
     ps1  = newCon.prepareStatement("");  
     rr1 = ps1.executeQuery();
     ps1.close();
     rr1.close();
     }
     catch(Exception e1)
          {
System.out.println("Error1"+e1);
     newCon.rollback();
commitcheck++;
          }
          try
     {
     ps3  = newCon.prepareStatement("");  
     rr3 = ps3.executeQuery();
          ps3.close();
     rr3.close();
          }
     catch(Exception e2)
          {
     System.out.println("Error2"+e2);
     newCon.rollback();
     commitcheck++;
     }
     }
     catch(Exception e13)
               {
System.out.println("Errore13"+e13);
          try
          {
     newCon.rollback();
     commitcheck++;
          }
     catch(Exception e133)
          {
System.out.println("Errore133"+e133);
          }
     }
}
void deleteData()
          {
          try{
     PreparedStatement pss12=null,pss10=null,pss13=null;
     ResultSet rrr12=null,rrr10=null,rrr13=null;
          try
     {
     pss12  = newCon.prepareStatement("");
     rrr12 = pss12.executeQuery();
     pss12.close();
     rrr12.close();
     }
catch(Exception e24)
          {
System.out.println("Errore24"+e24);
     newCon.rollback();
     commitcheck++;
          }          
     try
{
     pss10  = newCon.prepareStatement("");
     rrr10 = pss10.executeQuery();
     pss10.close();
     rrr10.close();
}
     catch(Exception e23)
     {
System.out.println("Errore23"+e23);
newCon.rollback();
     commitcheck++;
          }
try
{
pss13  = newCon.prepareStatement("");  
rrr13 = pss13.executeQuery();
pss13.close();
rrr13.close();
     }
     catch(Exception e222)
     {
System.out.println("Error222"+e222);
newCon.rollback();
commitcheck++;
     }
}
catch(Exception e26)
{
System.out.println("Errore26"+e26);
     try
          {
     newCon.rollback();
     commitcheck++;
          }
     catch(Exception e134)
     {
     System.out.println("Errore134"+e134);
     }

     }

     }

}          
0
 

Author Comment

by:suman_m
ID: 6936106
Heres the code


import java.io.*;
import java.net.*;
import java.util.*;
import java.sql.*;
public class archiveTest {
     static Connection newCon = null;
     static int commitcheck=0;
     archiveTest()
     {
          try
          {
 Class.forName("oracle.jdbc.driver.OracleDriver");
                newCon  = DriverManager.getConnection("jdbc:oracle:thin:@xxxx:1521:xxx","xxx","xxx");
//newCon.setAutoCommit(false);
     
          }
          catch(Exception E)
          {
             System.out.println(E);
               return;
             }
     }
     public static void main(String args[])
     {
          archiveTest newObj=new archiveTest();
               int flag=0;
    try{
          newCon.setAutoCommit(false);
     try
     {
     newObj.archiveData();
     newObj.deleteData();
     flag=0;
}
catch(Exception main)
{
System.out.println("ErrorMain"+main);
newCon.rollback();
     commitcheck++;
}
if(commitcheck==0)
          {
          //newCon.commit();
          System.out.println("Transaction Commited");
          }
          else
          {
System.out.println("Transaction RollBacked");
          }
     }
     catch(Exception ee)
     {
          System.out.println("Erroree"+ee);
     try{    
          newCon.rollback();
          commitcheck++;
     }
     catch(Exception aa)
     {
     System.out.println("Erroraa"+aa);
     }
}
void archiveData()
          {
     try{
     PreparedStatement ps1=null,ps3=null;
     ResultSet rr1=null,rr3=null;
          try
          {
     ps1  = newCon.prepareStatement("");  
     rr1 = ps1.executeQuery();
     ps1.close();
     rr1.close();
     }
     catch(Exception e1)
          {
System.out.println("Error1"+e1);
     newCon.rollback();
commitcheck++;
          }
          try
     {
     ps3  = newCon.prepareStatement("");  
     rr3 = ps3.executeQuery();
          ps3.close();
     rr3.close();
          }
     catch(Exception e2)
          {
     System.out.println("Error2"+e2);
     newCon.rollback();
     commitcheck++;
     }
     }
     catch(Exception e13)
               {
System.out.println("Errore13"+e13);
          try
          {
     newCon.rollback();
     commitcheck++;
          }
     catch(Exception e133)
          {
System.out.println("Errore133"+e133);
          }
     }
}
void deleteData()
          {
          try{
     PreparedStatement pss12=null,pss10=null,pss13=null;
     ResultSet rrr12=null,rrr10=null,rrr13=null;
          try
     {
     pss12  = newCon.prepareStatement("");
     rrr12 = pss12.executeQuery();
     pss12.close();
     rrr12.close();
     }
catch(Exception e24)
          {
System.out.println("Errore24"+e24);
     newCon.rollback();
     commitcheck++;
          }          
     try
{
     pss10  = newCon.prepareStatement("");
     rrr10 = pss10.executeQuery();
     pss10.close();
     rrr10.close();
}
     catch(Exception e23)
     {
System.out.println("Errore23"+e23);
newCon.rollback();
     commitcheck++;
          }
try
{
pss13  = newCon.prepareStatement("");  
rrr13 = pss13.executeQuery();
pss13.close();
rrr13.close();
     }
     catch(Exception e222)
     {
System.out.println("Error222"+e222);
newCon.rollback();
commitcheck++;
     }
}
catch(Exception e26)
{
System.out.println("Errore26"+e26);
     try
          {
     newCon.rollback();
     commitcheck++;
          }
     catch(Exception e134)
     {
     System.out.println("Errore134"+e134);
     }

     }

     }

}          
0
 

Author Comment

by:suman_m
ID: 6936119
Heres the code


import java.io.*;
import java.net.*;
import java.util.*;
import java.sql.*;
public class archiveTest {
     static Connection newCon = null;
     static int commitcheck=0;
     archiveTest()
     {
          try
          {
 Class.forName("oracle.jdbc.driver.OracleDriver");
                newCon  = DriverManager.getConnection("jdbc:oracle:thin:@xxxx:1521:xxx","xxx","xxx");
//newCon.setAutoCommit(false);
     
          }
          catch(Exception E)
          {
             System.out.println(E);
               return;
             }
     }
     public static void main(String args[])
     {
          archiveTest newObj=new archiveTest();
               int flag=0;
    try{
          newCon.setAutoCommit(false);
     try
     {
     newObj.archiveData();
     newObj.deleteData();
     flag=0;
}
catch(Exception main)
{
System.out.println("ErrorMain"+main);
newCon.rollback();
     commitcheck++;
}
if(commitcheck==0)
          {
          //newCon.commit();
          System.out.println("Transaction Commited");
          }
          else
          {
System.out.println("Transaction RollBacked");
          }
     }
     catch(Exception ee)
     {
          System.out.println("Erroree"+ee);
     try{    
          newCon.rollback();
          commitcheck++;
     }
     catch(Exception aa)
     {
     System.out.println("Erroraa"+aa);
     }
}
void archiveData()
          {
     try{
     PreparedStatement ps1=null,ps3=null;
     ResultSet rr1=null,rr3=null;
          try
          {
     ps1  = newCon.prepareStatement("");  
     rr1 = ps1.executeQuery();
     ps1.close();
     rr1.close();
     }
     catch(Exception e1)
          {
System.out.println("Error1"+e1);
     newCon.rollback();
commitcheck++;
          }
          try
     {
     ps3  = newCon.prepareStatement("");  
     rr3 = ps3.executeQuery();
          ps3.close();
     rr3.close();
          }
     catch(Exception e2)
          {
     System.out.println("Error2"+e2);
     newCon.rollback();
     commitcheck++;
     }
     }
     catch(Exception e13)
               {
System.out.println("Errore13"+e13);
          try
          {
     newCon.rollback();
     commitcheck++;
          }
     catch(Exception e133)
          {
System.out.println("Errore133"+e133);
          }
     }
}
void deleteData()
          {
          try{
     PreparedStatement pss12=null,pss10=null,pss13=null;
     ResultSet rrr12=null,rrr10=null,rrr13=null;
          try
     {
     pss12  = newCon.prepareStatement("");
     rrr12 = pss12.executeQuery();
     pss12.close();
     rrr12.close();
     }
catch(Exception e24)
          {
System.out.println("Errore24"+e24);
     newCon.rollback();
     commitcheck++;
          }          
     try
{
     pss10  = newCon.prepareStatement("");
     rrr10 = pss10.executeQuery();
     pss10.close();
     rrr10.close();
}
     catch(Exception e23)
     {
System.out.println("Errore23"+e23);
newCon.rollback();
     commitcheck++;
          }
try
{
pss13  = newCon.prepareStatement("");  
rrr13 = pss13.executeQuery();
pss13.close();
rrr13.close();
     }
     catch(Exception e222)
     {
System.out.println("Error222"+e222);
newCon.rollback();
commitcheck++;
     }
}
catch(Exception e26)
{
System.out.println("Errore26"+e26);
     try
          {
     newCon.rollback();
     commitcheck++;
          }
     catch(Exception e134)
     {
     System.out.println("Errore134"+e134);
     }

     }

     }

}          
0
 
LVL 16

Expert Comment

by:heyhey_
ID: 6936221
so you are telling us that
setAutoCommit(false)
works fine when called inside main method but does not work when called in constructor ?
0
 

Author Comment

by:suman_m
ID: 6936301
Heres the code


import java.io.*;
import java.net.*;
import java.util.*;
import java.sql.*;
public class archiveTest {
     static Connection newCon = null;
     static int commitcheck=0;
     archiveTest()
     {
          try
          {
 Class.forName("oracle.jdbc.driver.OracleDriver");
                newCon  = DriverManager.getConnection("jdbc:oracle:thin:@xxxx:1521:xxx","xxx","xxx");
//newCon.setAutoCommit(false);
     
          }
          catch(Exception E)
          {
             System.out.println(E);
               return;
             }
     }
     public static void main(String args[])
     {
          archiveTest newObj=new archiveTest();
               int flag=0;
    try{
          newCon.setAutoCommit(false);
     try
     {
     newObj.archiveData();
     newObj.deleteData();
     flag=0;
}
catch(Exception main)
{
System.out.println("ErrorMain"+main);
newCon.rollback();
     commitcheck++;
}
if(commitcheck==0)
          {
          //newCon.commit();
          System.out.println("Transaction Commited");
          }
          else
          {
System.out.println("Transaction RollBacked");
          }
     }
     catch(Exception ee)
     {
          System.out.println("Erroree"+ee);
     try{    
          newCon.rollback();
          commitcheck++;
     }
     catch(Exception aa)
     {
     System.out.println("Erroraa"+aa);
     }
}
void archiveData()
          {
     try{
     PreparedStatement ps1=null,ps3=null;
     ResultSet rr1=null,rr3=null;
          try
          {
     ps1  = newCon.prepareStatement("");  
     rr1 = ps1.executeQuery();
     ps1.close();
     rr1.close();
     }
     catch(Exception e1)
          {
System.out.println("Error1"+e1);
     newCon.rollback();
commitcheck++;
          }
          try
     {
     ps3  = newCon.prepareStatement("");  
     rr3 = ps3.executeQuery();
          ps3.close();
     rr3.close();
          }
     catch(Exception e2)
          {
     System.out.println("Error2"+e2);
     newCon.rollback();
     commitcheck++;
     }
     }
     catch(Exception e13)
               {
System.out.println("Errore13"+e13);
          try
          {
     newCon.rollback();
     commitcheck++;
          }
     catch(Exception e133)
          {
System.out.println("Errore133"+e133);
          }
     }
}
void deleteData()
          {
          try{
     PreparedStatement pss12=null,pss10=null,pss13=null;
     ResultSet rrr12=null,rrr10=null,rrr13=null;
          try
     {
     pss12  = newCon.prepareStatement("");
     rrr12 = pss12.executeQuery();
     pss12.close();
     rrr12.close();
     }
catch(Exception e24)
          {
System.out.println("Errore24"+e24);
     newCon.rollback();
     commitcheck++;
          }          
     try
{
     pss10  = newCon.prepareStatement("");
     rrr10 = pss10.executeQuery();
     pss10.close();
     rrr10.close();
}
     catch(Exception e23)
     {
System.out.println("Errore23"+e23);
newCon.rollback();
     commitcheck++;
          }
try
{
pss13  = newCon.prepareStatement("");  
rrr13 = pss13.executeQuery();
pss13.close();
rrr13.close();
     }
     catch(Exception e222)
     {
System.out.println("Error222"+e222);
newCon.rollback();
commitcheck++;
     }
}
catch(Exception e26)
{
System.out.println("Errore26"+e26);
     try
          {
     newCon.rollback();
     commitcheck++;
          }
     catch(Exception e134)
     {
     System.out.println("Errore134"+e134);
     }

     }

     }

}          
0
 

Expert Comment

by:bakyaraj
ID: 6936520
instead of declaring the connection object as static declare it as public or private and give the setAutoCommit(false) statement in the constructor. Try this and see.
0
 

Author Comment

by:suman_m
ID: 6936567
Heres the code


import java.io.*;
import java.net.*;
import java.util.*;
import java.sql.*;
public class archiveTest {
     static Connection newCon = null;
     static int commitcheck=0;
     archiveTest()
     {
          try
          {
 Class.forName("oracle.jdbc.driver.OracleDriver");
                newCon  = DriverManager.getConnection("jdbc:oracle:thin:@xxxx:1521:xxx","xxx","xxx");
//newCon.setAutoCommit(false);
     
          }
          catch(Exception E)
          {
             System.out.println(E);
               return;
             }
     }
     public static void main(String args[])
     {
          archiveTest newObj=new archiveTest();
               int flag=0;
    try{
          newCon.setAutoCommit(false);
     try
     {
     newObj.archiveData();
     newObj.deleteData();
     flag=0;
}
catch(Exception main)
{
System.out.println("ErrorMain"+main);
newCon.rollback();
     commitcheck++;
}
if(commitcheck==0)
          {
          //newCon.commit();
          System.out.println("Transaction Commited");
          }
          else
          {
System.out.println("Transaction RollBacked");
          }
     }
     catch(Exception ee)
     {
          System.out.println("Erroree"+ee);
     try{    
          newCon.rollback();
          commitcheck++;
     }
     catch(Exception aa)
     {
     System.out.println("Erroraa"+aa);
     }
}
void archiveData()
          {
     try{
     PreparedStatement ps1=null,ps3=null;
     ResultSet rr1=null,rr3=null;
          try
          {
     ps1  = newCon.prepareStatement("");  
     rr1 = ps1.executeQuery();
     ps1.close();
     rr1.close();
     }
     catch(Exception e1)
          {
System.out.println("Error1"+e1);
     newCon.rollback();
commitcheck++;
          }
          try
     {
     ps3  = newCon.prepareStatement("");  
     rr3 = ps3.executeQuery();
          ps3.close();
     rr3.close();
          }
     catch(Exception e2)
          {
     System.out.println("Error2"+e2);
     newCon.rollback();
     commitcheck++;
     }
     }
     catch(Exception e13)
               {
System.out.println("Errore13"+e13);
          try
          {
     newCon.rollback();
     commitcheck++;
          }
     catch(Exception e133)
          {
System.out.println("Errore133"+e133);
          }
     }
}
void deleteData()
          {
          try{
     PreparedStatement pss12=null,pss10=null,pss13=null;
     ResultSet rrr12=null,rrr10=null,rrr13=null;
          try
     {
     pss12  = newCon.prepareStatement("");
     rrr12 = pss12.executeQuery();
     pss12.close();
     rrr12.close();
     }
catch(Exception e24)
          {
System.out.println("Errore24"+e24);
     newCon.rollback();
     commitcheck++;
          }          
     try
{
     pss10  = newCon.prepareStatement("");
     rrr10 = pss10.executeQuery();
     pss10.close();
     rrr10.close();
}
     catch(Exception e23)
     {
System.out.println("Errore23"+e23);
newCon.rollback();
     commitcheck++;
          }
try
{
pss13  = newCon.prepareStatement("");  
rrr13 = pss13.executeQuery();
pss13.close();
rrr13.close();
     }
     catch(Exception e222)
     {
System.out.println("Error222"+e222);
newCon.rollback();
commitcheck++;
     }
}
catch(Exception e26)
{
System.out.println("Errore26"+e26);
     try
          {
     newCon.rollback();
     commitcheck++;
          }
     catch(Exception e134)
     {
     System.out.println("Errore134"+e134);
     }

     }

     }

}          
0
 

Expert Comment

by:bakyaraj
ID: 6938352
what are u trying to say? u have not yet made the changes suggested by me.
0
 
LVL 1

Expert Comment

by:Moondancer
ID: 6956529
This question is current, some others are not and need some attention by you.

ADMINISTRATION WILL BE CONTACTING YOU SHORTLY.  Moderators Computer101 or Netminder will return to finalize these if still open in seven days.  Please post closing recommendations before that time.

Question(s) below appears to have been abandoned. Your options are:
 
1. Accept a Comment As Answer (use the button next to the Expert's name).
2. Close the question if the information was not useful to you, but may help others. You must tell the participants why you wish to do this, and allow for Expert response.  This choice will include a refund to you, and will move this question to our PAQ (Previously Asked Question) database.  If you found information outside this question thread, please add it.
3. Ask Community Support to help split points between participating experts, or just comment here with details and we'll respond with the process.
4. Delete the question (if it has no potential value for others).
   --> Post comments for expert of your intention to delete and why
   --> YOU CANNOT DELETE A QUESTION with comments; special handling by a Moderator is required.

For special handling needs, please post a zero point question in the link below and include the URL (question QID/link) that it regards with details.
http://www.experts-exchange.com/jsp/qList.jsp?ta=commspt
 
Please click this link for Help Desk, Guidelines/Member Agreement and the Question/Answer process.  http://www.experts-exchange.com/jsp/cmtyHelpDesk.jsp

Click you Member Profile to view your question history and please keep them updated. If you are a KnowledgePro user, use the Power Search option to find them.  

Questions which are LOCKED with a Proposed Answer but do not help you, should be rejected with comments added.  When you grade the question less than an A, please comment as to why.  This helps all involved, as well as others who may access this item in the future.  PLEASE DO NOT AWARD POINTS TO ME.

To view your open questions, please click the following link(s) and keep them all current with updates.
http://www.experts-exchange.com/questions/Q.20094488.html
http://www.experts-exchange.com/questions/Q.20102949.html
http://www.experts-exchange.com/questions/Q.20258023.html
http://www.experts-exchange.com/questions/Q.20287827.html




*****  E X P E R T S    P L E A S E  ******  Leave your closing recommendations if this item remains inactive another seven (7) days.  If you are interested in the cleanup effort, please click this link http://www.experts-exchange.com/jsp/qManageQuestion.jsp?ta=commspt&qid=20274643 
POINTS FOR EXPERTS awaiting comments are listed here -> http://www.experts-exchange.com/commspt/Q.20277028.html
 
Moderators will finalize this question if in @7 days Asker has not responded.  This will be moved to the PAQ (Previously Asked Questions) at zero points, deleted or awarded.
 
Thank you everyone.
 
Moondancer
Moderator @ Experts Exchange
0
 
LVL 9

Expert Comment

by:Venci75
ID: 7940641
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:
PAQ'd and pts removed
Please leave any comments here within the next seven days.
 
PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!
 
Venci75
EE Cleanup Volunteer
0
 

Accepted Solution

by:
modulo earned 0 total points
ID: 7990810
Finalized as proposed

modulo

Community Support Moderator
Experts Exchange
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Oracle SQL syntax check  without executing 6 76
iterator/ListIterator approach 17 41
Java string replace 11 55
Glassfish admin console not working 1 36
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

821 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question