File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Servlets and the fly likes Multiple Instance of App server- threading issue Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Multiple Instance of App server- threading issue" Watch "Multiple Instance of App server- threading issue" New topic

Multiple Instance of App server- threading issue

deenbandhu prasad

Joined: Feb 21, 2008
Posts: 9
The application architecture - We have 2 application server(NOn clustered environment) with the same application , both the application server pointing to the single database.

Service : we have service which reads the database and check for the entity and if that entity is not present its adds it to the database and if it is present it will update the entity.

Problem : we have multiple threads running simultaneously to through the 2 application server. Both at the same time reads the Database and find that the entity is not present. Both thread adds the same entity in the Database. and we have duplicate rows.

The primary key and entity are different so the database doesn't restrict the duplicate rows.

Ques :
By making code thread safe by using synchronized, will it prevent the above problem ? I doubt that as we have 2 application server running on different JVM. How to prevent the above problem ? thanks in advance
Bear Bibeault
Author and ninkuma

Joined: Jan 10, 2002
Posts: 63858

Please use real words when posting to the forums. Abbreviations such as "Ques" in place of "Question" only serve to make your posts more difficult to read and less likely to generate useful responses.

Please read this for more information.

JavaRanch sheriff

[Asking smart questions] [About Bear] [Books by Bear]
Kalyani Sailendra

Joined: Mar 06, 2008
Posts: 2
You might want to get the lock on the database object while updating so that the other server will not update.
But, this may not be a best practice.

Synchronization would help if it were in a single server. In multiple server environment, this will not work.
William Brogden
Author and all-around good cowpoke

Joined: Mar 22, 2000
Posts: 13037
Pass the lookup/update task objects to a single application that can coordinate access and return a result for ANY number of applications that need the function. Only this application needs to know anything about the database or whatever lies behind the function.

Possible techniques include JavaSpaces, JMS, roll your own.

Roger Chung-Wee
Ranch Hand

Joined: Sep 29, 2002
Posts: 1683
The primary key and entity are different so the database doesn't restrict the duplicate rows.

Of course they are different: the PK uniquely identifies the entity. Why do you have duplicate PKs? And why can't the database handle all concurrency control and deadlock detection for each transaction?

SCJP 1.4, SCWCD 1.3, SCBCD 1.3
I agree. Here's the link:
subject: Multiple Instance of App server- threading issue
It's not a secret anymore!