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"/>

	<class name="com.langhua.hibernate.pojo.CollectionMapping" table="t_lock" optimistic-lock="version">
		<id name="id">
		<version name="version"/>
		<property name="name"/>
		<property name="totle"/>

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

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!!!

分类:Java 时间:2009-04-02 人气:846
blog comments powered by Disqus


  • Oracle client handling the data row locking problem (select for update) 2010-07-07

    Data integrity and concurrency: Process the data in the network database, it is necessary to ensure data integrity, multi-transaction concurrency and data consistency. Database system must guarantee the maximum data content is effective and reasonabl

  • Research Report on Oracle Advanced Replication of data 2010-04-14

    Research Report on Oracle replication data Kwan Chan Time :2010-2-9 Using the scene description: Have a database server running, in order not to affect the usage of the database, a station needs a new database server Kai, the original database data a

  • oracle sql optimization and data 2010-11-03

    SQL write a good summary of other ways to improve efficiency, very practical, down In the massive data tables, each table has a basic or more of the indexes to ensure efficient query, the index in the ETL process used to follow the following principl

  • Oracle GoldenGate real-time using data integration 2010-12-07

    ] [Switched http://blog.chinaunix.net/u1/53677/showart_2314585.html Oracle GoldenGate real-time using data integration Learn how to install, set up and configure Oracle GoldenGate to easily achieve Oracle Database 10 g and 11 g Seamless replication o

  • Oracle table spaces and data files 2011-07-26

    --============================== - Oracle tablespace and data files --============================== / * First, the concept Table space: one or more logical collection of data files Table space logical storage objects: permanent section -> Table and

  • Oracle and SQL Server Data Type Mapping 2011-08-15

    2.2 Data Types This chapter provides detailed descriptions of the differences in data types used by Microsoft SQL Server and Oracle databases. Specifically, this chapter contains the following information: A table showing the base Microsoft SQL Serve

  • Oracle to create test data 2010-05-25

    Oracle to create test data shown in the Annex

  • About Oracle import and export data and objects [change] 2010-09-14

    About Oracle import and export data and objects [transfer]: http://www.cnblogs.com/oapro/archive/2006/04/14/374908.html

  • oracle imp to import data to another tablespace 2010-09-19

    oracle imp to import data to another tablespace Many people during data migration, we hope to import the data into the table space is different from the original system, are often found after import, the data is imported the original table space. Thi

  • [Reserved] Oracle table space and data files 2010-10-13

    The structure of the oracle and some other basic problems, or some confused, search, see this man's article, rewarding: oracle table spaces and data files

iOS 开发

Android 开发

Python 开发



PHP 开发

Ruby 开发






Javascript 开发

.NET 开发



Copyright (C) codeweblog.com, All Rights Reserved.

CodeWeblog.com 版权所有 黔ICP备15002463号-1

processed in 0.180 (s). 12 q(s)