spring durid 事务无论怎么配置都是失败!快崩溃了。。。

蒂法娜 发布于 2016/05/11 13:25
阅读 992
收藏 0

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("测试事务回滚");
}
}
抛出错误后还是插入成功,没有回滚。求各位大神帮忙看下哪里有问题。


我已经排除了:


  1. MariaDB 的数据库引擎情况
  2. 排除了springMVC配置扫描冲突的情况

快崩溃了。。。。


加载中
0
年少爱追梦
年少爱追梦
你定义一下回滚规则呢,不对再看看
蒂法娜
蒂法娜
回复 @年少爱追梦 : 不行啊,大神,我完全都照抄了,一个字一个字审查了,还是不行。不论是注解还是声明式
年少爱追梦
年少爱追梦
@蒂法娜 http://www.oschina.net/question/2008155_220656,看一下这篇,不行的话,我明天休息的时候也试试
蒂法娜
蒂法娜
试过了,不行
0
魔力猫
魔力猫
Spring事务回滚只接受RuntimeException
蒂法娜
蒂法娜
我试过了切面式的配置,试过了RuntimeException
0
李永china
李永china
你手动抛出的异常代码放到dao层insert方法试试
蒂法娜
蒂法娜
不行,哭
0
平安北京
确认数据库引擎,然后确认事务的配置是否正确
蒂法娜
蒂法娜
嗯,已经确认了数据库引擎,我用的数据库式mariaDB, 引擎是Innodb
0
培公啊
培公啊

看下log,看看Connection打开后注册没注册事务,确定下是事务没配上还是没回滚

或者配下rollbackFor Exception试试

蒂法娜
蒂法娜
回复 @贱起苍澜 : 这样啊
培公啊
培公啊
回复 @蒂法娜 : 你自己封装的jdbc是不是还要自己封装事务啊,我没自己封装过。。要么你等等别的大神回复
蒂法娜
蒂法娜
回复 @贱起苍澜 : 还是不行。setAutoCommit false,update,操作,insert操作错误,最后还是setAutoCommit true。
蒂法娜
蒂法娜
回复 @贱起苍澜 : 我这就试试看
培公啊
培公啊
回复 @蒂法娜 : 或者你可以试试不用注解声明事务,用拦截器式事务看看能不能回滚
下一页
0
0
ccc_clk
ccc_clk
整个项目看看?
0
张开源中国
张开源中国

以前也遇到,后来把异常定位到顶级异常类型就解决了,你可以试试:

<tx:method name="add*" propagation="REQUIRED"  read-only="false"  rollback-for="java.lang.Throwable"/>

蒂法娜
蒂法娜
好想还是不行
0
蒂法娜
蒂法娜
<2016-05-12 13:15:24> <DEBUG> [druid.sql.Connection] - {conn-10010} pool-connect
<2016-05-12 13:15:24> <DEBUG> [druid.sql.Connection] - {conn-10010} setAutoCommit false
<2016-05-12 13:15:24> <DEBUG> [druid.sql.Connection] - {conn-10009} pool-connect
<2016-05-12 13:15:24> <DEBUG> [druid.sql.Statement] - {conn-10009, pstmt-20016} Parameters : [201635152yhqh1]
<2016-05-12 13:15:24> <DEBUG> [druid.sql.Statement] - {conn-10009, pstmt-20016} Types : [VARCHAR]
<2016-05-12 13:15:24> <DEBUG> [druid.sql.Statement] - {conn-10009, pstmt-20016, rs-50017} query executed. 8.365109 millis. 
SELECT * FROM Appointment WHERE appointmentNum = ? LIMIT 1
<2016-05-12 13:15:24> <DEBUG> [druid.sql.ResultSet] - {conn-10009, pstmt-20016, rs-50017} open
<2016-05-12 13:15:24> <DEBUG> [druid.sql.ResultSet] - {conn-10009, pstmt-20016, rs-50017} Header: [id, kartingParkId, appointmentNum, userInfoId, eventId, price, status, updated_at, updated_by, created_at, created_by, expiresIn]
<2016-05-12 13:15:24> <DEBUG> [druid.sql.ResultSet] - {conn-10009, pstmt-20016, rs-50017} Result: [1, 0, 201635152yhqh1, 2, 3454, 0.00, 0, 2016-04-15 17:35:04.0, , 2016-04-15 17:35:04.0, o4g5lv5Hclc3m29MyYVZeOLEar1o, 900]
<2016-05-12 13:15:24> <DEBUG> [druid.sql.ResultSet] - {conn-10009, pstmt-20016, rs-50017} closed
<2016-05-12 13:15:24> <DEBUG> [druid.sql.Statement] - {conn-10009, pstmt-20016} 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-10009} pool-connect
<2016-05-12 13:15:24> <DEBUG> [druid.sql.Statement] - {conn-10009, pstmt-20017} Parameters : [1]
<2016-05-12 13:15:24> <DEBUG> [druid.sql.Statement] - {conn-10009, pstmt-20017} Types : [INTEGER]
<2016-05-12 13:15:24> <DEBUG> [druid.sql.Statement] - {conn-10009, pstmt-20017, rs-50018} query executed. 9.60952 millis. 
SELECT * FROM Appointment WHERE id = ? LIMIT 1
<2016-05-12 13:15:24> <DEBUG> [druid.sql.ResultSet] - {conn-10009, pstmt-20017, rs-50018} open
<2016-05-12 13:15:24> <DEBUG> [druid.sql.ResultSet] - {conn-10009, pstmt-20017, rs-50018} Header: [id, kartingParkId, appointmentNum, userInfoId, eventId, price, status, updated_at, updated_by, created_at, created_by, expiresIn]
<2016-05-12 13:15:24> <DEBUG> [druid.sql.ResultSet] - {conn-10009, pstmt-20017, rs-50018} Result: [1, 0, 201635152yhqh1, 2, 3454, 0.00, 0, 2016-04-15 17:35:04.0, , 2016-04-15 17:35:04.0, o4g5lv5Hclc3m29MyYVZeOLEar1o, 900]
<2016-05-12 13:15:24> <DEBUG> [druid.sql.ResultSet] - {conn-10009, pstmt-20017, rs-50018} closed
<2016-05-12 13:15:24> <DEBUG> [druid.sql.Statement] - {conn-10009, pstmt-20017} 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-10009} pool-connect
<2016-05-12 13:15:24> <DEBUG> [druid.sql.Statement] - {conn-10009, pstmt-20018} Parameters : [3454, 2016-04-15 17:35:04.0, o4g5lv5Hclc3m29MyYVZeOLEar1o, 2, 900, 2016-04-15 17:35:04.0, 201635152yhqh1, 0.0, test, 0, 0]
<2016-05-12 13:15:24> <DEBUG> [druid.sql.Statement] - {conn-10009, pstmt-20018} Types : [VARCHAR, VARCHAR, VARCHAR, VARCHAR, VARCHAR, VARCHAR, VARCHAR, VARCHAR, VARCHAR, VARCHAR, VARCHAR]
<2016-05-12 13:15:24> <DEBUG> [druid.sql.Statement] - {conn-10009, pstmt-20018} update executed. effort 1. 20.194617 millis. 
UPDATE Appointment SET eventId = ?, created_at = ?, created_by = ?, userInfoId = ?, expiresIn = ?, updated_at = ?, appointmentNum = ?, price = ?, updated_by = ?, kartingParkId = ?, status = ? WHERE id = 1
<2016-05-12 13:15:24> <DEBUG> [druid.sql.Statement] - {conn-10009, pstmt-20018} 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-10009} pool-connect
<2016-05-12 13:15:24> <DEBUG> [druid.sql.Statement] - {conn-10009, pstmt-20019} Parameters : [3454, 2016-04-15 17:35:04.0, o4g5lv5Hclc3m29MyYVZeOLEar1o, 2, 900, 2016-04-15 17:35:04.0, 201635152yhqh1, 0.0, test, 0, 0]
<2016-05-12 13:15:24> <DEBUG> [druid.sql.Statement] - {conn-10009, pstmt-20019} Types : [VARCHAR, VARCHAR, VARCHAR, VARCHAR, VARCHAR, VARCHAR, VARCHAR, VARCHAR, VARCHAR, VARCHAR, VARCHAR]
java.sql.BatchUpdateException: Duplicate entry '201635152yhqh1' for key 'appointmentNum'

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

0
阿影
阿影
<tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true"/>



阿影
阿影
不定义Service 接口,直接作用于类时,要加 proxy-target-class="true"
返回顶部
顶部