Hibernate execution Process:
1. Create a configuration class instance to read and parse The config file (e.g. Hibernate.cfg.xml), A configuration instance represents the set of Hibernate all Pojo classes to SQL database mappings;
2. Create the Sessionfactory object to Read and parse the mapping information , and copy all configuration information from the previous step to the sessionfactory cache;
3, open the session through the Sessionfactory object created in the previous step;
4. Start the transaction, the transaction refers to the interface provided by the session to the database crud operation.
Only by understanding hibernate's execution process can we better understand the sessionfactory creation process.
First put the code:
Packagecom;Importorg.hibernate.Session;Importorg.hibernate.SessionFactory;Importorg.hibernate.cfg.Configuration;ImportOrg.hibernate.service.ServiceRegistry;ImportOrg.hibernate.service.ServiceRegistryBuilder; Public classHibernatesessionfactory {Private StaticString config_file_location = "/hibernate.cfg.xml"; Private Static FinalThreadlocal<session> sessionthreadlocal =NewThreadlocal<session>();//Create a Threadlocal<session> object to hold the current Session objectPrivate StaticConfiguration Configuration =NewConfiguration (); Private Staticsessionfactory sessionfactory; Private StaticString ConfigFile =config_file_location; Static { Try{configuration.configure ();//Read and parse Hibernate.cfg.xml file
The hibernate4.x uses the Registrar to parse the mapping information, so the Serviceregistry object Serviceregistry is created first servicergistry=NewServiceregistrybuilder (). Applysettings (Configuration.getproperties ()). Buildserviceregistry ();
Serviceregister object as a parameter, create a Sessionfactory object using a Configuration object, copy the information from the configuration object to the Sessionfactory cache session Factory=configuration.buildsessionfactory (servicergistry); }Catch(Exception e) {e.printstacktrace (); } }
Declaring a private parameterless constructorPrivatehibernatesessionfactory () {} Public Staticsessionfactory getsessionfactory () {returnsessionfactory; } Public Static voidrebuildsessionfactory () {synchronized(sessionfactory) {Try{configuration.configure (configfile); Serviceregistry Serviceregistry=NewServiceregistrybuilder (). Applysettings (Configuration.getproperties ()). Buildserviceregistry (); Sessionfactory=configuration.buildsessionfactory (serviceregistry); }Catch(Exception e) {e.printstacktrace (); } } }
Open Session Public StaticSession getsession () {
Gets the session object of the current thread session session=Sessionthreadlocal.get (); Try{ if(Session = =NULL|| !Session.isopen ()) { if(Sessionfactory = =NULL{//If sessionfactory is null, create a rebuildsessionfactory (); }
If the session is not open, open the session with Sessionfactory= (sessionfactory!=NULL)? Sessionfactory.opensession ():NULL;
Place the session object in the Threadlocal object to use the Sessionthreadlocal.set (session); } }Catch(Exception e) {e.printstacktrace (); } returnsession; } Public Static voidCloseSession () {Session session=Sessionthreadlocal.get (); Sessionthreadlocal.set (NULL); Try{ if(Session! =NULL&&Session.isopen ()) {Session.close (); } }Catch(Exception e) {e.printstacktrace (); } } Public Static voidsetconfigfile (String configfile) {hibernatesessionfactory.configfile=ConfigFile; Sessionfactory=NULL; } Public StaticConfiguration getconfiguration () {returnconfiguration; }}
There is already some comment in the code, I believe enough to understand the code. One of the special notes here is that the jar package used by this sessionfactory creation code is hibernate4.x, and if you use more than 5.x, you need to make the following changes to the Create Sessionfactory process:
//Added Add (user.class), this user class is a Pojo class, corresponding to the database of a User table, hibernate.cfg.xml inside the configuration, if there is no such statement, will be an error: Unknow Entity xxx
Configuration. Add (User. Class // Read and parse the Hibernate.cfg.xml file // hibernate5.x uses the Registrar to parse the mapping information, so the Serviceregistry object is created first, using the Standardserviceregistrybuilder () serviceregistry servicergistry = new Standardserviceregistrybuilder (). Applysettings (Configuration.getproperties ()). build (); // serviceregister object as a parameter, creating a Sessionfactory object using a Configuration object, Copy the information in the configuration object to the Sessionfactory cache sessionfactory = configuration.buildsessionfactory ( Servicergistry);
Careful readers can see that the created Sessionfactory is thread-safe, so sessionfactory can be shared by multiple threads at the same time, but normally the session is not thread-safe, and each thread uses the session instance. If multiple threads operate the database at the same time, a thread closes the session, and the thread that is manipulating the database has an exception, in order to solve this problem, the code uses the Threadlocal object to secure the session thread, so that each thread will only manipulate the current session instance. copy , without affecting other threads.
Hibernate execution Process--sessionfactory creation