wood burning stoves 2.0*
The moose likes Threads and Synchronization and the fly likes Synchronizing database record manipulation by multiple rmi clients Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Synchronizing database record manipulation by multiple rmi clients" Watch "Synchronizing database record manipulation by multiple rmi clients" New topic
Author

Synchronizing database record manipulation by multiple rmi clients

David Weber
Greenhorn

Joined: Nov 03, 2009
Posts: 9
Hi All,

I have an RMI Server that allows clients to retrieve,edit and remove records from a database.

How do I make sure that not more than one client can edit an order at the same time?

So far the method below does all my database manipulation, but I don't think this is safe.



I am really new to Concurrency and Synchronization,

All orders are in a JTable and if you click the row a editor pops up, where a user may edit values and add products.
The JTable is updated every second by the event dispatcher if the table is currently visible to the user.

Please offer your advice as how I can safely allow multiple clients (up to 10) to view and edit records

Much Thanks,

David Weber

Using:
NetBeans 6.7
JDK 6
Chris Beckey
Ranch Hand

Joined: Jun 09, 2006
Posts: 116

"not more than one client can edit an order at the same time"
I think the answer is going to depend on what is meant by the "client" and what is meant by "at the same time". If all you are worried about is assuring that the database is in a consistent state then the answer is to set the isolation level to something appropriate and let the RDBMS deal with it. It doesn't look like you have anything longer than a single statement transaction so you shouldn't even have to start/commit transactions. This is where the definition of "at the same time" becomes important and this answer assumes it is defined at the database, not at the user's GUI.
OTOH, if you wanted to lock a row while a user (as in an actual person) was typing into it then the synchronization is significantly more complex. Locking any part of a shared database while waiting for a user action is most decidedly not recommended.
Before getting into something more exotic, look into transaction isolation and synchronization for your particular RDBMS and see if that will satisfy the requirement.
David Weber
Greenhorn

Joined: Nov 03, 2009
Posts: 9
Hi Chris,

What I've done is add a processing column to the orders table,

and when someone is editing the order the client app sets that order's processing to true,

and when the user exits the editor, the client app sets processing back to false.



If someone tries to launch the editor on an order that is being edited a Message pops up, to try again later.

I have added an override mechanism in case any orders get stuck on processing due to connection or app failure.

What do you think?

I know its not the safest and most efficient, but if only 10 users are using the system, this should suffice.

I only have about 6 months exp with Java and about 2 months exp with SQL.
Nitesh Kant
Bartender

Joined: Feb 25, 2007
Posts: 1638

how do you decide that the processing was stuck?
Can 2 users see the same account together. The first locks and the second overrides?
It will probably be a good idea to give override permission only to administrators, if you have an authentication/authorization mechanism in place.

I hope you commit the transaction after updating the flag.

apigee, a better way to API!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Synchronizing database record manipulation by multiple rmi clients
 
Similar Threads
JTable
JTable(!!!)
Problem with JTable
JTable
JTable