Hibernate框架学习(session详解)

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;

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 1. Java基础部分 基础部分的顺序:基本语法,类相关的语法,内部类的语法,继承相关的语法,异常的语法,线程的语...
    子非鱼_t_阅读 31,917评论 18 399
  • 从三月份找实习到现在,面了一些公司,挂了不少,但最终还是拿到小米、百度、阿里、京东、新浪、CVTE、乐视家的研发岗...
    时芥蓝阅读 42,444评论 11 349
  • Hibernate: 一个持久化框架 一个ORM框架 加载:根据特定的OID,把一个对象从数据库加载到内存中OID...
    JHMichael阅读 6,046评论 0 27
  • 1. 简介 1.1 什么是 MyBatis ? MyBatis 是支持定制化 SQL、存储过程以及高级映射的优秀的...
    笨鸟慢飞阅读 11,146评论 0 4
  • 人和人生来就不一样,但是天生的模仿能力却好像能改变一切,人们总是觉得别人怎么怎么样很好我也要怎么怎么样,如此反复...
    脑子不是用来吃的阅读 4,697评论 0 0