hibernate的session详解

一 opensession与getCurrentSession的区别
1、 getCurrentSession在事务提交或者回滚之后会自动关闭,而openSession需要手动关闭。如果使用openSession而没有手动关闭,多次之后会导致连接池溢出。
2、openSession每次创建新的session对象,getCurrentSession使用现有的session对象。
 
二 测试openSession每次创建新的session对象
1、测试代码
        @Test
        public void testOpenSession(){
            // 创建配置对象  
            Configuration config = new Configuration().configure();
            // 创建服务注册对象
            ServiceRegistry serviceRegistery = new ServiceRegistryBuilder().applySettings(config.getProperties()).buildServiceRegistry();
            // 创建会话工厂对象
            SessionFactory sessionFactory = config.buildSessionFactory(serviceRegistery);
            // 创建会话对象
            Session session1 = sessionFactory.openSession();
            Session session2 = sessionFactory.openSession();
            System.out.println(session1==session2);                        
        }
2、运行结果
false
 
三 测试getCurrentSession使用现有的session对象
1、测试代码
        @Test
        public void testGetCurrentSession(){
            // 创建配置对象  
            Configuration config = new Configuration().configure();
            // 创建服务注册对象
            ServiceRegistry serviceRegistery = new ServiceRegistryBuilder().applySettings(config.getProperties()).buildServiceRegistry();
            // 创建会话工厂对象
            SessionFactory sessionFactory = config.buildSessionFactory(serviceRegistery);
            // 创建会话对象
            Session session1 = sessionFactory.getCurrentSession();
            Session session2 = sessionFactory.getCurrentSession();
            System.out.println(session1==session2);
        }
2、运行结果
true
 
四 openSession用完后必须手动关闭,否则会溢出
1、测试代码
       @Test
        public void testSaveStudentswithOpenSession(){
            // 创建配置对象  
            Configuration config = new Configuration().configure();
            // 创建服务注册对象
            ServiceRegistry serviceRegistery = new ServiceRegistryBuilder().applySettings(config.getProperties()).buildServiceRegistry();
            // 创建会话工厂对象
            SessionFactory sessionFactory = config.buildSessionFactory(serviceRegistery);
            // 创建会话对象
            Session session1 = sessionFactory.openSession();
            // 开启事务
            Transaction transaction = session1.beginTransaction();
                Students s= new Students(1,"张三丰","男",new Date(),"武当山");
                session1.doWork(new Work(){
 
                        @Override
                        public void execute(Connection connection) throws SQLException {
                                // TODO Auto-generated method stub
                                System.out.println("connection hashCode:"+connection.hashCode());
                        }
                });
                session1.save(s);
                //session.close();
                transaction.commit();
                
                Session session2 = sessionFactory.openSession();
                transaction = session2.beginTransaction();
                s= new Students(2,"李四","男",new Date(),"武当山");
                session2.doWork(new Work(){
 
                        @Override
                        public void execute(Connection connection) throws SQLException {
                                // TODO Auto-generated method stub
                                System.out.println("connection hashCode:"+connection.hashCode());
                        }
                });
                session2.save(s);
                transaction.commit();
        }
2、测试结果
connection hashCode:1048278180
Hibernate:
    insert
    into
        STUDENTS
        (SNAME, GENDER, BIRTHDAY, ADDRESS, SID)
    values
        (?, ?, ?, ?, ?)
connection hashCode:156943829
Hibernate:
    insert
    into
        STUDENTS
        (SNAME, GENDER, BIRTHDAY, ADDRESS, SID)
    values
        (?, ?, ?, ?, ?)
 
五 getCurrentSession不用关闭session
1、测试代码
        @Test
        public void testSaveStudentswithGetCurrentSession(){
                 // 创建配置对象  
            Configuration config = new Configuration().configure();
            // 创建服务注册对象
            ServiceRegistry serviceRegistery = new ServiceRegistryBuilder().applySettings(config.getProperties()).buildServiceRegistry();
            // 创建会话工厂对象
            SessionFactory sessionFactory = config.buildSessionFactory(serviceRegistery);
            // 创建会话对象
            Session session1 = sessionFactory.getCurrentSession();
            // 开启事务
            Transaction transaction = session1.beginTransaction();
                Students s= new Students(1,"张三丰","男",new Date(),"武当山");
                session1.doWork(new Work(){
                        @Override
                        public void execute(Connection connection) throws SQLException {
                                // TODO Auto-generated method stub
                                System.out.println("connection hashCode:"+connection.hashCode());
                        }
                });
                session1.save(s);
                transaction.commit();
                
                Session session2 = sessionFactory.getCurrentSession();
                transaction = session2.beginTransaction();
                s= new Students(2,"李四","男",new Date(),"武当山");
                session2.doWork(new Work(){
 
                        @Override
                        public void execute(Connection connection) throws SQLException {
                                // TODO Auto-generated method stub
                                System.out.println("connection hashCode:"+connection.hashCode());
                        }
                });
                session2.save(s);
                transaction.commit();
        }
2、运行结果
connection hashCode:455886737
Hibernate:
    insert
    into
        STUDENTS
        (SNAME, GENDER, BIRTHDAY, ADDRESS, SID)
    values
        (?, ?, ?, ?, ?)
connection hashCode:455886737
Hibernate:
    insert
    into
        STUDENTS
        (SNAME, GENDER, BIRTHDAY, ADDRESS, SID)
    values
        (?, ?, ?, ?, ?)

 

猜你喜欢

转载自cakin24.iteye.com/blog/2398840