美文网首页
Hibernate框架学习(session详解)

Hibernate框架学习(session详解)

作者: 戴宏鹏 | 来源:发表于2017-04-26 10:46 被阅读0次

session详解

获得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>

<code>

@Test
public void testOpenSession() {
    // 获得配置对象
    Configuration config = new Configuration().configure();
    // 获得服务注册对象
    ServiceRegistry serviceRegistry = new ServiceRegistryBuilder().applySettings(config.getProperties())
            .buildServiceRegistry();
    SessionFactory sessionFactory = config.buildSessionFactory(serviceRegistry);
    Session session = sessionFactory.openSession();

    if (session != null) {
        System.out.println("session创建成功");
    } else {
        System.out.println("session创建失败");
    }
}

@Test
public void testGetCurrentSession() {
            //需要配置hibernate.cfg.xml文件,否则控制台不会有输出
    // 获得配置对象
    Configuration config = new Configuration().configure();
    // 获得服务注册对象
    ServiceRegistry serviceRegistry = new ServiceRegistryBuilder().applySettings(config.getProperties())
            .buildServiceRegistry();
    SessionFactory sessionFactory = config.buildSessionFactory(serviceRegistry);
    Session session = sessionFactory.getCurrentSession();
    
    if (session != null) {
        System.out.println("session创建成功");
    } else {
        System.out.println("session创建失败");
    }
}

</code>

getCurrentSession 是获取当前session对象(类似单例模式),连续使用多次时,得到的session都是同一个对象,这就是与openSession的区别之一 ;

<code>

@Test
public void testOpenSession() {
    // 获得配置对象
    Configuration config = new Configuration().configure();
    // 获得服务注册对象
    ServiceRegistry serviceRegistry = new ServiceRegistryBuilder().applySettings(config.getProperties())
            .buildServiceRegistry();
    SessionFactory sessionFactory = config.buildSessionFactory(serviceRegistry);

    Session session1 = sessionFactory.openSession();
    Session session2 = sessionFactory.openSession();
    
    System.out.println(session1 == session2); //false

}

@Test
public void testGetCurrentSession() {
    // 获得配置对象
    Configuration config = new Configuration().configure();
    // 获得服务注册对象
    ServiceRegistry serviceRegistry = new ServiceRegistryBuilder().applySettings(config.getProperties())
            .buildServiceRegistry();
    SessionFactory sessionFactory = config.buildSessionFactory(serviceRegistry);
    Session session1 = sessionFactory.getCurrentSession();
    Session session2 = sessionFactory.getCurrentSession();
    
    System.out.println(session1 == session2);  //true
    
}

</code>

openSession 每次使用都是打开一个新的session,使用完需要调用close方法关闭session,不关闭session会导致连接池溢出。

为了观察不手工关闭session的后果,我们可以打印两个连接对象的hashcode.
因为openSession打开的两个session是不同的,它们用的也就是不同的Connection连接对象,那么打印它们的hashcode显然也是不同的.
代码测试:

<code>

@Test
    public void testSaveStudentsWithOpenSession() {
        // 获得配置对象
        Configuration config = new Configuration().configure();
        // 获得服务注册对象
        ServiceRegistry serviceRegistry = new ServiceRegistryBuilder().applySettings(config.getProperties())
            .buildServiceRegistry();
    
        SessionFactory sessionFactory = config.buildSessionFactory(serviceRegistry);
    
        // 创建Session对象
        
        Session session1 = sessionFactory.openSession();
        // 开启事务
        Transaction transaction = session1.beginTransaction();
        // 生成一个学生对象
        Students stu = new Students(1, "张三", "男", new Date(), "北京");        
        session1.doWork(new Work(){

        @Override
        public void execute(Connection conn) throws SQLException {
            System.out.println("connection hashCode:" + conn.hashCode());
        }
    });
    session1.save(stu);
    transaction.commit(); // 提交事务
    // session1.close();

    Session session2 = sessionFactory.openSession();
    // 开启事务
    transaction = session2.beginTransaction();
    // 生成一个学生对象
    stu = new Students(2, "李四", "男", new Date(), "北京");
    session2.doWork(new Work(){

        @Override
        public void execute(Connection conn) throws SQLException {
            System.out.println("connection hashCode:" + conn.hashCode());
        }
    });
    session2.save(stu);
    transaction.commit(); // 提交事务

}

</code>

图片.png

可以看到两次的hashCode是不同的,证明了这两个session对应的Connection对象是不同的,第一个session没有显式的释放,第二个同样也没有释放,造成的后果是将来的数据库连接池有可能溢出.

getCurrentSession自动关闭session,相同的测试方法可以测出通过getCurrentSession获得的两个session,hashCode是一样的.

一般在实际开发中,往往使用getCurrentSession多,因为一般是处理同一个事务,所以在一般情况下比较少使用openSession;

相关文章

网友评论

      本文标题:Hibernate框架学习(session详解)

      本文链接:https://www.haomeiwen.com/subject/yrvbzttx.html