hibernate frequently used when many individuals of the same data at the same time when changes will happen dirty data, resulting in data inconsistency, the solution can be adopted by the pessimistic and optimistic locking to achieve lock.
Pessimistic locking: At a time when the data load has given its locked until the lock was released out, other users can modify advantages: the consistency of data maintained very well Disadvantages: not suitable for multiple users concurrent visit. When a resource is not locked out when the release of the resources will never be modified by other users, easy to cause an indefinite wait.
Optimistic locking: is the modification of data when the data only go version or timestamp, such as comparison of data consistency to achieve locking. The advantages of better.
1Pessimistic locking, as long as the load time to the load method in the session, for the chain ,session.load(****.class,1,LockMode.UPDATE); HibernateTransaction management is delegated to the underlying JDBC or JTA, default is based on JDBC Transaction. HibernateSupport the pessimistic locking (Pessimistic Locking) "and" optimistic lock (Optimistic Locking) ". Pessimistic lock on the data is external to modify the conservative attitude, therefore, the whole data processing, the data in a locked state. Pessimistic lock implementations, often depending on the database lock mechanism. Hibernate by using the database of the for update clause enables pessimistic locking mechanism. Hibernate's lock modes are: 1. LockMode.NONE: No lock mechanism 2. LockMode.WRITE: Hibernate in insert and update records automatically gets 3. LockMode.READ: Hibernate in reading record automatically gets 4. LockMode.UPGRADE: Use database for update clause lock 5. LockMode.UPGRADE_NOWAIT: Oracle specific implementation, use Oracle's implementation for nowait clause of the update lock 2Optimistic locking, mostly based on the data version (Version) logging mechanism. Hibernate in their data access engine built into optimistic locking implementation, you can use the class descriptor of optimistic -lockProperty for the specified version descriptor. optimistic -lockThe property has the following optional values: 1. none: No optimistic locking 2. versionThrough version mechanism to implement optimistic locking 3. dirty: By examining the change over of property implements optimistic locking 4. all: By checking all properties implement optimistic locking
2, optimistic locking:
1> At the basis of pessimistic locking on add private int version; and related setter, getter methods.
2> mapping file configuration in the class add tags inside the optimistic-lock = "version", then at the back of the id tag add <version name="version"/>
<hibernate-mapping> <class name="com.langhua.hibernate.pojo.CollectionMapping" table="t_lock" optimistic-lock="version"> <id name="id"> <generator/> </id> <version name="version"/> <property name="name"/> <property name="totle"/> </class> </hibernate-mapping>
lazy strategy can be used in:
* <class> Tag on: You can value true / false
* <property> Tags on, you can value true / false, this type of enhanced features required
* <set> / <list> Set up, etc., can be value for the true / false / extra
* <one-to-one> / <many-to-one> Such as tags on, you can value false / proxy / no-proxy
1, lazy concept, refers to the time required before the issue of sql
2, lazy strategy is only open during the session to be effective
Hibernate Class Level of lazy loading strategy: You can control when the property to load these ordinary objects
Hibernate lazy loading property collection strategies:
Set up in the property, you can configure the lazy loading strategy, value for: true / false / extra
true: the default value, which means that only in this collection calls to obtain the elements inside the object when it is issued query, load the data collection element of its
false: the abolition of lazy loading characteristics, namely, the object is loaded at the same time, it issued a second query to load its associated data collection
extra: a more clever lazy loading strategy, that is, call a collection of size / contains methods such as time, hibernate will not go to load the entire collection of data, but issued a smart SQL statement in order to obtain the required value only at those in genuine need to use object data collection element of time before trying to issue a query to load all objects in the data
Single-ended association Hibernate lazy loading strategy: that is, at <one-to-one> / <many-to-one> tag can be configured on
Lazy loading strategy.
Can be value for: false / proxy / no-proxy
false: the abolition of the lazy load strategy, that is, the object is loaded at the same time, issued a query, load its associated objects
proxy: This is hibernate for the single-ended linked to the default lazy loading strategy, that is, only in the call to its associated object when it is really the issue of query about their object data, and its associated objects are agent-type no-proxy: this lazy loading characteristics of the type required to enhance the use of no-proxy, its associated object is not a type of agent
NOTE: Labels in class on lazy allocation of property will not affect the associated object!!!