Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Question transaction in several request

 
Evaristo Ballorca
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello every body, i've a problem and i've been studying it from monday. Th e application is a web application( for a intranet ). My problem is that i want a database transaction in several request. It's posible??? I've been studying JTA, but it's for distribute transaction, one transaction with several connection that access distinct data resources.
A example is that in a request i load the client data to be updated, and i want that nobody could change this record. I've thing to implement this operative with flags. Could i do it in other away??
Thanks you very much??
Regards
 
Lasse Koskela
author
Sheriff
Posts: 11962
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"eballosa",
May I ask you to edit your display name so that it conforms to our naming policy.
Thanks.
 
Evaristo Ballorca
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've just changed it.
 
David Harkness
Ranch Hand
Posts: 1646
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by eballosa:
My problem is that i want a database transaction in several request. It's posible??? I've been studying JTA, but it's for distribute transaction, one transaction with several connection that access distinct data resources.
A example is that in a request i load the client data to be updated, and i want that nobody could change this record. I've thing to implement this operative with flags. Could i do it in other away??

In general you want your transactions to be very short. Doing a lot of processing during the transaction is one thing. Leaving a transaction active while the user modifies fields in the HTML form, goes to get some coffee, decides to play a little ping pong, and then goes to lunch, is not a good idea.
One solution is to tag each object/row with a version number (usually stored in the database). When you load the record for the user, send the current version number. Once the user has made their changes and submitted it, check the version number they gave you (read earlier) against the current value. If they are the same, save the user's new values and *increment* the version number. If the version numbers differ, it means that another user modified the record while the first user was working on it.
At this point you can either deny the first user's edits with an error message, refresh the data (merge with their changes to be super-cool) from the database and let them try again, or provide an option to "force" the edit, overwriting what the changes made by the second user.
Basically, the goal is to allow the system to remain usable at all times. In the rare cases where a user's edits are blocked, they'll learn to save their changes before going to lunch.
 
Evaristo Ballorca
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
David, thank you very much. It's a very good solution. I never thought in this way.
Thanks.
 
Evaristo Ballorca
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello David, i'm looking for same information about this mechanism in internet. I thing that exist diferent strategies to do this in web application. Do you know any web site wher i could read something about that??
thanks again
 
Chris Mathews
Ranch Hand
Posts: 2712
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What David is referring to is know as offline locking. Probably the best resource I have seen on this is Martin Fowler's most recent book: Patterns of Enterprise Application Architecture. I highly suggest you pick up a copy of it... and not just because of this problem. It is an incredible book!
Back to your question... there are basically two types of locking: Optimistic and Pessimistic. Take a look at these links for a description of each as they apply to offling locking:
Optimistic Offline Lock
Pessimistic Offline Lock
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic