在Hibernate中,session建立时如getCurrentSession();不会获取数据库的链接,只有在beginTransaction()时才会从链接池里得到链接,默认情况下当事务commit或者rollback时session会flush,随后链接会被释放,新的事务会得到新的链接。
在使用spring整合hibernate时,一般采用代理模式来管理 事务,所以在 dao里,不需要我们手动的去获取 transaction和 commit事务。例如下面的代码所示。
<bean id="txManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory">
<ref local="sessionFactory" /> </property>
</bean>
<bean id="transactionInterceptor"
class="org.springframework.transaction.interceptor.TransactionInterceptor">
<property name="transactionManager">
<ref bean="txManager" />
</property>
<property name="transactionAttributes">
<props>
<prop key="search*">PROPAGATION_REQUIRED,readOnly</prop>
<prop key="get*">PROPAGATION_REQUIRED,readOnly</prop>
<prop key="save*">PROPAGATION_REQUIRED,-Exception</prop>
<prop key="add*">PROPAGATION_REQUIRED,-Exception</prop>
<prop key="*">PROPAGATION_REQUIRED</prop>
</props>
</property>
</bean>
<!-- BeanNameAutoProxyCreator -->
<bean id="autoProxyCreator"
class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
<property name="interceptorNames">
<list>
<value>logInterceptor</value>
<value>transactionInterceptor</value>
</list>
</property>
<!-- Add all the services to here for transaction and rollback -->
<property name="beanNames">
<list>
<!-- put in service interfaces here for transaction management -->
<idref bean="registrationService" />
</list>
</property>
</bean>
以前我一直有疑问,因为在dao里执行完某个查询操作后,session就会被关闭。这样得到的就是个detached对象,很多情况下就应该有LazyInitializationException。我一直都不明白开发架构是如何解决这个问题的。
《java persistence with hibernate》的16章有相关的描述,一种解决方法是Open Session in View (OSIV),这种比较好理解,就是利用filter扩展整个session,"leave the persistence context open until the view is completely rendered."。它适用于“the presentation,business,and persistence layers are colocated on the same virtual machine.” 我们所要做的就是在web.xml加上
<filter>
<filter-name>sessionFilter</filter-name>
<filter-class>
org.springframework.orm.hibernate3.support.OpenSessionInViewFilter
</filter-class>
</filter>
<filter-mapping>
<filter-name>sessionFilter</filter-name>
<url-pattern>*.faces</url-pattern>
</filter-mapping>
如果是分布式系统里OSIV就不行了,只得用其他的解决方法,如session facade, DTO(data transfer object) and command。
分享到:
相关推荐
可以对hibernate的 Session 和 Transaction 有更深入的认识 希望对一些概念比较模糊的人 有帮助
NULL 博文链接:https://stewchicken.iteye.com/blog/938875
Transaction tx1 = session1.beginTransaction(); // 先加载一个持久化对象 Customer customer = (Customer)session.get(Customer.class, new Long(1)); session.delete(customer); // 计划执行一个delete语句
Hibernate的核心接口一共有6个,分别为:Session、SessionFactory、Transaction、Query、Criteria和Configuration。这6个核心接口在任何开发中都会用到。通过这些接口,不仅可以对持久化对象进行存取,还能够进行事务...
hibernate概述,hibernate入门Demo,hibernate配置文件详解(全局配置,实体类映射配置),配置实体规则,核心API详解(Configuration,sessionFactory,session,Transaction),hibernate中的对象状态以及刷新能缓存机制 ...
#hibernate.transaction.auto_close_session ## the Transaction API abstracts application code from the underlying JTA or JDBC transactions #hibernate.transaction.factory_class org.hibernate....
Hibernate Web应用的开发一般经过以下几个步骤: (1)创建数据库。 (2)将Hibernate所需的JAR包复制到WEB-...(8)通过创建的Transaction实例进行事务管理。 (9)通过创建的Query或Criteria实例实现数据库的查询。
Hibernate是一个开放源代码的对象关系映射框架,它对JDBC进行了非常轻量级的对象封装,使得Java...Hibernate的核心接口一共有6个,分别为:Session、SessionFactory、Transaction、Query、Criteria和Configuration。
Session对象.beginTransaction().begin(); //开始一个事务 4.用Session对象的以下方法操作数据 (1).save(Object obj); (2).saveOrUpdate(Object obj); (3).delete(Object obj); //只要删除的对象设置了主键的值...
session.beginTransaction(); Teacher t = (Teacher) session.get(Teacher.class, 3); t.setName("yangtb2"); session.update(t); session.getTransaction().commit(); } } ``` 然而,Hibernate 生成的 SQL ...
import org.hibernate.Transaction; public class OneToOneTest extends TestCase { public void testSave1(){ Session session = null; Transaction ta = null; try{ session = HibernateUtil.getSession(); ...
SessionSession接口接口 TransactionTransaction接口接口 QueryQuery等接口等接口 熟练使用熟练使用HibernateHibernate的各个接口的各个接口 使用使用HiberntaeHiberntae的的HQLHQL语言进行数据库访问语言进行数据库...
8. session.beginTransaction(); 9. Query query = session.createQuery("from User"); 10. query.setFirstResult(0);//从第一条记录开始 11. query.setMaxResults(4);//取出四条记录 12. List userList = query...
hibernate就会抛出: No Hibernate Session bound to thread, and configuration does not allow creation of one here}异常。 在实际的SSH web应用开发中,我们通常用spring来进行事务的管理。我们一般不会在dao层...
The book keeps its focus on Hibernate without wasting time on nonessential third-party tools, so you’ll be able to immediately start building transaction-based engines and applications. Experienced ...
有效的Session管理:Spring提供了有效、简单、安全的Hibernate Session处理。 IoC容器降低了DAO组件与业务逻辑层之间的耦合性。 DAO模式的使用,降低了系统重构的代价。 方便的事务管理:Spring提供的声明式事务处理...
11.1. Session和事务范围(transaction scope) 11.1.1. 操作单元(Unit of work) 11.1.2. 长对话 11.1.3. 关注对象标识(Considering object identity) 11.1.4. 常见问题 11.2. 数据库事务声明 11.2.1. 非托管...
本资源介绍了Hibernate组织架构,工作原理,配置方法、开发步骤,以及Configuration、SessionFactory、Session、Query、Transaction在Hibernate中的主要作用和使用方法,并资源以一个具体的例子贯穿所有内容;...
Hibernate的核心接口一共有5个,分别为:Session、SessionFactory、Transaction、Query和Configuration。
session.beginTransaction(); session.save(student); session.getTransaction().commit(); session.close(); factory.close(); } } 但运行上面的测试类之后,我们查看一下数据库中student表,表中将会多...