有关spring事务回滚的问题

宝宝很6 2017-08-11 03:09:12
一个方法中多次与数据库交互,多个insert及update ,用的JdbcTemplate,如何实现当方法有异常时之前执行的多个insert和update语句进行回滚?跪求大神!!!!!
...全文
352 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
飞翔小Teemo 2017-08-16
  • 打赏
  • 举报
回复
如果你AOP,事务配置没有问题的话,在需要回滚的地方不要try catch 如果一定要捕捉的话 要在catch里抛出runtimeException异常
天涯若风 2017-08-14
  • 打赏
  • 举报
回复
看你的回滚机制了,你可以去找下,事物的传播机制。
Freefish1994 2017-08-14
  • 打赏
  • 举报
回复
引用 6 楼 qq_21995733 的回复:
insert也不能回滚,各种事务配置方式都用了,完全起不到效果啊
你得有异常才能回滚,你确定有异常吗?
宝宝很6 2017-08-11
  • 打赏
  • 举报
回复
insert也不能回滚,各种事务配置方式都用了,完全起不到效果啊
宝宝很6 2017-08-11
  • 打赏
  • 举报
回复
<?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:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd" default-lazy-init="true"> <description>Spring公共配置文件 </description> <!-- 定义受环境影响易变的变量 --> <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE" /> <property name="ignoreResourceNotFound" value="true" /> <property name="locations"> <list> <!-- 标准配置 --> <value>classpath*:/application.properties</value> </list> </property> </bean> <!-- 使用annotation 自动注册bean,并保证@Required,@Autowired的属性被注入 --> <context:component-scan base-package="com.ying" /> <!-- 数据源配置,使用应用内的DBCP数据库连接池 --> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <property name="driverClassName" value="${jdbc.driver}" /> <property name="url" value="${jdbc.url}" /> <property name="username" value="${jdbc.username}" /> <property name="password" value="${jdbc.password}" /> <property name="initialSize" value="1" /> <property name="maxActive" value="10" /> <property name="maxIdle" value="3" /> <property name="maxWait" value="1000" /> <property name="poolPreparedStatements" value="true" /> <property name="validationQuery" value="select count(*) from dual" /> <property name="testOnBorrow" value="true" /> <property name="testOnReturn" value="true" /> <property name="testWhileIdle" value="true" /> <property name="timeBetweenEvictionRunsMillis" value="1" /> </bean> <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"> <constructor-arg ref="dataSource" /> </bean> <!-- 配置事务管理器 --> <bean name="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"></property> </bean> <!-- 注解方式配置事物 --> <!-- <tx:annotation-driven transaction-manager="transactionManager" /> --> <!-- 拦截器方式配置事物 --> <tx:advice id="transactionAdvice" transaction-manager="transactionManager"> <tx:attributes> <tx:method name="add*" propagation="REQUIRED" /> <tx:method name="append*" propagation="REQUIRED" /> <tx:method name="save*" propagation="REQUIRED" /> <tx:method name="update*" propagation="REQUIRED" /> <tx:method name="insert*" propagation="REQUIRED" /> <tx:method name="edit*" propagation="REQUIRED" /> <tx:method name="delete*" propagation="REQUIRED" /> <tx:method name="remove*" propagation="REQUIRED" /> <tx:method name="init" propagation="REQUIRED" /> <tx:method name="delAndInit" propagation="REQUIRED" /> <tx:method name="get*" propagation="REQUIRED" read-only="true" /> <tx:method name="find*" propagation="REQUIRED" read-only="true" /> <tx:method name="load*" propagation="REQUIRED" read-only="true" /> <tx:method name="search*" propagation="REQUIRED" read-only="true" /> <tx:method name="datagrid*" propagation="REQUIRED" read-only="true" /> <tx:method name="*" propagation="REQUIRED" /> </tx:attributes> </tx:advice> <aop:config> <aop:pointcut id="transactionPointcut" expression="execution(* com.ying.dao.*.*(..))" /> <aop:advisor pointcut-ref="transactionPointcut" advice-ref="transactionAdvice" /> </aop:config> <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver" p:defaultEncoding="UTF-8"> <property name="maxUploadSize"> <value>104857600</value> </property> <property name="maxInMemorySize"> <value>4096</value> </property> </bean> </beans>
宝宝很6 2017-08-11
  • 打赏
  • 举报
回复
引用 2 楼 qq_27762917 的回复:
把@Transactional(rollbackFor = Exception.class)加到你的方法上面
注解啥的aop都试过了,insert可以回滚,但是就这个方法进行的update不会回滚。。。getJt().update这个方法
9524_ 2017-08-11
  • 打赏
  • 举报
回复
兄弟 你可以学习下spring aop
Freefish1994 2017-08-11
  • 打赏
  • 举报
回复
把@Transactional(rollbackFor = Exception.class)加到你的方法上面
李德胜1995 2017-08-11
  • 打赏
  • 举报
回复
使用@Transactional或者配置<aop:config>都可以。。。。。

62,628

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧