The life cycle of the session object is bound to the local thread _session

Source: Internet
Author: User
Tags bulk insert

1. Management session

1) Hibernate provides three ways to manage session objects

The ①session object's lifecycle is bound to the local thread

②session object's lifecycle and JTA transaction binding

③hibernate Delegate program manages the life cycle of Session objects

2 in the Hibernate configuration file, the Hibernate.current_session_context_class property is used to specify the session management method, and the optional values include

The ①thread:session object's lifecycle is bound to the local thread

②jta*: The lifecycle of the session object and JTA transaction binding

③managed:hibernate delegate program to manage the lifecycle of the Session object

The life cycle of 2.Session objects is bound to the local thread

1 if the Hibernate.current_session_context_class attribute value of the Hibernate configuration file is set to thread, Hibernate will manage the session in a way that is bound to the local thread

2) Hibernate The session with the local thread by clicking the rule

① when a thread (Threada) invokes the Getcurrentsession () method of the Sessionfactory object for the first time, the method creates a new session (Sessiona) object, binds the object to the Threada, and adds the SE Ssiona return

② when Threada calls the Getcurrentsession () method of the Sessionfactory object again, the method returns the Sessiona object

③ when Threada commits a transaction associated with a Sessiona object, Hibernate automatically flush the cache of the Sessiona object, then commits the transaction and closes the Sessiona object. The Sessiona object is also automatically closed when Threada undo the transaction associated with the Sessiona object

④ if Threada invokes the Getcurrentsession () method of the Sessionfactory object again, the method creates a new session (SESSIONB) object, binds the object to the Threada, and Sessio NB return

Create a Hibernateutils tool class from a single case pattern

 1 package Com.jason.hibernate.dao;
 2 3 Import org.hibernate.Session;
 4 Import org.hibernate.SessionFactory;
 5 Import org.hibernate.cfg.Configuration;
 6 Import Org.hibernate.service.ServiceRegistry;
 7 Import Org.hibernate.service.ServiceRegistryBuilder; 8 9 public class Hibernateutils {one private hibernateutils () {} private static hibernateutils ins
tance = new Hibernateutils ();
Sessionfactory private sessionfactory; The public static Hibernateutils getinstance () {instance] private Ses Sionfactory getssessionfactory () {sessionfactory = null) {Configuration Configur
ation = new Configuration (). Configure (); Serviceregistry serviceregistry = new Serviceregistrybuilder () applysettings (config
Uration.getproperties ()) buildserviceregistry (); Sessionfactory = Configuration.buIldsessionfactory (Serviceregistry);
return to Sessionfactory;
The public session getssession () {Panax Notoginseng return getssessionfactory (). Getcurrentsession (); 38} 39 40 41 42 43}

3. Batch processing data

1 batch processing data refers to processing a large amount of data in a transaction.

2 in the application layer for bulk operations, mainly in the following ways: Through the session, through the HQL; through the statelesssession; through the JDBC API;

  

3 Batch operation through session

①session's Save () and update () methods store the processed objects in their own cache. If you handle a large number of persistent objects through a Session object, you should empty the cache of objects that have been processed and that are not accessed. The practice is to call the flush () method to flush the cache immediately after processing an object or a small batch object, and then empty the cache by calling the clear () method

  

② processing through session is subject to the following constraints

> need to set the number of JDBC single batch processing in the Hibernate configuration file to ensure that the number of SQL statements sent to the database is consistent with the Batch_size property

> If the object uses the "identity" identifier builder, Hibernate cannot perform bulk insert operations at the JDBC layer

> It is recommended to turn off the Hibernate level two cache when doing bulk operations

③ batch update: In the batch update, if all the objects are loaded into the session cache, and then cache one by one update, it is obviously undesirable;

Using a scrollable result set org.hibernate.ScrollableResults, the object does not actually contain any objects, only cursors that are used to locate records online. Only when a program traverses a specific element that accesses a Scrollableresults object does it load the corresponding object into the database.

    

4. Batch operation via HQL: HQL only supports INSERT into ... Insert statement in SELECT form, but insert into ... The INSERT statement in the form of VALUES. Therefore, you cannot use HQL for BULK INSERT operations

5. Bulk operations via statelesssession: From a formal perspective, Statelesssession is similar to session usage

Statelesssession compared to session, there are the following differences:

1 statelesssession is not cached, the object that is loaded, saved or updated through Statelesssession is in a free State

2) Statelesssession does not interact with the second-level cache of Hibernate.

3 when the Save (), update (), or delete () method of the Statelesssession is invoked, the methods execute the appropriate SQL statement immediately, rather than just plan to execute a single SQL statement

4) Statelesssession does not perform a dirty check, so after modifying the Customer object properties, you also need to call the Statelesssession update () method to update the data in the database.

5) Statelesssession does not perform any cascading operations on the associated object.

6 The Customer object with OID 1 is loaded two times by the same Statelesssession object, and the resulting two object memory addresses are different.

7 The operation done by Statelesssession can be captured by the Interceptor interceptor, but it will be ignored by the Hibernate event handling system.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.