grdhgx 2013-09-03 01:07 采纳率: 0%
浏览 2826

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,... 是不是意味着每个操作都用了一个单独的连接?

  • 写回答

1条回答 默认 最新

  • 绝版男子 2013-09-30 07:28
    关注

    可以把每个connection对象都打印一下看看,就知道是不是单独的了。

    评论

报告相同问题?

悬赏问题

  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥15 stable diffusion
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条