spring+mybatis,事务不起作用,求帮忙

grdhgx 发布于 2013/09/03 09:00
阅读 1K+
收藏 1

spring + mybatis,开始annotation式事务
配置如下:
   <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" >
        <property name="driverClassName" value="${jdbc.driver}" />
        <property name="url" value="${jdbc.url}" />
        <property name="username" value="${jdbc.user}" />
        <property name="password" value="${jdbc.password}" />
        <property name="maxIdle" value="60" />
        <property name="maxActive" value="100" />
        <property name="timeBetweenEvictionRunsMillis" value="3600000" />
        <property name="minEvictableIdleTimeMillis" value="3600000" />
    </bean>
    
    <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,... 是不是意味着每个操作都用了一个单独的连接?

加载中
0
Catelyn
Catelyn
不要catch异常试试
0
R-Lu
R-Lu

看下你的SqlSessionFactory配置了没有...

<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
		<property name="dataSource" ref="dataSource" />
		<property name="typeAliasesPackage" value="cn.haohaowo.entity" />
		<property name="configLocation" value="classpath:mybatis-config.xml"></property>
	</bean>

0
一路找北

看看你表的engine,mysql的MyISAM引擎是不支持事务的,想要事务的话换InnoDB

0
g
grdhgx

谢谢回答,问题已找到。
我少说了一点,我用的是spring MVC
在dispatch-servlet.xml加载时,需要对context:component-scan加exclude-filter,对Service类过滤,防止加载applicationContext.xml时因容器已经存在Service类,使得cglib将不对Service进行代理
<context:component-scan base-package="com.mytest" >
        <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Service" />
    </context:component-scan>

返回顶部
顶部