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(应为自己测试)
BasicDataSource newDataSource = (BasicDataSource) Global._ctx
.getBean("datasource_"+hibernateEntityName);
newDataSource.getNumActive();
newDataSource.getInitialSize();
newDataSource.getMaxActive();
newDataSource.getMaxIdle();
newDataSource.getNumIdle();
分享到:
相关推荐
博文链接:https://shaqiang32.iteye.com/blog/201918
NULL 博文链接:https://bbxyhaihua.iteye.com/blog/505085
NULL 博文链接:https://zgdkik.iteye.com/blog/1835667
ssh整合中getCurrentSession()的使用,个人写的练习,备忘用,不建议下载
hibernate 学习笔记: 了解hibernate的基本概念 配置hbm.xml cfg.xml 快速入门案例3: 从domain-xml-数据库表 ...openSession()和getCurrentSession() 线程局部变量模式 transaction事务 在web项目中开发hibernate
123.在 hibernate 中 getCurrentSession 和 openSession 的区别是什么? 124.hibernate 实体类必须要有无参构造函数吗?为什么? 十三、Mybatis 125.mybatis 中 #{}和 ${}的区别是什么? 126.mybatis 有几种分页方式...
mysql,jdbc,Hibernate,tomcat [mysql+jdbc+jsp+Hibernate3.2+tomcattomcat5.028成功测试-简单] <br> 1 .数据库设计:库mydb 表events <br>EVENT_ID BIGINT(20) NOT NULL AUTOINC EVENT_DATE DATETIME...
9、openSession和getCurrentSession 90 10、拦截器的作用?拦截器和过滤器的区别? 91 11、struts.xml中result的type有哪些类型? 91 12、什么时候用JDBC什么时候用Hibernete; 91 13、hibernate 数据的三个状态 91 ...
Hibernate 修改数据 1.用 HQL方式来更新 在 这里修改 Person 的name 和age 通过 id 标识 Session currentSession = H3Utils.getCurrentSession(); currentSession.beginTransaction(); //创建 HQL String ...
Query对象不仅能查询数据,还可以绑定参数、限制查询记录数量、实现批量删除和批量更新等。 Configuration cfg=new Configuration().configure("hibernate.cfg.xml"); SessionFactory sf=cfg.buildSessionFactory();...
实现一个基于hibernate3的基本的dao,实现CRUD; 一行代码完成CRUD,提供多种重载方法详见IBaseDao,但是项目需要基于SSH框架。 ============ 使用方法,以用户添加为例: 1、定义User类,并提供get和set方法。 2、...
Assert.assertNotNull(sessionFactory.openSession()); } 2,配置声明式事务(使用基于注解的方式) 1,配置 <!-- 配置事务管理器 --> ...
3)增强修复S2SH技术栈,不再使用openSession语句,而是使用getSessionFactory().getCurrentSession(), Spring 3,Spring 4通用,同时web.xml里增加SpringOpenSessionInViewFilter ===============0.7.19.2==========...
不带参数的情况下Web.config中一定要有hibernate-configuration节 5.连接是单线程的,在整个httpRequest处理过程中是公用的,缓存在HttpContext context中 see ref NHibernateHelper.getSession... 6.增加log4net演示,...
学生在学习jdbc的时候,会问到怎么调用存储过程,现在将java调用oracle存储过程的示例总结如下
三大框架分页的实现: DAO里写法: //查出页面要显示的字段 -----分页代码 页面上的查询全部 ... <property name="newsclassService"> //这个名字要和action里定义的那个成员变量对应 </bean>