获取session对象
1)openSession
2) getCurrentSession
使用getCurrentSession需要在hibernate.cfg.xml文件中进行配置:
如果是本地事务的(jdbc事务)
<property name="hibernate.current_session_context_class">thread</property>
如果是全局事务的(jta事务)(不常用)
<property name="hibernate.current_session_context_class">jta</property>
openSession 与 getCurrentSession的区别
1、 getCurrentSession在事务提交或者回滚的时候会自动关闭,
而openSession需要你手动关闭,若是没有手动关闭多次连接则会造成连接池溢出的后果。
2、openSession每次都是创建的新的session对象,getCurrentSession使用的是现有的session对象(单例模式)。
package test;
import static org.junit.Assert.*;
import java.sql.Connection;
import java.sql.SQLException;
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.Test;
import pojo.Student;
public class SessionTest {
@Test
public void testOpenSession() {
Configuration config = new Configuration().configure();//获得配置对象
//获得服务注册对象
ServiceRegistry serviceRegistry = new ServiceRegistryBuilder().applySettings(config.getProperties()).buildServiceRegistry();
//获得sessionFactory对象
SessionFactory sessionFactory = config.buildSessionFactory(serviceRegistry);
//获得session对象
Session session1 = sessionFactory.openSession();
Session session2 = sessionFactory.openSession();
System.out.println(session1==session2); //false
/* if(session!=null){
System.out.println("session创建成功!");
}else{
System.out.println("session创建失败");
}
*/
}
@Test
public void testGetCurrentSession(){
Configuration config = new Configuration().configure();//获得配置对象
//获得服务注册对象
ServiceRegistry serviceRegistry = new ServiceRegistryBuilder().applySettings(config.getProperties()).buildServiceRegistry();
//获得sessionFactory对象
SessionFactory sessionFactory = config.buildSessionFactory(serviceRegistry);
//获得session对象注意其中要在hibernate.cfg.xml中配置属性 <property name="hibernate.current_session_context_class">thread</property>
Session session1 = sessionFactory.getCurrentSession();
Session session2 = sessionFactory.getCurrentSession();
System.out.println(session1==session2); //true
/* if(session!=null){
System.out.println("session创建成功2!");
}else{
System.out.println("session创建失败");
}*/
}
@Test
public void testSaveOpenSession(){
Configuration config = new Configuration().configure();//获得配置对象
//获得服务注册对象
ServiceRegistry serviceRegistry = new ServiceRegistryBuilder().applySettings(config.getProperties()).buildServiceRegistry();
//获得sessionFactory对象
SessionFactory sessionFactory = config.buildSessionFactory(serviceRegistry);
//获得session对象
Session session1 = sessionFactory.openSession();
//开启事务
Transaction transaction = session1.beginTransaction();
//创建对象
Student student = new Student("张六",4);
session1.doWork(new Work() {
@Override
public void execute(Connection connection) throws SQLException {
System.out.println("connection hashcode:"+connection.hashCode());
}
});
session1.save(student);
//session1.close();
transaction.commit();
//获得session2对象
Session session2 = sessionFactory.openSession();
//开启事务
transaction = session2.beginTransaction();
//创建对象
student = new Student("张七",4);
session2.doWork(new Work() {
@Override
public void execute(Connection connection) throws SQLException {
System.out.println("connection hashcode:"+connection.hashCode()); //两次输出的连接code不一样
}
});
session2.save(student);
//session2.close();
transaction.commit();
}
@Test
public void testSaveGetCurrentSession(){
Configuration config = new Configuration().configure();//获得配置对象
//获得服务注册对象
ServiceRegistry serviceRegistry = new ServiceRegistryBuilder().applySettings(config.getProperties()).buildServiceRegistry();
//获得sessionFactory对象
SessionFactory sessionFactory = config.buildSessionFactory(serviceRegistry);
//获得session对象
Session session1 = sessionFactory.getCurrentSession();
//开启事务
Transaction transaction = session1.beginTransaction();
//创建对象
Student student = new Student("张六",4);
session1.doWork(new Work() {
@Override
public void execute(Connection connection) throws SQLException {
System.out.println("connection hashcode:"+connection.hashCode());
}
});
session1.save(student);
transaction.commit();
//获得session2对象
Session session2 = sessionFactory.getCurrentSession();
//开启事务
transaction = session2.beginTransaction();
//创建对象
student = new Student("张七",4);
session2.doWork(new Work() {
@Override
public void execute(Connection connection) throws SQLException {
System.out.println("connection hashcode:"+connection.hashCode()); //两次输出的连接code一样
}
});
session2.save(student);
transaction.commit();
}
}