您現在的位置是:首頁 >精選問答 > 2023-10-28 11:24:18 來源:
spring事務傳播機制(spring事務)
大家好,我是小夏,我來為大家解答以上問題。spring事務傳播機制,spring事務很多人還不知道,現在讓我們一起來看看吧!
在HIBERNATE中,如果我們要訪問到某個類的集合屬性,那我們一般在類的的映射文件里SET節點啟用LAZE=FALSE;
那么在S2SH中怎么解決呢?
1.OpenSessionInViewFilter是Spring提供的一個針對Hibernate的一個支持類,其主要意思是在發起一個頁面請求時打開Hibernate的Session,
一直保持這個Session,直到這個請求結束,具體是通過一個Filter來實現的。
2.由于Hibernate引入了Lazy Load特性,使得脫離Hibernate的Session周期的對象如果再想通過getter方法取到其關聯對象的值,
Hibernate會拋出一個LazyLoad的Exception。所以為了解決這個問題,Spring引入了這個Filter,使得Hibernate的Session的生命周期變長。
解決方法:
修改WEB.XML !!!
在WEB.XML中加入OpenSessionInView 的FILETER就可以了
具體如下:
<filter>
<filter-name>openSessionInViewFilter</filter-name>
<filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>openSessionInViewFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
PS:老郭給你們的忠告!
1.在加入這個FILETER的時候,一定一定,千萬千萬記得放在STRUTS2的核心控制器的位置的上面!切記!
2.如果你的SPRING的配置文件中SESSIONFACTORY的BEAN ID默認不是sessionFactory的話,你的openSessionInView要改成這樣.
<filter>
<filter-name>openSessionInViewFilter</filter-name>
<filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
<init-param>
<param-name>sessionFactoryBeanName</param-name>
<param-value>xxxx</param-value> //也就是你的SPRING配置文件中SESSIONFACTORY的名字
</init-param>
</filter>
<filter-mapping>
<filter-name>openSessionInViewFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
3.當你把OPENSESSIONINVIEW配置上后,你就可以讓SESSION晚點關閉的.但是又一個新的問題產生了.什么問題呢?自己可以試試,如果這個時候你的SPRING
沒有配置事務的話,那么你所有的更新操作都是不允許的.
所以要在SPRING里把需要更新的SERVICE操作都配置事務.
配置如下:
首先在SPRING配置文件中加入XML命名空間:
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
方案地址:
xsi:schemaLocation里加上
http://www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx-2.0.xsd
http://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop-2.0.xsd"
前提是你配置了AOP的命名空間和方案地址
<!-- 事務配置 -->
<!--Step1.事務管理器配置-->
<bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<!--Step2.交給SPRING事務管理的一些方法.-->
<aop:config>
<aop:pointcut id="productServiceMethods"
<!-- 指定切面是哪個范圍類,比如你項目的SERVICE的所有方法-->
expression="execution(* com.公司名.項目名.service..*.*(..))" />
<!-- 一個通知的集合,這個集合都用上的POINTCUT-->
<aop:advisor advice-ref="txAdvice"
pointcut-ref="productServiceMethods" />
</aop:config>
<!--定義通知集合,以TX開頭,是有事務的 -->
<tx:advice id="txAdvice" transaction-manager="txManager">
<tx:attributes>
<!--所有的以ADD開頭的方法名的方法都用事務 REQUIRED 表示,比如方法A有更新操作,A中調用了B方法,那么B到底是重新起一個事務還是用A方法的事務,
REQUIRED標識不起就用當前事務,如果有就用A的,如果沒有就起一個-->
<tx:method name="add*" propagation="REQUIRED" />
<tx:method name="delete*" propagation="REQUIRED" />
<tx:method name="update*" propagation="REQUIRED" />
<!--加入只讀,說明以GET或者LOAD開頭的方法名的方法都是只讀事務,說明這個方法內沒有UPDATE操作,這樣聲明下可以提高該查詢方法的效率 -->
<tx:method name="get*" propagation="REQUIRED"
read-only="true" />
<tx:method name="load*" propagation="REQUIRED"
read-only="true" />
</tx:attributes>
</tx:advice>
本文到此講解完畢了,希望對大家有幫助。