spring 的配置:
扫描配置:
<context:component-scan base-package="com.karting"> <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/> </context:component-scan>配置数据库源:
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" p:url="${jdbc.url}" p:username="${jdbc.username}" p:password="${jdbc.password}" p:maxActive="${jdbc.maxActive}" p:initialSize="${jdbc.initialSize}" p:minEvictableIdleTimeMillis="${jdbc.minEvictableIdleTimeMillis}" p:testOnBorrow="${jdbc.testOnBorrow}" p:testWhileIdle="${jdbc.testWhileIdle}" p:validationQuery="${jdbc.validationQuery}" p:removeAbandoned="${jdbc.removeAbandoned}" p:removeAbandonedTimeout="${jdbc.removeAbandonedTimeout}" p:logAbandoned="${jdbc.logAbandoned}" p:poolPreparedStatements="${jdbc.poolPreparedStatements}" p:maxOpenPreparedStatements="${jdbc.maxOpenPreparedStatements}"> </bean>
配置注解事务:
<!-- the PlatformTransactionManager --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"/> </bean> <tx:annotation-driven transaction-manager="transactionManager"/>springMVC 的配置(只扫描Controller层):
<context:component-scan base-package="com.karting.controller"/>
Service的调用(Service类上有注解@Transactional):
@Transactional @Service public class AppointmentService {
@Transactional(readOnly = false, propagation = Propagation.REQUIRES_NEW) public void testTransactional() throws Exception{ Appointment appointment = new Appointment(); appointment.setId(1); appointment.setUpdated_by("test"); appointMentDAO.insert(appointment); throw new Exception("测试事务回滚"); }} 抛出错误后还是插入成功,没有回滚。求各位大神帮忙看下哪里有问题。
我已经排除了:
快崩溃了。。。。
你定义一下回滚规则呢,不对再看看######回复 @年少爱追梦 : 不行啊,大神,我完全都照抄了,一个字一个字审查了,还是不行。不论是注解还是声明式######@蒂法娜 http://www.oschina.net/question/2008155_220656,看一下这篇,不行的话,我明天休息的时候也试试######试过了,不行######Spring事务回滚只接受RuntimeException
######我试过了切面式的配置,试过了RuntimeException######你手动抛出的异常代码放到dao层insert方法试试######不行,哭######确认数据库引擎,然后确认事务的配置是否正确######嗯,已经确认了数据库引擎,我用的数据库式mariaDB, 引擎是Innodb######
看下log,看看Connection打开后注册没注册事务,确定下是事务没配上还是没回滚
或者配下rollbackFor Exception试试
######回复 @贱起苍澜 : 这样啊######回复 @蒂法娜 : 你自己封装的jdbc是不是还要自己封装事务啊,我没自己封装过。。要么你等等别的大神回复######回复 @贱起苍澜 : 还是不行。setAutoCommit false,update,操作,insert操作错误,最后还是setAutoCommit true。######回复 @贱起苍澜 : 我这就试试看######回复 @蒂法娜 : 或者你可以试试不用注解声明事务,用拦截器式事务看看能不能回滚######求真相!######整个项目看看?######以前也遇到,后来把异常定位到顶级异常类型就解决了,你可以试试:
<tx:method name="add*" propagation="REQUIRED" read-only="false" rollback-for="java.lang.Throwable"/>
######好想还是不行######<2016-05-12 13:15:24> <DEBUG> [druid.sql.Connection] - {conn-10010} pool-connect at org.mariadb.jdbc.MariaDbServerPreparedStatement.execute(MariaDbServerPreparedStatement.java:374)
at org.mariadb.jdbc.MariaDbServerPreparedStatement.executeUpdate(MariaDbServerPreparedStatement.java:347)
at com.alibaba.druid.filter.FilterChainImpl.preparedStatement_executeUpdate(FilterChainImpl.java:2723)
at com.alibaba.druid.filter.FilterAdapter.preparedStatement_executeUpdate(FilterAdapter.java:1069)
at com.alibaba.druid.filter.FilterEventAdapter.preparedStatement_executeUpdate(FilterEventAdapter.java:491)
at com.alibaba.druid.filter.FilterChainImpl.preparedStatement_executeUpdate(FilterChainImpl.java:2721)
at com.alibaba.druid.filter.FilterAdapter.preparedStatement_executeUpdate(FilterAdapter.java:1069)
at com.alibaba.druid.filter.FilterEventAdapter.preparedStatement_executeUpdate(FilterEventAdapter.java:491)
at com.alibaba.druid.filter.FilterChainImpl.preparedStatement_executeUpdate(FilterChainImpl.java:2721)
at com.alibaba.druid.filter.FilterAdapter.preparedStatement_executeUpdate(FilterAdapter.java:1069)
at com.alibaba.druid.filter.FilterChainImpl.preparedStatement_executeUpdate(FilterChainImpl.java:2721)
at com.alibaba.druid.proxy.jdbc.PreparedStatementProxyImpl.executeUpdate(PreparedStatementProxyImpl.java:158)
at com.alibaba.druid.pool.DruidPooledPreparedStatement.executeUpdate(DruidPooledPreparedStatement.java:253)
<2016-05-12 13:15:24> <DEBUG> [druid.sql.Statement] - {conn-10009, pstmt-20019} clearParameters.
<2016-05-12 13:15:24> <DEBUG> [druid.sql.Connection] - {conn-10009} pool-recycle
<2016-05-12 13:15:24> <DEBUG> [druid.sql.Connection] - {conn-10010} setAutoCommit true
<2016-05-12 13:15:25> <DEBUG> [druid.sql.Connection] - {conn-10010} pool-recycle
<tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true"/>
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。