Solved

setAutoCommit(false) in constructor

Posted on 2002-04-11
15
274 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
Comment Utility
Hi
  Can u post your piece of code.
Tijo
0
 

Author Comment

by:suman_m
Comment Utility
0
 

Author Comment

by:suman_m
Comment Utility
0
 
LVL 92

Expert Comment

by:objects
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 16

Expert Comment

by:heyhey_
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
what are u trying to say? u have not yet made the changes suggested by me.
0
 
LVL 1

Expert Comment

by:Moondancer
Comment Utility
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
Comment Utility
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
Comment Utility
Finalized as proposed

modulo

Community Support Moderator
Experts Exchange
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Basics of Drool Rule Engine with few samples 2 39
topping3 challenge 14 48
wordcount challenge 11 72
Unable to start eclipse ? 17 82
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

762 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

11 Experts available now in Live!

Get 1:1 Help Now