This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes JDBC and the fly likes How to tackle simultaneous updates done by different user to a single webpage Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "How to tackle simultaneous updates done by different user to a single webpage" Watch "How to tackle simultaneous updates done by different user to a single webpage" New topic
Author

How to tackle simultaneous updates done by different user to a single webpage

Nachiket Patel
Ranch Hand

Joined: Sep 16, 2007
Posts: 110
Hello friends,
This question is about general web programming...
The problem is, when 2 users opens same webpage containing CRUD table of table X

Consider, User-1 at Time:t and User-2 at Time t+x
Now User-1 updates screen at Time t+x+y and User-2 updates same screen at Time t+x+y+z

So, User-1's update is overridden and User-1 does not know about it.
I want to invalidate User-2 update by showing User-2 screen: "you cannot update screen, as it is chaged by User-1 after you opened up the screen"

My question is: "Is there any standard way to tackle this problem??" (I am using Struts-Spring-Jdbc)

Regards,


Before software can be reusable, it first has to be usable. - Ralph Johnson
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

Yes. The normal pattern is to use something called Optimistic Locking (see this). Basically, all your records need to keep track of their version. Any client that reads from the database with a view to updating it will read this value into memory. Whan it comes to performing an update in your JDBC code you will first check the version in the database agains the value you have in memory. If they are different another thread has updated the data between the point you read it and the point you tried to update it so the application can inform the user the data is stale and prevent the update.


JavaRanch FAQ HowToAskQuestionsOnJavaRanch
krish laks
Greenhorn

Joined: May 18, 2007
Posts: 27
You can add a column named version in your table. You can start the value from 0 for that.
when you pick up a row from this table, pick up this column also. While updating this row, increment this version by 1 and update.

Whenever user saves the value check for this version and if the value in DB column and your input are same then it is fine to update. Otherwise you can throw the error.

For your case, say User-1 and User -2 would have picked up the row when the version is 0.
Now User-1 saves. So the version will become 1.
When User-2 tries to save it will throw error since this version is updated already.

Nachiket Patel
Ranch Hand

Joined: Sep 16, 2007
Posts: 110
Dear krish,
Are you suggesting that i should add 1 column (non domain) to all the tables,???

I don't think this is standard solution..
I am thinking of using database locking using JDBC.. But not clear...

Regards,
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

What krish is describing is one method of implementing Optimistic Locking and is fairly standard.

By "using database locking" I am assuming you are thinking you may use Pessimistic Locking (its worth having a read of the link I supplied). This will prevent lost updates, but at the cost adding a bottle neck to your data access.
krish laks
Greenhorn

Joined: May 18, 2007
Posts: 27
Hi Nachiket,

Yes it is pretty much industry standard. No worries. Even hibernate has implemented in this way only for row locking.
You can refer hibernate documentation if you need more details.

As Paul said, having lock at DB level is not a good idea, since it will affect the performance in considerable amount.

Nachiket Patel
Ranch Hand

Joined: Sep 16, 2007
Posts: 110
Thanks paul and krish,
I tried to find out at few places, but not getting much information...
May be i am using wrong search keyword, i am searching for "Optimistic Concurrency Control"...

Is there any alternatives??? May be in global context, i can keep note of last screen update time, and open time???
Because, i am not convienced with maintaining a field (even though it is a standard) for all the tables...


Regards,
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336


May be in global context, i can keep note of last screen update time, and open time???

In the database? Doesn't this tie your data to you view?

Because, i am not convienced with maintaining a field (even though it is a standard) for all the tables...

What do you thing the issue will be?
Nachiket Patel
Ranch Hand

Joined: Sep 16, 2007
Posts: 110

What do you thing the issue will be?

Result will be similar to what you told in reply to my first question."In the database? Doesn't this tie your data to you view?" . if i put versionId in each table (we got 215 tables), so now all table contains auxiliary information which is not about entity (or domain).

Consider, each screen is screen object with URL ,screen description and menu placement (which i already have), now which contains one more information last updated time,.. i think it will work... what do you say??

Regards,
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336


Result will be similar to what you told in reply to my first question."In the database? Doesn't this tie your data to you view?" . if i put versionId in each table (we got 215 tables), so now all table contains auxiliary information which is not about entity (or domain).

It doesn't tie it to your view. It does acknowledge clients will use the data, but doesn't say anything about how those clients are implemented. But your data model will have non-domain data in it anyway because you are presumably using surrogate keys? Requiring your data model to only contain domain data seems like a unnecessary limitation with no obvious benefit.


Consider, each screen is screen object with URL ,screen description and menu placement (which i already have), now which contains one more information last updated time,.. i think it will work... what do you say??

What do you do if you change you GUI to be a Swing client? What does any of the extra meta data provide you?

One thing to be aware of, "last update time" is not the best candidate to use to version data because (unlike a numeric version field) timestamps can clash. Its very rare but they are open to this issue, so you may still get the odd lost update.
Nachiket Patel
Ranch Hand

Joined: Sep 16, 2007
Posts: 110
One thing to be aware of, "last update time" is not the best candidate to use to version data because (unlike a numeric version field) timestamps can clash. Its very rare but they are open to this issue, so you may still get the odd lost update.

Right, Sorry, lets keep version and read/right of versionId with synchronization, In Java, with every screen object......
For swing client, i have to right some code, but we can reuse almost all the code which we write for taking care of this issue...
,
Okay,

In the case of keeping it into database, we need to use create some kind of class (or utility library) where we can pass table name with some additional required information for identifying that record, which tells whether this data is updated (stale) or not..Because i don't want to right down same code for all DAO's.
Is there any sample code link or code you have???

But, is there any other approach??

Regards,
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336


Right, Sorry, lets keep version and read/right of versionId with synchronization, In Java, with every screen object......

Not sure you need synchronization, unless you are refering to "synchronizing" the in-memory value with the database value rather than Java synchronization?


For swing client, i have to right some code, but we can reuse almost all the code which we write for taking care of this issue..

If you use a simple version number your DAO code will not need to change from one type of client to another.


In the case of keeping it into database, we need to use create some kind of class (or utility library) where we can pass table name with some additional required information for identifying that record, which tells whether this data is updated (stale) or not..Because i don't want to right down same code for all DAO's.

I would assume you could add this as easily enough to a base DAO. Your DAOs will know the table name and will (or at least should) aready have common code for a bunch of nuts-and-bolts operations.
Nachiket Patel
Ranch Hand

Joined: Sep 16, 2007
Posts: 110
The problem is i have never wrote (extensive) JDBC code.. ..
Although i used Hibernate and JPA (a bit) but not JDBC and JDBC-DAOs...

Thanks anyway.. i think i need to spend considerable amount of time for JDBC and DAO...

Regards,
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to tackle simultaneous updates done by different user to a single webpage