Thread safety- span across multiple jvm

Dear Experts

we have two jvms running invoking the same codebase.
currently the issue is we have a synchronized block which makes sure for each message ID, NAME and ACCOUNT a unique Uuid is generated
I have that logic within the synchronized block to make sure that no two threads are generating two different Uuid for the same message ID, NAME and ACCOUNT.

How do i handle this scenario running in multiple JVMs since a synchronized block works fine only with a single JVM.

thanks.
royjaydAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

dpearsonCommented:
You're right that you can't use any type of Thread synchronization to do this.

You'll need to synchronize in some other way.  Lots of options here, all will take some work:
 - Most common is to use a database.
   E.g. Insert a new row in a table with a unique key on (id,name,account) so 2 inserts at the same time will collide and only one succeeds.  Each JVM generates a UUID and inserts it into the table.  Then read back the UUID from the database - all JVMs will see the same UUID on the read back even if a dozen tried to insert at the same time.
 - Use a distributed cache like memcache to do the same sort of thing
 - Use a file in the file system and lock/release that file using file I/O ops to sync
 - Use a messaging protocol to send all generation requests to one JVM

Basically they all use some shared resource that's external to the JVM to resolve the synchronization.

Doug

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
royjaydAuthor Commented:
thanks
Not sure I understood this completely

 <<<E.g. Insert a new row in a table with a unique key on (id,name,account) so 2 inserts at the same time will collide and only one succeeds.  Each JVM generates a UUID and inserts it into the table.  Then read back the UUID from the database - all JVMs will see the same UUID on the read back even if a dozen tried to insert at the same time.>>>>
 
Lets say I have this code design

  String uid = fetchUIdFromUIDTable(id, name, account);    
  if (uid is null ) {
               generate new Uid ;
            insert new Uid into UIDTable;                
            }
                  
 Thread1 and Thread2 from different JVM invokes fetchUIdFromUIDTable(id, name, account);   at the same time with  id = 1, name = PRODUCT, account= NAM
 
 Lets say there is no uid retrieved from table so , String uid will be null
 
 now both the threads will go inside the if condition and generate new Uid (random number) which are different from each other and insert into UIDtable.
 
 My goal is for a given Id, name and account, there must be only one Uid.

thanks.
dpearsonCommented:
You need one more step in your code:

  String uid = fetchUIdFromUIDTable(id, name, account);    
  if (uid is null ) {
               generate new Uid ;
            insert new Uid into UIDTable;   // Technically "INSERT ... IGNORE" so errors don't throw  
           // Read back the UID from the table (may not be the value you generated)  
            uid = fetchUIdFromUIDTable(id, name, account);    
           }

Now when 2 threads see "null" for the fetch, they both generate a UID (different values) and try to insert into the table.

The database is set with a UNIQUE key on (id,name,account) so whichever insert is received first is accepted, the second will error out (you just ignore that error, it's OK).

Now both threads read the value back from the database.

Whichever insert arrived first is returned to both threads and now each has the same, unique value.

Make sense?

Doug
royjaydAuthor Commented:
got it
<< // Read back the UID from the table (may not be the value you generated)  
             uid = fetchUIdFromUIDTable(id, name, account);    
>>

but I am not really doing anything with that uid. do we really need it there?
dpearsonCommented:
but I am not really doing anything with that uid. do we really need it there?

What was your existing single JVM code doing with the uid?  Presumably whatever you were doing with that unique UID before, you would want to keep doing?  (Record it somewhere, send it somewhere, use it for some purpose)?

Unless your whole goal was to store it in the database...in which case I guess you don't need it any more once that's done :)

Doug
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.