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
"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.
Joined: Nov 03, 2009
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.
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.