Dear All,
I have a snippet of code as below. Here I build one db connection and is shared across. I can put the dbconn.commit in place and no problem. The problem when I put the dbconn.rollback I get this sort of error "commServer.java:1798: unreported exception java.sql.SQLException; must be caught or declared to be thrown dbconn.rollback();". So where best to put the rollback?

public void run() 
{
 createConnection();
 while (true) 
 {
   try 
   {
     //all my sql statements.
     dbconn.commit();
   }
   catch (Exception e) 
   {
     System.out.println("\nSQL Error here :");
     //dbconn.rollback();
     e.printStackTrace();
   }
 }
 closeConnection();
}
void createConnection()
{
     	System.out.println("Crerate Connection");
     	connCreated = new Date();
     	try 
       	{
     	dbconn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test1?"+"user=8888888&password=8888888");
       	dbconn.setAutoCommit(false);

       	}
       	catch(Throwable ex)
         {
             System.out.println("MyError:SQLException has been caught for dbconn close");
             ex.printStackTrace(System.out);
         }
        	    
}
void closeConnection()
{
     	 try 
       	 {
	        if ( dbconn != null ) 
	        {
	          dbconn.close();
	        }
	        else 
	        {
	          //logger.log(Level.SEVERE, "MyError:dbconn is null in finally close", "");
	          System.out.println("MyError:dbconn is null in finally close");
	        }
         }
         catch(SQLException ex)
         {
             System.out.println("MyError:SQLException has been caught for dbconn close");
             ex.printStackTrace(System.out);
         }
}

The rollback method throws an SQLException. It needs to be in a try-catch. You will put it where your logic dictates.

Dear JavaAddict,
I have put here but I get error so how to solve this error "commServer.java:1798: unreported exception java.sql.SQLException; must be caught or declared to be thrown dbconn.rollback();".

try    
{     //all my sql statements.     
     dbconn.commit();   
}   catch (Exception e)    
{     
System.out.println("\nSQL Error here :");     
dbconn.rollback();     
e.printStackTrace();   
}

I dont know that dbconn.commit() returns but if it returns something that can be interpreted as a success or failure, lets say true/false for the sake of argument. Then you could do something like

if (!dbconn.commit()){
  dbconn.rollback(); <- will still be inside try/catch
}

if not you need to wrap the method call in a new try/catch like

try    
{     
  //all my sql statements.     
  dbconn.commit();   
} catch (Exception e) {     
  System.out.println("\nSQL Error here :");     
  try {
    dbconn.rollback();
  } catch (Exception e) {
    System.out.println("\nSQL Error here :");
  }
}

Dear Slimmy,
I have tried this method and the compiler did not complain so may it should work.

try    {       
//all my sql statements.       
dbconn.commit();   
} 
catch (Exception e) 
{       
System.out.println("\nSQL Error here :");       
try {    
dbconn.rollback();  
} 
catch (Exception e) 
{    
System.out.println("\nSQL Error here :");  
}
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.