Hibernate manages sessions and batch operations, and hibernatesession

Source: Internet
Author: User

Hibernate manages sessions and batch operations, and hibernatesession

Hibernate Session management

 

Hibernate provides three methods to manage Session objects.
The Session Object lifecycle is bound to the local thread
The lifecycle of the Session object is bound to the JTA transaction.
Hibernate delegate program to manage the lifecycle of Session objects


In the Hibernate configuration file, the hibernate. current_session_context_class attribute is used to specify the Session management mode. Optional values include
Thread: binds the lifecycle of the Session object to the local thread.
Jta *: the lifecycle of the Session object is bound to the JTA transaction.
Managed: Hibernate delegate program to manage the lifecycle of Session objects

The Session Object lifecycle is bound to the local thread
If you set the attribute value of Hibernate. current_session_context_class in the hibernate configuration file to thread, Hibernate manages the Session by binding to the local thread.
  

Hibernate binds a Session to a local thread according to the following rules:
When a thread calls the getCurrentSession () method of the SessionFactory object for the first time, this method creates a new Session (sessionA) object and binds the object to threadA, and return the session
When threadA calls the getCurrentSession () method of the SessionFactory object again, this method returns the sessionA object
When threadA commits the transaction associated with the sessionA object, Hibernate will automatically flush the cache of the sessionA object, and then commit the transaction to close the session. When threadA revokes the transaction associated with the sessionA object, it also automatically closes the sessionA object
If threadA calls the getCurrentSession () method of the SessionFactory object again, this method creates a new Session (sessionB) object, binds the object to threadA, and returns sessionB


Batch Data Processing
Batch Data Processing refers to processing a large amount of data in a transaction.
You can perform batch operations at the application layer by using the following methods:
Through Session
Using HQL
Use StatelessSession
This is recommended through jdbc api ---- because the speed is the fastest

Batch operation of sessions:
The save () and update () Methods of the Session store the processed objects in their own cache. If you use a Session object to process a large number of persistent objects, you should promptly clear the objects that have been processed and will not be accessed from the cache. The specific method is to call the flush () method to refresh the cache immediately after processing an object or a small batch of objects, and then call the clear () method to cache the results.
  

Session processing is subject to the following constraints:
You need to set the number of JDBC batch processing in the Hibernate configuration file. Ensure that the number of batch SQL statements sent to the database each time is consistent with the batch size attribute.
If the object uses the "identity" identifier generator, Hibernate cannot perform batch insert operations in JDBC.
When performing batch operations, we recommend that you disable the Hibernate secondary cache.
Batch Data insertion code Demonstration:

 1 News news = null; 2 for(int i = 0; i < 10000; i++) { 3     news = new News(); 4     news.setTitle("--" + i); 5  6     session.save(news); 7     if((i + 1) % 20 == 0) { 8         session.flush(); 9         session.clear();10     }11 }

Batch update: during batch update, if all objects are loaded to the Session cache and updated one by one in the cache, it is obviously not desirable.
Using a scrollable result set org. hibernate. ScrollableResults, this object does not actually contain any object, but only contains the cursor for online location record. Only when the program traverses specific elements that access the ScrollableResults object will it load the corresponding objects in the database
The org. hibernate. ScrollableResults object is returned by the Query scroll method.

Batch operation through HQL
Note: HQL only supports INSERT statements in the form of insert into... SELECT, but does not support INSERT statements in the form of insert into... VALUES. Therefore, HQL cannot be used for batch insert operations.

Batch operation through StatelessSession
Formally speaking, StatelessSession and Session have similar usage. Compared with Session, StatelessSession has the following differences:
StatelessSession is not cached. objects loaded, saved, or updated through StatelessSession are in the Free State.
StatelessSession does not interact with Hibernate's second-level cache
When you call the save (), update (), or delete () Methods of StatelessSession, these methods immediately execute the corresponding SQL statement instead of executing only one SQL statement.
StatelessSession does not perform a dirty check. Therefore, after modifying the attributes of the Customer object, you must call the update () method of StatelessSession to update data in the database.
StatelessSession does not perform any cascade operations on the associated objects.
Using the same StatelessSession object to load the Customer object whose OID is 1 twice, the memory address of the two objects is different.
Operations performed by StatelessSession can be captured by the Interceptor, but will be ignored by the Hibernate event processing system.

Related Article

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.