transaction lock question

Discussions

General J2EE: transaction lock question

  1. transaction lock question (2 messages)

    If I want to implement locking mechanism in a program, when it is locked
    for read, then others cannot lock for write? Or read or write should
    be considered different actions, and doesn't matter?

    Please advise. thanks!!
  2. transaction lock question[ Go to top ]

    Are you talking about locking of data? If yes, why would you implement it from scratch? Databases provide locking that can be influenced by choosing appropriate ISOLATION LEVELS.
  3. transaction lock question[ Go to top ]

    If I want to implement locking mechanism in a program, when it is lockedfor read, then others cannot lock for write? Or read or write should be considered different actions, and doesn't matter?Please advise. thanks!!
    To get a good (and short) description of the different isolation levels in a relational database, take a look at section 10.2 "Transaction Isolation Levels" in the JDBC 3.0 specification. (I have "final draft 3" on my disk, but so the seciton number can have changed later.)

    The isolation level is more technical in the database. In you application, you normally have to go for "optimistic locking" or "pessimistic locking". In the first case, everyone can look at data. When you are doing an update of data, you will fail if someone else has made an update since you read data. "First update wins" is the strategy. With "pessimistic locking", you get a lock on data when reading. When you have the lock, other clients will get read-only copies of data. There must be a timeout on this lock. Otherwise you will lock data forever if something goes wrong (or the client just closes the browser).

    I know Martin Fowler has written about this in his book about enterprise patterns (http://www.amazon.com/exec/obidos/tg/detail/-/0321127420/qid=1090143418/sr=8-1/ref=sr_8_xs_ap_i1_xgl14/102-4380997-4128134?v=glance&s=books&n=507846).

    Another strategy is to just go for "last update wins" and then avoid the problem with locking on application level. (The isolation level in the database still has to be set or can use the default value provided by the EJB container.) This can be a good idea if you have a non-critical application and limited budget in the project.

    /Tomas