问题描述
- spring+mybatis,事务不起作用,求帮忙
-
spring + mybatis,开始annotation式事务
配置如下:<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource" /> </bean> <tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true" />
在Service层使用@Transactional,Service没有用接口
@Transactional
public void registerAccount(Account account, String email, Long classId) {
try {
addAccount(account);
AccountDetail accountDetail = new AccountDetail(accountId);
accountDetail.setEmail(email.trim());
addAccountDetail(accountDetail);
addAccountToClass(accountId, classId);
} catch (Exception e) {
logger.error(e.getMessage(), e);
throw new RuntimeException(e.getMessage());
}
}三个add*方法都是private方法,没有try catch块。当addAccountToClass方法抛错时,会在当前这个try catch块中被捕获,并抛出RuntimeException。但是,前面的两个add方法却没有回滚,谁知道是怎么回事?
log日志如下:
[DEBUG] 2013-09-03 08:51:48 :ooo Using Connection [jdbc:mysql://localhost:3306/test, UserName=root@localhost , MySQL-AB JDBC Driver]
[DEBUG] 2013-09-03 08:51:48 :==> Preparing: select * from account where name = ?;
[DEBUG] 2013-09-03 08:51:48 :==> Parameters: uuux(String)
[DEBUG] 2013-09-03 08:51:48 :ooo Using Connection [jdbc:mysql://localhost:3306/test, UserName=root@localhost , MySQL-AB JDBC Driver]
[DEBUG] 2013-09-03 08:51:48 :==> Preparing: insert into tbl_account (password, name, gender) values (?, ?, ?);
[DEBUG] 2013-09-03 08:51:48 :==> Parameters: 81dc9bdb52d04dc20036dbd8313ed055(String), uuux(String), 0(Integer)
[INFO ] 2013-09-03 08:51:48 :创建新用户uuux
[DEBUG] 2013-09-03 08:51:48 :ooo Using Connection [jdbc:mysql://localhost:3306/test, UserName=root@localhost , MySQL-AB JDBC Driver]
[DEBUG] 2013-09-03 08:51:48 :==> Preparing: insert into tbl_account_detail (account_id, email, age) values (?, ?, ?);
[DEBUG] 2013-09-03 08:51:48 :==> Parameters: 20(Long), ds@s.s(String), null
[INFO ] 2013-09-03 08:51:48 :创建新用户详情
[DEBUG] 2013-09-03 08:51:48 :ooo Using Connection [jdbc:mysql://localhost:3306/test, UserName=root@localhost , MySQL-AB JDBC Driver]
[DEBUG] 2013-09-03 08:51:48 :==> Preparing: insert into tbl_account_class_ref (account_id, class_id) values (accountId, ?);
[DEBUG] 2013-09-03 08:51:48 :==> Parameters: 1(Long)
[INFO ] 2013-09-03 08:51:48 :Loading XML bean definitions from class path resource [org/springframework/jdbc/support/sql-error-codes.xml]
[INFO ] 2013-09-03 08:51:48 :SQLErrorCodes loaded: [DB2, Derby, H2, HSQL, Informix, MS-SQL, MySQL, Oracle, PostgreSQL, Sybase]
[ERROR] 2013-09-03 08:51:48 :Error updating database. Cause: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Unknown column 'accountId' in 'field list'
The error may involve defaultParameterMap
The error occurred while setting parameters
SQL: insert into tbl_account_class_ref (account_id, class_id) values (accountId, ?);
Cause: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Unknown column 'accountId' in 'field list'
这里我觉得比较奇怪的一点事:日志中打印了多个ooo Using Connection [jdbc:mysql://localhost:3306/test,... 是不是意味着每个操作都用了一个单独的连接?
解决方案
可以把每个connection对象都打印一下看看,就知道是不是单独的了。