Hibernate openSession() 和 getCurrentSession的区别 .

Hibernate openSession() 和 getCurrentSession的区别

 

getHiberanteTemplate 、getCurrentSession和OpenSession
采用getCurrentSession()创建的Session会绑定到当前的线程中去、而采用OpenSession()则不会。

采用getCurrentSession()创建的Session在commit或rollback后会自动关闭,采用OpenSession()必须手动关闭。

采用getCurrentSession()需要在Hibernate.cfg.xml配置文件中加入如下配置:

如果是本地事物,及JDBC一个数据库:

<propety name=”Hibernate.current_session_context_class”>thread</propety>

如果是全局事物,及jta事物、多个数据库资源或事物资源:

<propety name=”Hibernate.current_session_context_class”>jta</propety>

使用spring的getHiberanteTemplate 就不需要考虑事务管理和session关闭的问题:

public List getEntityCriteria(final DetachedCriteria detachedCriteria) {
        return (List) getHibernateTemplate().executeFind(
                new HibernateCallback() {
                    public Object doInHibernate(Session session)
                            throws HibernateException {
                        Criteria criteria = detachedCriteria
                                .getExecutableCriteria(session);
                        return criteria.list();
                    }
                });
    }
    public List getEntityCriteriaByPage(final DetachedCriteria detachedCriteria,final Page page) {
        return (List) getHibernateTemplate().executeFind(
                new HibernateCallback() {
                    public Object doInHibernate(Session session)
                            throws HibernateException {
                        Criteria criteria = detachedCriteria
                                .getExecutableCriteria(session);

                        criteria.setFirstResult(page.getFirstItemPos());
                        criteria.setMaxResults(page.getPageSize());
                        return criteria.list();
                    }
                });
    }

DetachedCriteria detachedCriteria = DetachedCriteria.forClass(Department.class);

detachedCriteria.add(Restrictions.eq("eid", seacher.getEid()));//企业id为条件查询

detachedCriteria.add(Restrictions.in("dprtid", ids));//根据id数组查询部门

detachedCriteria.add(Restrictions.like("name", "%" + seacher.getName()+ "%"));//名称模糊查询

getEntityCriteriaByPage(detachedCriteria,pageinfo);

经过检查激活连接为0,被使用的连接总是为1(应为自己测试)

package com.myj.hibernate.model; 



  import org.hibernate.HibernateException; 



  import org.hibernate.SessionFactory; 



  import org.hibernate.cfg.AnnotationConfiguration; 



  import org.hibernate.classic.Session; 



  import org.junit.AfterClass; 



  import org.junit.BeforeClass; 



  import org.junit.Test; 



  public class TeacherTest { 



  public static SessionFactory sessionFactory = null; 



  @BeforeClass 



  public static void beforeClass() { 



  try { 



  sessionFactory = new AnnotationConfiguration().configure().buildSession Factory(); 



  } catch (HibernateException e) { 



  e.printStackTrace(); 



  } 



  } 



  @Test 



  public void testTeacherSave() { 



  Teacher teacher = new Teacher(); 



  teacher.setName("martian"); 



  teacher.setAge(23); 



  Session session = sessionFactory.getCurrentSession(); 



  session.beginTransaction(); 



  session.save(teacher); 



  //测试getCurrentSession()方法,这里输出true,因为在一个事务内,所以取得线程中的session 



  Session session1 = sessionFactory.getCurrentSession(); 



  System.out.println(session == session1); 



  //commit()之后不用close(),假如使用sessionFactory.openSession();就需要close(); 



  session.getTransaction().commit(); 



  //测试getCurrentSession()方法,这输出false,上一个事务已经提交,这里将重新生成一个session 



  Session session2 = sessionFactory.getCurrentSession(); 



  System.out.println(session == session2); 



  } 



  @AfterClass 



  public static void afterClass() { 



  sessionFactory.close(); 



  } 



  } 

 

你可能感兴趣的:(Hibernate)