• 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
  • Ron McLeod
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Rob Spoor
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Carey Brown
Bartenders:

Tranactions handling in multi threaded environment.

 
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I need to do 3 operations (insert into schema A,insert into schema B & insert into schema C)
in DB
Requirement is use to multithread.

Thread A --> Insert into Schema A
Thread B --> Insert into Schema B
Thread C --> Insert into Schema C

Each thread needs to run independently.

But any one of the operation failed (either A or B or C), need to roll back the entire transaction.

Is it possible in multi thread environment (using spring - declrative transactions)?
 
Rancher
Posts: 2759
32
Eclipse IDE Spring Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No.. database transactions cannot cross thread boundaries
 
Raghu Sha
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks..
Please suggest best design approach for the above scenario.

All the transactions should run parallel.
 
Bartender
Posts: 1682
7
Android Mac OS X IntelliJ IDE Spring Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It really does not sound like your scenario lends itself well to parallel processing. If you are going to make this work you are going to have to join those 3 threads at some point and do the inserts in a single transaction.
 
Raghu Sha
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Bill..
The below are the code snippets used for multithreaded opeartion.

Each method need to put @Transactional annotation?
Could you please guide to where to join the threads?
Explicitly check exception in catch block or @Transactional handles rollback?

@Transactional(propagation = Propagation.REQUIRES_NEW, rollbackFor = ConCurrentException.class)
public static void DataThread() throws InterruptedException{

final long threadinterval= 500L;

final Thread updateThread1 = new Thread(new Runnable() {
public void run() {
System.out.println("Inside updateThread1..");

try {
updateDB1();
Thread.sleep(threadinterval);
} catch (Exception e) {
System.out.println(e);

}

}
});
updateThread1.start();


Thread updateThread2 = new Thread(new Runnable() {
public void run() {
System.out.println("Inside UpdateThread2..");

try {
updateDB2();

} catch (Exception e) {
System.out.println("Exception ");

}

}
});
updateThread2.start();

Thread updateThread3 = new Thread(new Runnable() {
public void run() {
System.out.println("Inside UpdateThread3..");

try {
updateDB3();

} catch (Exception e) {
System.out.println("Exception ");

}

}
});
updateThread3.start();

}


@Transactional(propagation = Propagation.REQUIRES_NEW, rollbackFor = ConCurrentException.class)
public static void updateDB1(){
// save operation in schema 1
System.out.println("Inside updateDB1");
}

@Transactional(propagation = Propagation.REQUIRES_NEW, rollbackFor = ConCurrentException.class)
public static void updateDB2(){
// save operation in schema 2
System.out.println("Inside updateDB2");
}

@Transactional(propagation = Propagation.REQUIRES_NEW, rollbackFor = ConCurrentException.class)
public static void updateDB3(){
// save operation in schema 3
System.out.println("Inside updateDB3");
}

Thanks.
reply
    Bookmark Topic Watch Topic
  • New Topic