Hibernate getCurrentSession()和openSession()的区别

  1. 通过getCurrentSession()创建的Session会绑定到当前线程上;openSession()不会。
  2. 通过getCurrentSession()获取Session,首先是从当前上下文中寻找旧的Session,如果没有,则创建新的Session;而openSession()是每次都创建新的Session。
  3. getCurrentSession()创建的Session在事物提交时自动关闭;openSession()创建的Session需要手动关闭。

     1     @Test
     2     public void testTeacherSave() {
     3
     4         Teacher t = new Teacher();
     5
     6         t.setName("t1");
     7         t.setTitle("middle");
     8         t.setBirthDate(new Date());
     9
    10         Session session = sessionFactory.openSession();
    11         //Session session = sessionFactory.getCurrentSession();
    12
    13         session.beginTransaction();
    14         session.save(t);
    15
    16         Session session2 = sessionFactory.getCurrentSession();
    17
    18         System.out.println(session == session2);
    19
    20         session.getTransaction().commit();
    21
    22         Session session3 = sessionFactory.getCurrentSession();
    23
    24         System.out.println(session == session3);
    25
    26     }
    

    输出:Hibernate:
        insert
        into
            Teacher
            (birthDate, gender, good, name, title)
        values
            (?, ?, ?, ?, ?)
    false
    false

     1 @Test
     2     public void testTeacherSave() {
     3
     4         Teacher t = new Teacher();
     5
     6         t.setName("t1");
     7         t.setTitle("middle");
     8         t.setBirthDate(new Date());
     9
    10         //Session session = sessionFactory.openSession();
    11         Session session = sessionFactory.getCurrentSession();
    12
    13         session.beginTransaction();
    14         session.save(t);
    15
    16         Session session2 = sessionFactory.getCurrentSession();
    17
    18         System.out.println(session == session2);
    19
    20         session.getTransaction().commit();
    21
    22         Session session3 = sessionFactory.getCurrentSession();
    23
    24         System.out.println(session == session3);
    25
    26     }

    输出:Hibernate:
        insert
        into
            Teacher
            (birthDate, gender, good, name, title)
        values
            (?, ?, ?, ?, ?)
    true
    false

时间: 2024-08-06 03:38:53

Hibernate getCurrentSession()和openSession()的区别的相关文章

hibernate 的SessionFactory的getCurrentSession 与 openSession() 的区别

1 getCurrentSession创建的session会和绑定到当前线程,而openSession不会. 2 getCurrentSession创建的线程会在事务回滚或事物提交后自动关闭,而openSession必须手动关闭 这里getCurrentSession本地事务(本地事务:jdbc)时 要在配置文件里进行如下设置 * 如果使用的是本地事务(jdbc事务) <property name="hibernate.current_session_context_class"

Hibernate中getCurrentSession()与openSession()的区别及应用

获取openSession和CurrentSession: session=HibernateSessionFactory.getSession(); session=HibernateSessionFactory.getSessionFactory().getCurrentSession(); 1.getCurrentSession()与openSession()的区别? * 采用getCurrentSession()创建的session会绑定到当前线程中,而采用openSession()创建

getCurrentSession 与 openSession() 的区别&lt;转&gt;

1 getCurrentSession创建的session会和绑定到当前线程,而openSession不会. 2 getCurrentSession创建的线程会在事务回滚或事物提交后自动关闭,而openSession必须手动关闭 这里getCurrentSession本地事务(本地事务:jdbc)时 要在配置文件里进行如下设置 * 如果使用的是本地事务(jdbc事务) <property name="hibernate.current_session_context_class"

Hibernate4之getCurrentSession和openSession

在一个应用程序中,如果DAO层使用Spring的hibernate模板,通过Spring来控制session的生命周期,则首选getCurrentSession 使用Hibernate的大多数应用程序需要某种形式的"上下文相关的"session,特定的session在整个特定的上下文范围内始终有效.然而,对不同类型的应用程序而言,要给为什么是组成这种"上下文"下一个定义通常是困难的:不同的上下文对"当前"这个概念定义了不同的范围.在3.0版本之前

getHibernateTemplate和getSession 区别, this.getHibernateTemplate().getSessionFactory().getCurrentSession()和OpenSession区别

SSH的项目中,使用getHibernateTemplate 与 getSession有什么的区别? 1.使用getSession()方法你只要继承 sessionFactory,而使用getHibernateTemplate()方法必须继承HibernateDaoSupport当然包括 sessionFactory,这点区别都不是特别重要的,下面这些区别就很重要了 2.getSession()方法是没有经过spring包装 的,spring会把最原始的session给你,在使用完之后必须自己调

getCurrentSession 与 openSession区别

getCurrentSession () 使用当前的session openSession()重新建立一个新的session 使用SessionFactory.getCurrentSession()需要在hibernate.cfg.xml中如下配置: * 如果采用jdbc独立引用程序配置如下: <property name="hibernate.current_session_context_class">thread</property> * 如果采用了JTA

hibernate get和load的区别

区别主要有三点: 1,load返回的是代理对象,等到真正使用对象的内容时才会发出sql语句. 2,get直接从数据库加载,不会延迟. 3,不存在对应记录时表现不一样. 无论是get还是load,都会首先查找缓存(一级缓存),如果没有才会去数据库查找.调用clear()方法可以清除session缓存,调用flush()方法可以强制进行从内存到数据库的同步. 具体分析如下: 在hibernate中我们知道如果要从数据库中得到一个对象,通常有两种方式,一种是通过session.get()方法,另一种就

hibernate中get和load区别

在日常开发中,获取数据时必不可少的,这样就要用到get和load方法来实现了.下面简单说一下get和load的区别. 1.返回值不同 使用get方法检索数据时,没有该数据返回值为null. 而使用load方法时,则返回org.hibernate.ObjectNotFoundException异常,如下: 2.检索机制不同 get方法和find方法都是直接从数据库中检索 而load方法的执行则比较复杂首先查找session的persistent Context中是否有缓存,如果有则直接返回 如果没

jdbc,mybatis,hibernate各自有优缺点以及区别

JDBC: 我们平时使用jdbc进行编程,大致需要下面几个步骤: 1,使用jdbc编程需要连接数据库,注册驱动和数据库信息 2,操作Connection,打开Statement对象 3,通过Statement对象执行SQL,返回结果到ResultSet对象 4,使用ResultSet读取数据,然后通过代码转化为具体的POJO对象 5,关闭数据库相关的资源 jdbc的缺点: 一:工作量比较大,需要连接,然后处理jdbc底层事务,处理数据类型,还需要操作Connection,Statement对象和