struts2事物处理方法

第一种:

<!--

设置事务管理器

-->

<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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值