• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Multi-threading issues while handling JDBC transactions

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have 5 threads running, each thread tries to insert some records into the database. Each thread is unaware of what the other thread is processing. In this scenario when one of the thread out of 5 is facing some problem while inserting into database, it tries to rollback its own transaction. Is it possible to rollback the transactions of the other threads and also can we stop the execution of other threads??
Code snippets:
public class ThreadSample {
//Our own implementation
private TransactionContext getTransactionContext(Context context) {
return TransactionContext(context.getTransactionContextId());
}
class MyThread extends Thread {
public void run() {
try {
TransactionContext txt = getTransactionContext(context)
txt.begin();
Connection con = txt.getConnection();
<<insert records into table>>
txt.commit();
txt = null;
} catch (Exception ex) {
} finally {
if (txt != null) {
txt.rollback();
}
}
}
}
public static void main(String[] args) {
MyThread t1 = new MyThread();
.
.
MyThread t5 = new MyThread();
t1.start();
.
.
t5.start();
}
}
 
Ranch Hand
Posts: 193
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The five transactions are independent from
each other. Rollback on one does not affect
others.If DB implements SERIALIZABLE, there
should be not a problem in your case.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic