[Original]java Web Learning Note 94:hibernate Learning Path---Session management, Session object lifetime and local thread binding

Source: Internet
Author: User

The purpose of this blog: ① summary of their own learning process, equivalent to study notes ② to share their own experience to everyone, learn from each other, communication, not commercialcontent inevitably appear problems, welcome to correct, exchange, discussion, you can leave a message, can also be contacted by the following ways. I Internet technology enthusiasts, Internet technology enthusiastsWeibo: Ivan is in 0221qq:951226918

--------------------------------------------------------------------------------------------------------------- --------------------------------------------------------------------------------------------------------------- -----------------------------

1. Manage Session

1) Hibernate itself provides three ways to manage Session objects

Session object lifetime and local thread binding

②session object life cycle and JTA transaction binding

③hibernate the life cycle of the Session object by the entrusted program management

2) in Hibernate configuration file, the Hibernate.current_session_context_class attribute is used to specify the session management mode, and the optional values include

   ①thread:session the life cycle of an object with local thread bindings

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

③managed:hibernate delegate program to manage the life cycle of Session objects

2.The Session object's life cycle is bound to the local thread

1) If you set the Hibernate.current_session_context_class property valueof the Hibernate profile to thread, hibernate will be managed in a way that is bound to the local thread Session

2) Hibernate binds the Session to the local thread by pressing the rules

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

② when Threada calls the Sessionfactory object's Getcurrentsession () method 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, then commits the transaction and closes the Sessiona object. The Sessiona object is also automatically closed when Threada revokes the transaction associated with the Sessiona object

④ if Threada calls the Sessionfactory object's Getcurrentsession () method again, the method creates a new Session (SESSIONB) object, binds the object to Threada, and Sessio NB return

Create a single instance by using the singleton mode Hibernateutils Tool Class

1  PackageCom.jason.hibernate.dao;2 3 Importorg.hibernate.Session;4 Importorg.hibernate.SessionFactory;5 Importorg.hibernate.cfg.Configuration;6 ImportOrg.hibernate.service.ServiceRegistry;7 ImportOrg.hibernate.service.ServiceRegistryBuilder;8 9  Public classHibernateutils {Ten  One     Privatehibernateutils () {} A      -     Private StaticHibernateutils instance =Newhibernateutils (); -      the     Privatesessionfactory sessionfactory; -      -      Public Statichibernateutils getinstance () { -         returninstance; +     } -      +     Privatesessionfactory getssessionfactory () { A          at         if(Sessionfactory = =NULL){ -Configuration Configuration =NewConfiguration (). Configure (); -Serviceregistry Serviceregistry =NewServiceregistrybuilder () - . Applysettings (Configuration.getproperties ()) - . Buildserviceregistry (); -  inSessionfactory =configuration.buildsessionfactory (serviceregistry); -         } to          +         returnsessionfactory; -     } the      *      $      PublicSession getssession () {Panax Notoginseng         returngetssessionfactory (). Getcurrentsession (); -     } the      +      A      the      +}

3. Batch processing of data

1) batch processing of data refers to the processing of large amounts 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 Statelesssession; through the JDBC API;

  

3) batch operation via Session

The Save () and update () methods of the ①session store the processed objects in their own cache. If you are working with a large number of persisted objects through a Session object, you should clear the cache of objects that have been processed and will not be accessed in a timely manner . It is a practice to call the flush () method to flush the cache immediately after processing an object or a small batch object, and then empty the cache when the clear () method is called

  

② is subject to the following constraints when it is processed through the Session:

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

> Hibernate cannot bulk Insert at the JDBC layer if the object uses the "identity" identifier generator

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

Batch update : When a batch update, if all the objects are loaded into the Session cache, and then cached in the one by one update, it is obviously undesirable;

Using 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 iterates through a particular element of a Scrollableresults object does it load the appropriate object into the database.

    

4. Bulk operation via HQL: HQL only supports INSERT into ... Insert statement in the form of SELECT, but does not support insert INTO ... The INSERT statement in the VALUES form. Therefore, the bulk insert operation cannot be performed using HQL

5. Batch operation via statelesssession: In form, Statelesssession is similar to session usage

Statelesssession compared to the session, there are the following differences:

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

2) Statelesssession does not interact with Hibernate's second-level cache.

3) When you call the Save (), update (), or delete () method of Statelesssession, these methods execute the corresponding SQL statement immediately, and do not plan to execute only one SQL statement

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

5) Statelesssession does not perform any cascade operations on the associated objects.

6) through the same Statelesssession object two times to load the OID 1 of the Customer object, the resulting two object memory address is different.

7) The statelesssession operation can be captured by the Interceptor interceptor, but will be ignored by Hibernate's event handling system.

[Original]java Web Learning Note 94:hibernate Learning Path---Session management, Session object lifetime and local thread binding

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.