Solved

setAutoCommit(false) in constructor

Posted on 2002-04-11
15
276 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
This video teaches viewers about errors in exception handling.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

920 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now