当前位置:编程学习 > JAVA >>

关于Spring事务配置管理

Spring的事务配置,在service中发生异常后,整个方法内的数据库操作全部回滚,现在我想当方法内的更新或者删除语句返回值为0的时候也要全部回滚,spring有这样的配置吗?(除了判断返回值手动抛异常) Spring 数据库 事务 --------------------编程问答-------------------- ?没明白  更新和删除值为0不就代表没影响到吗  还要回滚什么- - --------------------编程问答-------------------- 对,现在是项目的需求呗,有可以传的参数不对或者怎样导致更新或者删除条数为0,但是不抛异常,这种情况他也想整个操作回滚。 --------------------编程问答-------------------- 可以自己实现函数,控制commit或者非commit;
至于spring有么有,我不知道了。 --------------------编程问答-------------------- 谢谢!网上我也没查到spring相关的配置。 --------------------编程问答-------------------- 你看这个能帮到你么 用的spring的txAdvice和aop
aop中配置事务作用路径,需要处理事务的方法名后加 TX 代表此方法是进行事务的。

<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory">
<ref local="sessionFactory" />
</property>
</bean>
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<!-- all methods starting with 'get' are read-only -->
<tx:method name="*TX" propagation="REQUIRED" />
<!-- other methods use the default transaction settings (see below) -->
<tx:method name="*" read-only="true" />
</tx:attributes>
</tx:advice>
<aop:config>
<aop:pointcut id="transactioncut"
expression="execution(* *..*BPO.*(..))" />
<aop:advisor advice-ref="txAdvice"
pointcut-ref="transactioncut" />
</aop:config>
<bean id="daoSupport"
class="路径"
abstract="true">
<property name="sessionFactory">
<ref local="sessionFactory" />
</property>
<property name="ds">
<ref local="dataSource" />
</property>
</bean>
--------------------编程问答-------------------- 不能编辑- -  配置文件开头还要引用一下


<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
           http://www.springframework.org/schema/tx
      http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
      http://www.springframework.org/schema/aop 
      http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
--------------------编程问答-------------------- 非常感谢,我也是这么配置的,但是这只是对service方法发生异常时管用,但是更新删除0条时不发生异常就没法控制回滚了。 --------------------编程问答-------------------- 其实我还是不明白  你既然返回0了  就表示已经commit了 又怎么回滚呢   --------------------编程问答-------------------- 一个service方法内会调很多个Dao方法去操作数据库,如果没有发生异常则事务提交,如果方法内发生异常抛出去了整个方法的数据库操作就全部回滚,现在是想中间弄个更新或者删除Dao方法返回0,也要整个方法回滚。 --------------------编程问答-------------------- 你先按照删除和修改的条件查询一下   如果返回的list大小为0 就不执行  如果不为0再全部执行事务 --------------------编程问答-------------------- 要不然 你执行增删改 有返回值那肯定是已经commit了  不能回滚了   我是这么想的 呵 --------------------编程问答-------------------- 果然没有这种配置吗? --------------------编程问答-------------------- aop编程! --------------------编程问答-------------------- 只要方法名和配置文件里面对应上就可以了哇 --------------------编程问答-------------------- 你看看aop切面编程,讲的就是这方面的! --------------------编程问答-------------------- 谢谢,正在查看中。 --------------------编程问答-------------------- 谢谢大家的回答,用aop切面编程也还是没有找到具体的做法。明天继续找。
补充:Java ,  Java EE
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,