We are developing a core java multithreaed application which will run across multiple machines(JVM's). Now there is a piece of code which will work like -
1. check if entry is resent in record_master table.
2. If no then make entry in record_master and record_child.
3. If yes then fetch primary key of record_master and use it as foreign key in record_child and make entry in later table only.
this whole code is synchronized() and a class level lock is placed on it to take care of multiple threads within JVM, which works fine. But incase of multiple JVM it fails some time and make 2 entries in record_master. Sceneries goes like this -
Machine 1 running thread t1m1 and t2m1
Machine 2 running thread t1m2 and t2m2.
If t1m1 and t1m2 will reach on point 1 at same time, they will make 2 entries in database while it should be only 1. We are using Jdk 1.5 executor framework to create and manage threads. Simple JDBC with a XA data source running on weblogic 9.1 and oracle 10g. we are using core java only and it's too late in the game to introduce something else. we can't take database table locks as this will hit performance.
Can someone please let me know how can we control dirty reads here?