1

To resolve the issue mentioned here.

We are creating and using 2 same JDBC Singleton Connections(Regular, Proxy).

  • But by doing so we are facing deadlock when we try to use both connections consecutively on same table for doing multiple inserts and updates.
  • When this happens, I cannot run any queries from the DB tool (Aqua Data Studio) as well.
  • My assumption is that it waits indefinitely for other connection to release lock.

Note: We are not dealing with multi-threading here.

Issue:

//  Auto Commit false
// Singelton
Connection connection = getConnection();  //same        

// Auto Commit true
// // Singelton
Connection proxyConnection= getConnection();  //same

PreparedStatement ps = null;

try{
   connection.setAutoCommit(false);

   //Step 1
   String sql = getQuery(); 
   ps = proxyConnection.prepareStatement(sql); 
   ps.executeUpdate();        
   .
   .
   //Step 2
   // if I don't execute this step everything works fine.  
   sql = getTransctionQuery();              
   ps = connection.prepareStatement(sql); 
   ps.executeUpdate();   

   .
   .
   //Step 3
   sql = getQuery(); 
   ps = proxyConnection.prepareStatement(sql);  
   ps.executeUpdate();  // this line never completes (if Step 2 runs)

}catch(){
   connection.rollback(); //Doesn’t rollback step 1 and understandably step 2.
}
finally{
   connection.close();  //cleanup code
   proxyConnection.close();
}

Question:

  • How to resolve this issue?
  • How to make sure different connections, though they are creating using same class loader, won't lock database/table.

Thanks

Community
  • 1
  • 1
suraj_fale
  • 978
  • 2
  • 21
  • 53
  • 1
    Using two connections is IMO no good idea. The behavior is expected if step 3 modifies data locked in step 2. I think you need something like *autonomous transaction* in Oracle - see if [this](http://stackoverflow.com/questions/26866521/autonomous-transactions-in-sybase-ase-15-5) helps. – Marmite Bomber Mar 09 '16 at 22:48
  • @up I totally agree. Two connections would be rather used if would need fetch data from 2 different databases. – FilMiOs Mar 10 '16 at 00:12
  • @MarmiteBomber I agree but then how to solve issue mentioned here http://stackoverflow.com/questions/35800951/jdbc-transaction-control-in-sybase – suraj_fale Mar 10 '16 at 15:03
  • 1
    In Oracle I'd appoach it using *autonomous transactions*. If the step 2 is done with an [autonomouus transaction](https://oracle-base.com/articles/misc/autonomous-transactions), it is commited, but the step 1 is kept uncommited. So check if Sybase has an analogous concept - the link in my comment above *may* provide answer. – Marmite Bomber Mar 10 '16 at 15:31
  • since the transaction is still uncommitted by 1st statement, table is exclusively locked. 2nd statement is also update so it can not run until 1st transaction is completed. you should concate all sqls into one and than run as only 1 statement – Meet Mar 11 '16 at 15:02

1 Answers1

0

I'm no expert here but I used to have problems with Oracle DB when running a query and then forgetting to commit (or cancel). So I think that the fact that you didn't commit after step 2 locks the database for the next access.

mrLucius
  • 33
  • 1
  • 12
  • I do not want to commit after step 2. I want to commit after Step 3, to make my rollback work for regular connection. – suraj_fale Mar 09 '16 at 22:21