The main contents of this section:
1 introduces the difference between Getcurrentsession and opensession.
2 Demo: Compare two sessions by printing to verify that two sessions are the same session
3 Demo: Verify that the session is not closed by printing hashcode, easy to connect pool overflow.
1 The difference between getcurrentsession and opensession
A getcurrentsession each fetch is the same connection, opensession each gets a different connection.
b getcurrentsession does not need to be closed manually, and shuts down automatically after committing a transaction. Opensession need to be closed manually, if not closed, the number of connections exhausted, easy connection pool overflow.
2 Demo1
Package hibernate_001;
Import Java.sql.Array; Import Java.sql.Blob; Import java.sql.CallableStatement; Import Java.sql.Clob; Import Java.sql.DatabaseMetaData; Import Java.sql.NClob; Import java.sql.PreparedStatement; Import java.sql.SQLClientInfoException; Import java.sql.SQLException; Import java.sql.SQLWarning; Import Java.sql.SQLXML; Import Java.sql.Savepoint; Import java.sql.Statement; Import java.sql.Struct; Import Java.util.Date; Import Java.util.Map; Import java.util.Properties; Import Java.util.TimeZone; Import Java.util.concurrent.Executor;
Import org.hibernate.Session; Import Org.hibernate.SessionFactory; Import org.hibernate.Transaction; Import org.hibernate.cfg.Configuration; Import Org.hibernate.jdbc.Work; Import Org.hibernate.service.ServiceRegistry; Import Org.hibernate.service.ServiceRegistryBuilder; Import Org.junit.After; Import Org.junit.Before; Import Org.junit.Test;
Import com.ddwei.student.Student; import com.mysql.jdbc.Connection; import com.mysql.jdbc.ExceptionInterceptor; Import com.mysql.jdbc.Extension; Import com.mysql.jdbc.MySQLConnection; Import Com.mysql.jdbc.log.Log;
public class Studenttest { private sessionfactory sessionfactory; private session session; priva Te Transaction trasaction; @Test public void Testsavestudent () { //Get the Configuration object and get the contents of the Hibernate.cfg.xml configuration Configuration config = new configuration (). Configure (); //Gets the service registration object, gets the contents of the Hibernate.hbm.xml configuration serviceregistry sry = new Serviceregistrybuilder (). Applysettings (Config.getproperties ()). Buildserviceregistry (); sessionfactory sfy = config.buildsessionfactory (SRY); session Sesion1 = Sfy.opensession (); session Sesion2 = Sfy.opensession (); system.out.println (Sesion1==sesion2); // student Student =new Student (3, "Zhou Enlai", "male", New Date (), "Shaoxing");//Create Student Object// session.save (student);//Session Save student object into database } @Test public void TestSaveStudent2 () { //Get the Configuration object, get HibernatE.cfg.xml configuration content configuration config = new configuration (). Configure (); //Gets the service registration object, gets the contents of the Hibernate.hbm.xml configuration serviceregistry sry = new Serviceregistrybuilder (). Applysettings (Config.getproperties ()). Buildserviceregistry (); sessionfactory sfy = config.buildsessionfactory (SRY); session Sesion1 = Sfy.getcurrentsession (); session Sesion2 = Sfy.getcurrentsession (); system.out.println (Sesion1==sesion2); // student Student =new Student (3, "Zhou Enlai", "male", New Date (), "Shaoxing");//Create Student Object// session.save (student);//Session Save Student Object enter database }
}
Demo2
Package hibernate_001;
Import Java.sql.Array; Import Java.sql.Blob; Import java.sql.CallableStatement; Import Java.sql.Clob; Import Java.sql.DatabaseMetaData; Import Java.sql.NClob; Import java.sql.PreparedStatement; Import java.sql.SQLClientInfoException; Import java.sql.SQLException; Import java.sql.SQLWarning; Import Java.sql.SQLXML; Import Java.sql.Savepoint; Import java.sql.Statement; Import java.sql.Struct; Import Java.util.Date; Import Java.util.Map; Import java.util.Properties; Import Java.util.TimeZone; Import Java.util.concurrent.Executor;
Import org.hibernate.Session; Import Org.hibernate.SessionFactory; Import org.hibernate.Transaction; Import org.hibernate.cfg.Configuration; Import Org.hibernate.jdbc.Work; Import Org.hibernate.service.ServiceRegistry; Import Org.hibernate.service.ServiceRegistryBuilder; Import Org.junit.After; Import Org.junit.Before; Import Org.junit.Test;
Import com.ddwei.student.Student; Import com.mysql.jdbc.Connection; Import Com.mysql.jdbc.ExceptionInterceptor; Import com.mysql.jdbc.Extension; Import com.mysql.jdbc.MySQLConnection; Import Com.mysql.jdbc.log.Log;
public class StudentTest2 { private sessionfactory sessionfactory; private session session; priv Ate Transaction trasaction; @Test public void Testsavestudent () { //Get the Configuration object and get the contents of the Hibernate.cfg.xml configuration Configuration config = new configuration (). Configure (); //Gets the service registration object, gets the contents of the Hibernate.hbm.xml configuration serviceregistry sry = new Serviceregistrybuilder (). Applysettings (Config.getproperties ()). Buildserviceregistry (); sessionfactory sfy = config.buildsessionfactory (SRY); session sesion1 = Sfy.opensession (); sesion1.dowork (new work () { @Override public void Execute (java.sql.Connection Connection) throws SQLException { //TODO auto-generated method Stub system.out.println ("Connection.hashcode is" +connection.hashCode ()); } }); student Student =new Student (4, "Zhou Enlai", "male", New Date (), "Shaoxing");//Create Student Object sesion1.save (student);//Session Save Student Object Enter database
session sesion2 = sfy.opensession () student student2 =new Student (5, "Zhou Enlai", "male", New Date (), " Shaoxing ");//Create Student Object sesion2.dowork (new work () { @Override public void Execute (java.sql.Connection Connection) throws SQLException { //TODO auto-generated method Stub system.out.println ("Connection.hashcode is" +connection.hashCode ()); } }); sesion2.save (STUDENT2);//Session Save Student Object Enter database
} @Test public void TestSaveStudent2 () { //Gets the configuration object, Get the contents of the hibernate.cfg.xml configuration configuration config = new configuration (). Configure (); //Gets the service registration object, gets the contents of the Hibernate.hbm.xml configuration serviceregistry sry = new Serviceregistrybuilder (). Applysettings (Config.getproperties ()). Buildserviceregistry (); sessionfactory sfy = config.buildsessionfactory (SRY); session Sesion1 = Sfy.getcurrentsession (); transaction tran = Sesion1.begintransaction (); student Student =new Student (1, "Zhou Enlai", "male", New Date (), "Shaoxing");//Create Student Object sesion1.dowork (new work () { @Override public void execute (java.sql.Connection Connection) throws SQLException { //TODO auto-generated method Stub system.out.println ( "Connection.hashcode is" +connection.hashcode ()); } }); sesIon1.save (student);//Session Save Student Object Enter database tran.commit (); session sesion2 = Sfy.getcurrentsession (); transaction tran2 = Sesion2.begintransaction ();
Student student2 =new Student (2, "Zhou Enlai", "male", New Date (), "Shaoxing");//Create Student Object Sesion2.dowork (new work () {@Override Publ IC void Execute (java.sql.Connection Connection) throws SQLException {//TODO auto-generated method stub System.ou T.println ("Connection.hashcode is" +connection.hashcode ()); } }); Sesion2.save (Student2);//Session Save Student Object Enter database Tran2.commit (); }
}
A tentative study of Hibernate course single-table mapping 2-6 Session details (bottom)