spring @Transactional JDBC 模板 MySQL DB 不回滚 [英] spring @Transactional JDBC Template MySQL DB not rolling back

查看:97
本文介绍了spring @Transactional JDBC 模板 MySQL DB 不回滚的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用来自服务层调用的 jdbc 模板来实现 Spring @transactionalDAOImpl 中的 2 个插入方法并使用 simplejdbctemplate 插入,我在日志中看到 spring 在我的服务方法上创建了一个新事务,我的第一个插入成功,第二个插入失败,即使它说它正在回滚相同连接第一次插入永远不会从我的 Mysql 数据库回滚.(我使用的是 innodb 引擎).

I am trying to implement the Spring @transactional with jdbc templates from the service layer calling 2 insert methods in a DAOImpl and using simplejdbctemplate to the insert and i see in the logs that spring creates a new transaction on my service method and my first insert suceeds and the second insert fails and even though it says it is rolling back on the same connection the first insert never get rolled back from my Mysql DB.(i am using innodb engine).

这是我的服务方法.

@Service
@TransactionConfiguration(transactionManager="txManager")
public class NewWizardService{
    ApplicationContext ctx = new ClassPathXmlApplicationContext("dataSourcesConfig.xml");
    UserDAO userDAO = (UserDAO)ctx.getBean("userDAO");
    @Transactional(rollbackFor=Throwable.class,  readOnly=false)
    public void createNewFirm()
    {

    userDAO.insert1();
    userDAO.insert2();

    }

}

这是我的数据源和事务管理器 spring 配置.

Here is my datasource and transaction manager spring configuration.

<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    <property name="dataSource" ref="dataSource"/>
</bean>


<bean id="dbcpDataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
    <property name="driverClassName"><value>${jdbc.driverClassName}</value></property>
    <property name="url"><value>${jdbc.url}</value></property>
    <property name="username"><value>${jdbc.username}</value></property>
    <property name="password"><value>${jdbc.password}</value></property>
</bean>

<tx:annotation-driven transaction-manager="txManager" proxy-target-class="true"/>

这是我的日志跟踪.

2012-03-28 16:56:31,460 DEBUG [org.springframework.jdbc.datasource.DataSourceTransactionManager] - Creating new transaction with name [com.CAIS.wizardService.NewWizardServiceImpl.createNewFirm]: PROPAGATION_REQUIRED,ISOLATION_DEFAULT; '',-java.lang.RuntimeException
2012-03-28 16:56:31,654 DEBUG [org.springframework.jdbc.datasource.DataSourceTransactionManager] - Acquired Connection [org.apache.commons.dbcp.PoolableConnection@7d627b8b] for JDBC transaction
2012-03-28 16:56:31,660 DEBUG [org.springframework.jdbc.datasource.DataSourceTransactionManager] - Switching JDBC Connection [org.apache.commons.dbcp.PoolableConnection@7d627b8b] to manual commit
2012-03-28 16:56:31,663 DEBUG [org.springframework.jdbc.core.JdbcTemplate] - Executing prepared SQL update
2012-03-28 16:56:31,663 DEBUG [org.springframework.jdbc.core.JdbcTemplate] - Executing prepared SQL statement [insert into client (fullName, createDate, createUser) values (?, ?, ?)]
2012-03-28 16:56:31,663 DEBUG [org.springframework.jdbc.datasource.DataSourceUtils] - Fetching JDBC Connection from DataSource
2012-03-28 16:56:31,664 DEBUG [org.springframework.jdbc.datasource.DriverManagerDataSource] - Creating new JDBC DriverManager Connection to [jdbc:mysql://localhost:3306/cais]
2012-03-28 16:56:31,816 DEBUG [org.springframework.jdbc.datasource.DataSourceUtils] - Registering transaction synchronization for JDBC Connection
2012-03-28 16:56:31,833 DEBUG [org.springframework.jdbc.core.JdbcTemplate] - SQL update affected 1 rows
2012-03-28 16:56:31,840 DEBUG [org.springframework.jdbc.core.JdbcTemplate] - SQLWarning ignored: SQL state '01000', error code '1265', message [Data truncated for column 'createDate' at row 1]
2012-03-28 16:56:31,842 DEBUG [org.springframework.jdbc.core.JdbcTemplate] - Executing prepared SQL update
2012-03-28 16:56:31,842 DEBUG [org.springframework.jdbc.core.JdbcTemplate] - Executing prepared SQL statement [insert into client (fullName, createDate, createUser) values (?, ?, ?)]
2012-03-28 16:56:31,918 DEBUG [org.springframework.beans.factory.support.DefaultListableBeanFactory] - Finished creating instance of bean 'Sybase'
2012-03-28 16:56:31,918 INFO [org.springframework.jdbc.support.SQLErrorCodesFactory] - SQLErrorCodes loaded: [DB2, Derby, H2, HSQL, Informix, MS-SQL, MySQL, Oracle, PostgreSQL, Sybase]
2012-03-28 16:56:31,918 DEBUG [org.springframework.jdbc.support.SQLErrorCodesFactory] - Looking up default SQLErrorCodes for DataSource [org.springframework.jdbc.datasource.DriverManagerDataSource@6cf84b0a]
2012-03-28 16:56:31,920 DEBUG [org.springframework.jdbc.support.SQLErrorCodesFactory] - Database product name cached for DataSource [org.springframework.jdbc.datasource.DriverManagerDataSource@6cf84b0a]: name is 'MySQL'
2012-03-28 16:56:31,920 DEBUG [org.springframework.jdbc.support.SQLErrorCodesFactory] - SQL error codes for 'MySQL' found
2012-03-28 16:56:31,920 DEBUG [org.springframework.jdbc.support.SQLErrorCodeSQLExceptionTranslator] - Unable to translate SQLException with Error code '1406', will now try the fallback translator
2012-03-28 16:56:31,920 DEBUG [org.springframework.jdbc.support.SQLStateSQLExceptionTranslator] - Extracted SQL state class '22' from value '22001'
2012-03-28 16:56:31,921 DEBUG [org.springframework.jdbc.datasource.DataSourceUtils] - Returning JDBC Connection to DataSource
2012-03-28 16:56:31,923 DEBUG [org.springframework.jdbc.datasource.DataSourceTransactionManager] - Initiating transaction rollback
2012-03-28 16:56:31,923 DEBUG [org.springframework.jdbc.datasource.DataSourceTransactionManager] - Rolling back JDBC transaction on Connection [org.apache.commons.dbcp.PoolableConnection@7d627b8b]
2012-03-28 16:56:31,934 DEBUG [org.springframework.jdbc.datasource.DataSourceTransactionManager] - Releasing JDBC Connection [org.apache.commons.dbcp.PoolableConnection@7d627b8b] after transaction
2012-03-28 16:56:31,934 DEBUG [org.springframework.jdbc.datasource.DataSourceUtils] - Returning JDBC Connection to DataSource

提前致谢.

推荐答案

你的 需要在您的 webapp-serlet.xml 中定义.

Your <tx:annotation-driven transaction-manager="txManager" proxy-target-class="true"/> needs to be defined in your webapp-serlet.xml.

您会在此处找到说明:忽略 Spring @Transactional 注释

这篇关于spring @Transactional JDBC 模板 MySQL DB 不回滚的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆