hibernate in the pessimistic and optimistic locking lock and lazy loading

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

Key concepts:
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

Note:
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!!!
  • del.icio.us
  • StumbleUpon
  • Digg
  • TwitThis
  • Mixx
  • Technorati
  • Facebook
  • NewsVine
  • Reddit
  • Google
  • LinkedIn
  • YahooMyWeb

Related Posts of hibernate in the pessimistic and optimistic locking lock and lazy loading

  • eclipse Hibernate proposals

    Error: The 'Hibernate proposals' proposal computer from the' org.hibernate.eclipse.jdt.ui 'plug-in dod not complete normally. Unable to instantiate the extension. To Avoid this message, disable the 'org.hibernate.eclipse.jdt.ui ...

  • Rails2.0.2 change the default DB adpter

    In Rails2.0.2 rails demo ... ... MissingSourceFile in SayController # hello no such file to load - sqlite3 RAILS_ROOT: / home / kenb / rails-projects / demo ... ... Checked config / database.yml, adpter default is set become the sqlite3. Check the ra ...

  • The company first entered the company I would like to Optimize SQL project

    I was a time not long into the company of people, but also a person to be graduated, but immediately entered the project team, involved in the development. The project is to make a China Telecom Operation System, is a big system. My so-called develop ...

  • In the Spring to configure Service

    JTA in Spring Development Environment: JDK1.5 (sun) + Myeclipse6.0 + Tomcat5.5 + ant 1.7.1 + MySql5.0.4 Framework version: JSF1.2 (sun) + hibernate3.3.1.GA + spring2.5.6 JTA (Java Transaction API) Only supports the standard EJB Persistence (JTA) transacti

  • Hibernate primary key strategy-sequence

    Today, the use of hibernate in the company encountered a troublesome problem, the use of hibernate when the primary key generation strategy set sequence, but always reported in the implementation could not get next sequence value of the error, then o ...

  • Hibernate pessimistic locking mechanism for locking and optimistic locking

    hibernate lock mechanism 1. Pessimistic lock It refers to the modification of data by outsiders hold a conservative attitude. The assumption that at any time access to data, may also have another client to access the same data, in order to maintain t ...

  • Oracle instant clent for ruby / rails on cygwin

    Environment: XP: oracle full client, ruby, rails, gem cygwin: ruby rails, gem (the version with the XP version) Needs: for cygwin is installed under the rails platform support oci Steps: <1> download oracle instant client (10.2.0.3 Instant Client Pa

  • spring + hibernate + oracle9i to read and write CLOB

    Database-driven update classes12-9i.jar Hibernate modify the configuration of the following code <bean/> <bean Lazy-init="true"> <property name="nativeJdbcExtractor"> <ref local="nativejdbcExtractor"/>

  • Hibernate connection pool configuration

    Hibernate connection pool configuration <! - Jdbc -> <property name="connection.driver_class"> oracle.jdbc.driver.OracleDriver </ property> <property name="connection.url"> jdbc: oracle: thin: @ 10.203.14.132:15

  • The EJB3 Persistence

    EJB3 persistence with Hibernate is very similar to the mechanism: Environment: Server: JBOSS5.0 Database: MySQL5.0 1. Set up a data source First of all, in jboss-5.0.0.GA \ server \ default \ deploy, the establishment of a database used to connect the dat

blog comments powered by Disqus
Recent
Recent Entries
Tag Cloud
Random Entries