关于Spring事务配置管理 Spring的事务配置,在service中发生异常后,整个方法内的数据库操作全部回滚,现在我想当方法内的更新或者删除语句返回值为0的时候也要全部回滚,spring有这样的配置吗?(除了判断返回值手动抛异常)Spring数据库事务 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 可以自己实现函数,控制commit或者非commit;至于spring有么有,我不知道了。 谢谢!网上我也没查到spring相关的配置。 你看这个能帮到你么 用的spring的txAdvice和aopaop中配置事务作用路径,需要处理事务的方法名后加 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切面编程也还是没有找到具体的做法。明天继续找。 jstack打印信息问题 这是怎么回事呢,跪求解决,不管你是新手还是大神都来看看吧 下面sql如何改为hql,谢谢! struts2接受参数乱码 快毕业了,想做java开发,各位给点意见啊? 有会用easyjweb的朋友吗? 关于Hibernate的讨论,,大虾们快来 Exception in thread "main" java.lang.NullPointerException 关于jsp网页实现打印的问题 关于struts的设计问题 关于MySQL工资查询语句 Action给邮箱发送链接是个设置密码界面三小时后链接失效 怎么获取点击链接时间
至于spring有么有,我不知道了。
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">