wood burning stoves 2.0*
The moose likes Object Relational Mapping and the fly likes Question about concurrency Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Question about concurrency" Watch "Question about concurrency" New topic
Author

Question about concurrency

Fabrizio Gianneschi
Ranch Hand

Joined: Nov 29, 2001
Posts: 70
Suppose we have two or more threads. Each thread obtains its Session from the SessionFactory and loads/saves some objects.
I wonder what is the concurrency status of these objects. In other words:

-if two threads execute the same "find" query, are the instance objects the SAME or only "equals"?

-if two threads are working on two persisted objects obtained from the same query (suppose in different methods), are subsequent changes to the first object automatically propagated into the second one?

Tks


Fabrizio Gianneschi<br />SCPJ2, SCWCD, SCBCD
Alexandru Popescu
Ranch Hand

Joined: Jul 12, 2004
Posts: 995
As written in the Chapter 2 of Hibernate2 reference doc you can see that:
  • SessionFactory: thread safe
  • Session: a single-threaded, short-lived object


  • Chapter 10.3 gives exactly the answers to your questions.

    ./pope


    blog - InfoQ.com
    Gavin King
    author
    Ranch Hand

    Joined: Aug 31, 2004
    Posts: 76
    Each Hibernate session works with a distinct set of persistent instances.


    Co-Author of <a href="http://www.amazon.com/exec/obidos/ASIN/193239415X/ref=jranch-20" target="_blank" rel="nofollow">Hibernate in Action</a>
    Fabrizio Gianneschi
    Ranch Hand

    Joined: Nov 29, 2001
    Posts: 70
    All right; docs are extremely clear.

    Thanks to Ali Pope and Gavin for the response.
    Pradeep bhatt
    Ranch Hand

    Joined: Feb 27, 2002
    Posts: 8919

    One question -How does Hibernate detect dirty reads?


    Groovy
    somkiat puisungnoen
    Ranch Hand

    Joined: Jul 04, 2003
    Posts: 1312
    Originally posted by Pradeep Bhat:
    One question -How does Hibernate detect dirty reads?


    It is not intended that users spend much time worring about locking strategies. Its usually enough to specify an isolation level for the JDBC connections and then simply let the database do all the work. However, advanced users may sometimes wish to obtain exclusive pessimistic locks, or re-obtain locks at the start of a new transaction.

    Hibernate will always use the locking mechanism of the database, never lock objects in memory!

    The LockMode class defines the different lock levels that may be acquired by Hibernate. A lock is obtained by the following mechanisms:

    LockMode.WRITE is acquired automatically when Hibernate updates or inserts a row.

    LockMode.UPGRADE may be acquired upon explicit user request using SELECT ... FOR UPDATE on databases which support that syntax.

    LockMode.UPGRADE_NOWAIT may be acquired upon explicit user request using a SELECT ... FOR UPDATE NOWAIT under Oracle.

    LockMode.READ is acquired automatically when Hibernate reads data under Repeatable Read or Serializable isolation level. May be re-acquired by explicit user request.

    LockMode.NONE represents the absence of a lock. All objects switch to this lock mode at the end of a Transaction. Objects associated with the session via a call to update() or saveOrUpdate() also start out in this lock mode.

    The "explicit user request" is expressed in one of the following ways:

    A call to Session.load(), specifying a LockMode.

    A call to Session.lock().

    A call to Query.setLockMode().

    If Session.load() is called with UPGRADE or UPGRADE_NOWAIT, and the requested object was not yet loaded by the session, the object is loaded using SELECT ... FOR UPDATE. If load() is called for an object that is already loaded with a less restrictive lock than the one requested, Hibernate calls lock() for that object.

    Session.lock() performs a version number check if the specified lock mode is READ, UPGRADE or UPGRADE_NOWAIT. (In the case of UPGRADE or UPGRADE_NOWAIT, SELECT ... FOR UPDATE is used.)

    If the database does not support the requested lock mode, Hibernate will use an appropriate alternate mode (instead of throwing an exception). This ensures that applications will be portable.


    SCJA,SCJP,SCWCD,SCBCD,SCEA I
    Java Developer, Thailand
    somkiat puisungnoen
    Ranch Hand

    Joined: Jul 04, 2003
    Posts: 1312
    Originally posted by Gavin King:
    Each Hibernate session works with a distinct set of persistent instances.



    when one person work with some Hibernate session, So this session locked or not ?

    another person can use this session ??
    Alexandru Popescu
    Ranch Hand

    Joined: Jul 12, 2004
    Posts: 995
    As I already wrote above the Session is single-threaded, so allowing 2 clients to access it concurrently from different threads is not an option.

    ./pope
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: Question about concurrency