Hibernate management session and batch operation analysis _java

Source: Internet
Author: User
Tags bulk insert flush

This paper analyzes the usage of Hibernate management session and batch operation in detail. Share to everyone for your reference. The specific analysis is as follows:

Hibernate Management Session

Hibernate itself 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


In the hibernate configuration file, the Hibernate.current_session_context_class property is used to specify session management, and optional values include:
The ①thread:session object's lifecycle is bound to the local thread
②jta*:session object's lifecycle and JTA transaction binding
③managed:hibernate delegate program to manage the lifecycle of the Session object

The life cycle of the session object is bound to the local thread:
If you set the Hibernate.current_session_context_class property value of the Hibernate profile to Thread,hibernate, the session is managed in a way that is bound to the local thread


Hibernate binds the session to the local thread according to the following rules:
When a thread (thread) invokes the Getcurrentsession () method for the Sessionfactory object for the first time, the method creates a new session (Sessiona) object that binds the object to the Threada. and returns the session
When Threada calls the Getcurrentsession () method of the Sessionfactory object again, the method returns the Sessiona object
When Threada commits the transaction associated with the Sessiona object, Hibernate automatically flush the cache of the Sessiona object, and then commits the transaction, closing the session as it pleases. The Sessiona object is also automatically closed when Threada undo the transaction associated with the Sessiona object
If Threada calls the Getcurrentsession () method of the Sessionfactory object again, the method creates a new session (SESSIONB) object, binds the object to the Threada, and returns the SESSIONB

Bulk processing of data

Bulk processing data is the process of handling large amounts of data in a transaction
Bulk operations in the application-tier process are mainly in the following ways:
① through session
② through HQL
③ through Statelesssession
The ④ is recommended by the JDBC API----because of the fastest

Session for bulk Operations:

The Save () and update () methods of the session 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 call the clear () method condition cache

Processing by session is subject to the following constraints:

You 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

Hibernate cannot have a bulk insert in JDBC If the object uses the "Identity" identity builder

It is recommended to turn off the level two cache of hibernate when doing bulk operations

Bulk INSERT Data Code Demo:

Copy Code code as follows:
News = null;
for (int i = 0; i < 10000; i++) {
News = new News ();
News.settitle ("--" + i);

Session.save (news);
if ((i + 1)% 20 = 0) {
Session.flush ();
Session.clear ();
}
}

batch update: in batch update, it is obviously undesirable to load all objects into the session cache and then cache the one by one update.

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
The Org.hibernate.ScrollableResults object is returned by the scroll method of query

Bulk operations via HQL:

Note: 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

Bulk operations via Statelesssession:

Formally, Statelesssession is similar to the usage of the session. Statelesssession compared to session, there are the following differences:

Statelesssession without caching, objects loaded, saved, or updated by Statelesssession are in a free State
Statelesssession does not interact with the hibernate level two cache
When the Statelesssession Save (), update (), or delete () method is invoked, the methods execute the appropriate SQL statement immediately, rather than just plan to execute a single SQL statement
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
Statelesssession does not perform any cascading operations on the associated objects
The 1 Customer object with the OID two loaded by the same Statelesssession object has a different memory address for two objects
Statelesssession can be captured by the Interceptor interceptor, but will be ignored by the Hibernate event-handling system

I hope this article will help you with your Java programming.

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.