Hibernate各类概念-openSession和getCurrentSession的区别

Hibernate有两种方式获得session,分别是: 
openSession和getCurrentSession 
他们的区别在于 
1. 获取的是否是同一个session对象 
openSession每次都会得到一个新的Session对象 
getCurrentSession在同一个线程中,每次都是获取相同的Session对象,但是在不同的线程中获取的是不同的Session对象 
2. 事务提交的必要性 
openSession只有在增加,删除,修改的时候需要事务,查询时不需要的 
getCurrentSession是所有操作都必须放在事务中进行,并且提交事务后,session就自动关闭,不能够再进行关闭 

  • OpenSession每次都会得到一个新的Session对象

    OpenSession每次都会得到一个新的Session对象,所以System.out.println(s1==s2);会打印false

    import org.hibernate.Session;
    import org.hibernate.SessionFactory;
    import org.hibernate.cfg.Configuration;
      
    public class TestHibernate {
        public static void main(String[] args) {
            SessionFactory sf = new Configuration().configure().buildSessionFactory();
      
            Session s1 = sf.openSession();
            Session s2 = sf.openSession();
             
            System.out.println(s1==s2);
             
            s1.close();
            s2.close();
            sf.close();
        }
    }
  • 相同线程的getCurrentSession

    如果是同一个线程(本例是在主线程里),每次获取的都是相同的Session

    import org.hibernate.Session;
    import org.hibernate.SessionFactory;
    import org.hibernate.cfg.Configuration;
     
    public class TestHibernate {
        public static void main(String[] args) {
            SessionFactory sf = new Configuration().configure().buildSessionFactory();
     
            Session s1 = sf.getCurrentSession();
            Session s2 = sf.getCurrentSession();
     
            System.out.println(s1 == s2);
     
            s1.close();
    //      s2.close();
            sf.close();
        }
    }
  • 不同线程的getCurrentSession

    如果是不同线程,每次获取的都是不同的Session


    import org.hibernate.Session;
    import org.hibernate.SessionFactory;
    import org.hibernate.cfg.Configuration;
     
    public class TestHibernate {
        static Session s1;
        static Session s2;
     
        public static void main(String[] args) throws InterruptedException {
     
            final SessionFactory sf = new Configuration().configure().buildSessionFactory();
     
            Thread t1 = new Thread() {
                public void run() {
                    s1 = sf.getCurrentSession();
                }
            };
            t1.start();
     
            Thread t2 = new Thread() {
                public void run() {
                    s2 = sf.getCurrentSession();
                }
            };
            t2.start();
            t1.join();
            t2.join();
     
            System.out.println(s1 == s2);
        }
     
    }
  • openSession查询时候不需要事务

    如果是做增加,修改,删除是必须放在事务里进行的。 但是如果是查询或者get,那么对于openSession而言就不需要放在事务中进行
    import org.hibernate.Session;
    import org.hibernate.SessionFactory;
    import org.hibernate.cfg.Configuration;
     
    import com.how2java.pojo.Product;
     
    public class TestHibernate {
        static Session s1;
        static Session s2;
     
        public static void main(String[] args) throws InterruptedException {
            SessionFactory sf = new Configuration().configure().buildSessionFactory();
     
            Session s1 = sf.openSession();
     
            s1.get(Product.class, 5);
     
            s1.close();
     
            sf.close();
        }
    }
  • getCurrentSession所有操作都必须放在事务中

    对于getCurrentSession而言所有操作都必须放在事务中,甚至于查询和get都需要事务。
    16行的get没有放在事务中,就会导致异常产生

    import org.hibernate.Session;
    import org.hibernate.SessionFactory;
    import org.hibernate.cfg.Configuration;
     
    import com.how2java.pojo.Product;
     
    public class TestHibernate {
     
        public static void main(String[] args) throws InterruptedException {
            SessionFactory sf = new Configuration().configure().buildSessionFactory();
     
            Session s1 = sf.getCurrentSession();
     
            s1.get(Product.class, 5);
     
            s1.close();
     
            sf.close();
        }
     
    }
  • getCurrentSession在提交事务后,session自动关闭

    22行,在事务关闭后,试图关闭session,就会报session已经关闭的异常

  • import org.hibernate.Session;
    import org.hibernate.SessionFactory;
    import org.hibernate.cfg.Configuration;
     
    import com.how2java.pojo.Product;
     
    public class TestHibernate {
     
        public static void main(String[] args) throws InterruptedException {
            SessionFactory sf = new Configuration().configure().buildSessionFactory();
     
            Session s1 = sf.getCurrentSession();
     
            s1.beginTransaction();
     
            s1.get(Product.class, 5);
     
            s1.getTransaction().commit();
             
            s1.close();
     
            sf.close();
        }
     
    }



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值