struts2事物处理方法

来源:互联网 发布:淘宝卖家注册账号 编辑:程序博客网 时间:2024/04/29 10:52

第一种:

<!--

设置事务管理器

-->

<bean id="TBHibernateTxManager"

class="org.springframework.orm.hibernate3.HibernateTransactionManager">

<property name="sessionFactory" ref="TBHibernateSessionFactory" />

</bean>

<!--

事务拦截器

-->

<bean id="TBHibernateTxInterceptor"

class="org.springframework.transaction.interceptor.TransactionInterceptor">

<property name="transactionManager">

<ref bean="TBHibernateTxManager" />

</property>

<!--

配置事务属性

-->

<property name="transactionAttributes">

<props>

<prop key="add*">PROPAGATION_REQUIRED,-Exception</prop>

<prop key="insert*">PROPAGATION_REQUIRED,-Exception</prop>

<prop key="save*">PROPAGATION_REQUIRED,-Exception</prop>

<prop key="update*">PROPAGATION_REQUIRED,-Exception</prop>

<prop key="modify*">PROPAGATION_REQUIRED,-Exception</prop>

<prop key="delete*">PROPAGATION_REQUIRED,-Exception</prop>

<prop key="del*">PROPAGATION_REQUIRED,-Exception</prop>

<prop key="do*">PROPAGATION_REQUIRED,-Exception</prop>

<prop key="test*">PROPAGATION_REQUIRED</prop>

<prop key="find*">PROPAGATION_REQUIRED,readOnly</prop>

<prop key="auto*">PROPAGATION_REQUIRED,-Exception</prop>

<prop key="*">PROPAGATION_REQUIRED,-Exception</prop>

</props>

</property>

</bean>

<!--

自动代理

-->

<bean id="TBHibernateTxAutoproxy"

class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">

<property name="beanNames">

<list>

<value>*Service</value>

<value>*BIZ</value>

<value>*Biz</value>

</list>

</property>

<property name="interceptorNames">

<list>

<value>TBHibernateTxInterceptor</value>

</list>

</property>

</bean>

这个比较简单一点

<list>

<value>*Service</value>

<value>*BIZ</value>

<value>*Biz</value>

</list>

这些是指你要做事务拦截的业务类的名字

以什么结尾。

第二种:

<!--

配置事务管理器

-->

<bean id="transactionManager"

class="org.springframework.orm.hibernate3.HibernateTransactionManager">

<property name="sessionFactory">

<ref bean="sessionFactory" />

</property>

</bean>

<!--

配置事务的传播特性

-->

<tx:advice id="txAdvice" transaction-manager="transactionManager">

<tx:attributes>

<tx:method name="add*" propagation="REQUIRED"

rollback-for="Exception"/>

<tx:method name="save*" propagation="REQUIRED"

rollback-for="Exception"/>

<tx:method name="del*" propagation="REQUIRED"

rollback-for="Exception"/>

<tx:method name="edit*" propagation="REQUIRED"

rollback-for="Exception"/>

<tx:method name="send*" propagation="REQUIRED"

rollback-for="Exception"/>

<tx:method name="*" read-only="true" rollback-for="Exception"/>

</tx:attributes>

</tx:advice>

<!--

配置哪些类的哪些方法进行事务管理

-->

<aop:config>

<aop:pointcut id="allManagerMethod" expression="execution(* com.lu.biz.impl.*.*(..))" />

<aop:advisor pointcut-ref="allManagerMethod" advice-ref="txAdvice" />

</aop:config>

第三种:

<bean id="txManager"

class="org.springframework.orm.hibernate3.HibernateTransactionManager">

<property name="sessionFactory" ref="sessionFactory" />

</bean>

<tx:annotation-driven transaction-manager="txManager" />

以上三种方式

第一种、第二种在实现事物方法上面不需要加注释

@Transactional

,因为在配置中已经配置

上哪些方法需要事物处理。

而第三种配置需要在方法上面加上:

@Transactional

在这里提醒一下大家,事物方法发生异常情况时启动事物回滚。

--

0 0