日期:2014-05-18  浏览次数:20593 次

spring事务管理
最近在看spring,当中有点不明白,
		<filter>
<filter-name>OpenSessionInViewFilter</filter-name>
<filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
</filter>

在web.xml中做了这样的配置,不是已经在每次请求的时候在过滤器开启session了吗?为什么在spring的application.xml中还需要配置事务管理,
	<tx:annotation-driven transaction-manager="transactionManager" />
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory">
<ref bean="sessionFactory"></ref>
</property>
</bean>
,两者什么关系呢?各自的作用是什么呢?
在线等答案?

------解决方案--------------------
首先:一般我们事务的控制是在service 层。
web.xml 的是用来打开view 层的session 取数据,并不是打开session,而是延迟session的关闭。
如:在dao层load 一张表的数据,在action,再取load 出来的这个对象的值的话就会报错(若是没有在web.xml中配OpenSessionInViewFilter);
第二:application.xml:
这是用来控制事务,如事务的开启和事务的提交。如:如果你向一张表里面插一条数据,如果你自己没控制事务,在spring中也没配事务,你会发现数据插入成功但是数据库里面却没有数据。
还有就是spring 帮我管理事务,我们自己编程的时候就不用考虑事务的管理了,方便编程。
很晚了,楼主,给分吧,我还在加班中呢!!!

------解决方案--------------------
OpenSessionInViewFilter是把sessionFactory绑定到 TransactionSynchronizationManager。它是把Hibernate的Session和一次完整的请求过程对应的线程相绑定。