setAutoCommit(false) in constructor

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
suman_mAsked:
Who is Participating?
 
moduloCommented:
Finalized as proposed

modulo

Community Support Moderator
Experts Exchange
0
 
jose_tijoCommented:
Hi
  Can u post your piece of code.
Tijo
0
 
suman_mAuthor Commented:
0
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.

 
suman_mAuthor Commented:
0
 
objectsCommented:
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
 
suman_mAuthor Commented:
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
 
suman_mAuthor Commented:
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
 
suman_mAuthor Commented:
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
 
heyhey_Commented:
so you are telling us that
setAutoCommit(false)
works fine when called inside main method but does not work when called in constructor ?
0
 
suman_mAuthor Commented:
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
 
bakyarajCommented:
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
 
suman_mAuthor Commented:
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
 
bakyarajCommented:
what are u trying to say? u have not yet made the changes suggested by me.
0
 
MoondancerCommented:
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
 
Venci75Commented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.