Hibernate manages Session and batch operation analysis

  • 2020-04-01 03:33:30
  • OfStack

This article has analyzed the usage of Hibernate to manage Session and batch operation in detail. Share with you for your reference. Specific analysis is as follows:

Hibernate Session management

Hibernate itself provides three ways to manage Session objects
The life cycle of the Session object is bound to the local thread
The lifetime of the Session object is bound to the JTA transaction
Hibernate delegate procedures to manage the Session object life cycle


In the Hibernate configuration file, the hibernate.current_session_context_class property is used to specify the Session management method. The optional values include:
The life cycle of the thread:Session object is bound to the local thread
Jta *: the lifetime of the Session object is bound to jta transactions
Managed :Hibernate delegate to manage the Session object lifecycle

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 configuration file to thread, Hibernate manages the Session as if it were bound to a local thread


Hibernate binds sessions to local threads according to the following rules:
When a thread first calls the getCurrentSession() method of the SessionFactory object, the method creates a new Session(sessionA) object, binds the object to 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 a transaction associated with a sessionA object, Hibernate automatically flush the cache of the sessionA object, then commits the transaction, closing the session heart. The sessionA object is also automatically closed when threadA revoks 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 threadA, and returns the sessionB

Batch processing data

Batch processing refers to processing a large amount of data in a transaction
In the application layer process batch operation, there are mainly the following ways:
(1) through the Session
(2) through the HQL
(3) through StatelessSession
Through the JDBC API-- this is recommended because it is the fastest

Session for batch operation:

Both the save() and update() methods of Session store the processed objects in their own cache. If you are dealing with a large number of persistent objects through a single Session object, you should immediately clear the cache of objects that have been processed and will not be accessed. This is done by calling flush() to flush the cache immediately after an object or small batch object has been processed, and then calling clear() to flush the cache

Processing via 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 in batches is the same as the batch size attribute

If the object USES an "identity" identity generator, Hibernate cannot perform bulk inserts in JDBC

It is recommended that Hibernate's level 2 cache be turned off for batch operations

Batch insert data code demo:

News 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: When making batch updates, it is obviously not desirable to load all objects into the Session cache at once and then update them in the cache one by one

Use a scrollable result set org. Hibernate. ScrollableResults, actually does not contain any object in the object, contains only the cursor positioning for online records. Only when the program iterates through certain elements of the ScrollableResults object does it load the corresponding object into the database
Org. Hibernate. ScrollableResults object by insensitive Query method returns

Batch operation through HQL:

Note: HQL only supports INSERT INTO... INSERT statements in the form of SELECT, but do not support INSERT INTO... Insert statements in the form of VALUES. So you can't do bulk inserts with HQL

Batch operation through StatelessSession:

Formally, StatelessSession is similar to the use of Session. StatelessSession differs from Session in the following ways:

StatelessSession has no cache, and objects loaded, saved, or updated by StatelessSession are in a free state
StatelessSession does not interact with Hibernate's level 2 cache
When the save(), update(), or delete() methods of StatelessSession are called, these methods immediately execute the corresponding SQL statement instead of just one SQL statement scheduled to execute
StatelessSession does not do dirty checking, so after you modify the Customer object property, you also need to call the update() method of StatelessSession to update the data in the database
StatelessSession does not perform any cascading operations on the associated objects
The Customer object whose OID is 1 is loaded twice by the same StatelessSession object, and the memory address of the two objects is different
The operations done by StatelessSession can be captured by an Interceptor Interceptor, but are ignored by Hibernate's event handling system

I hope this article has been helpful to your Java programming.


Related articles: