aspose file tools*
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
Author

Multiple Instance of App server- threading issue

deenbandhu prasad
Greenhorn

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
Marshal

Joined: Jan 10, 2002
Posts: 61006
    
  65

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.

thanks,
bear
JavaRanch sheriff


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Kalyani Sailendra
Greenhorn

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
Rancher

Joined: Mar 22, 2000
Posts: 12769
    
    5
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.

Bill
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: http://aspose.com/file-tools
 
subject: Multiple Instance of App server- threading issue