本节主要内容:
1 介绍了getCurrentSession和opensession的区别
2 demo:通过打印比较两个session是否相同,验证两个session是否是同一session
3 demo:通过打印hashcode验证不关闭session,容易连接池溢出。
1 getCurrentSession和opensession的区别
a getCurrentSession每次获取的都是同一连接,opensession每次获取的是不同的连接。
b getCurrentSession不需要手动关闭,在提交事务之后自动关闭。opensession需要手动关闭,如果不关闭的话,连接数枯竭,容易连接池溢出.
c getCurrentSession 必须开启事务transaction,opensession不是必须(自我总结的)
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; private Transaction trasaction; @Test public void testSaveStudent(){ //获取配置对象,得到hibernate.cfg.xml配置的内容 Configuration config = new Configuration().configure(); //获取服务注册对象,获取hibernate.hbm.xml配置的内容 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,"***","男",new Date(),"绍兴");//创建学生对象 // session.save(student);//会话保存学生对象进入数据库 } @Test public void testSaveStudent2(){ //获取配置对象,得到hibernate.cfg.xml配置的内容 Configuration config = new Configuration().configure(); //获取服务注册对象,获取hibernate.hbm.xml配置的内容 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,"***","男",new Date(),"绍兴");//创建学生对象 // session.save(student);//会话保存学生对象进入数据库 }
}
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; private Transaction trasaction; @Test public void testSaveStudent(){ //获取配置对象,得到hibernate.cfg.xml配置的内容 Configuration config = new Configuration().configure(); //获取服务注册对象,获取hibernate.hbm.xml配置的内容 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,"***","男",new Date(),"绍兴");//创建学生对象 sesion1.save(student);//会话保存学生对象进入数据库
Session sesion2 = sfy.openSession(); Student student2 =new Student(5,"***","男",new Date(),"绍兴");//创建学生对象 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);//会话保存学生对象进入数据库
} @Test public void testSaveStudent2(){ //获取配置对象,得到hibernate.cfg.xml配置的内容 Configuration config = new Configuration().configure(); //获取服务注册对象,获取hibernate.hbm.xml配置的内容 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,"***","男",new Date(),"绍兴");//创建学生对象 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);//会话保存学生对象进入数据库 tran.commit(); Session sesion2 = sfy.getCurrentSession(); Transaction tran2 = sesion2.beginTransaction();
Student student2 =new Student(2,"***","男",new Date(),"绍兴");//创建学生对象 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);//会话保存学生对象进入数据库 tran2.commit(); }
}