我理解的Persistent Object Identity in Hibernate

bruce發表於2003-08-09
>Persistent Object Identity

>The application may concurrently access the same persistent >state in two different sessions. However, an instance of a >persistent class is never shared between two Session instances. >Hence there are two different notions of identity:

>Persistent Identity foo.getId().equals( bar.getId() )
>JVM Identity foo==bar

>Then for objects returned by a particular Session, the two >notions are equivalent. However, while the application might >concurrently access the "same" (persistent identity) business >object in two different sessions, the two instances will >actually be "different" (JVM identity).

>This approach leaves Hibernate and the database to worry about >concurrency (the application never needs to synchronize on any >business object, as long as it sticks to a single thread per >Session) or object identity (within a session the application >may safely use == to compare objects).

不知我這樣理解對不對?
當不同的Session併發訪問同一個PO時,在JVM中,不同的Session拿到不同的ID(是不是做了PO不同的副本?或者是timestamp什麼的?),這樣就可以避免PO同步問題,留給Hibernate 和資料庫處理。我認為Hibernate會使用樂觀鎖,在提交的時候檢查一下是否有變化,而對於資料庫,它也會用相應的各種鎖和事務來控制。

另外,不知道Hibernate在什麼時候用上了悲觀鎖. 還望指點一二。

相關文章