天天看點

spring,ibatis整合後的事務問題

spring,ibatis整合後的事務問題

SPRING事務不復原的問題,我進行了兩次插入資料庫的操作,第二次故意給一個非空字段插入null,但第一條記錄還是成功插入了,一個星期了,沒搞定,求指教。其中資料庫已經設定為autocommit為false

架構及版本:

struts2 -2.0.1.4 , spring -2.5 ,ibatis 2.3,mysql 5.0

aplicationContext.xml:

Xml代碼  

spring,ibatis整合後的事務問題
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"  
  4.     xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"  
  5.     xsi:schemaLocation="http://www.springframework.org/schema/beans     
  6.            http://www.springframework.org/schema/beans/spring-beans-2.5.xsd     
  7.            http://www.springframework.org/schema/context     
  8.            http://www.springframework.org/schema/context/spring-context-2.5.xsd     
  9.            http://www.springframework.org/schema/aop  
  10.            http://www.springframework.org/schema/aop/spring-aop-2.5.xsd     
  11.            http://www.springframework.org/schema/tx  
  12.            http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">  
  13.     <bean id="dao" class="last.soul.dao.DAOImpl">  
  14.         <property name="sqlMapClient" ref="sqlMapClient"></property>  
  15.     </bean>  
  16.     <bean id="userService" class="last.soul.service.UserServiceImpl">  
  17.         <property name="dao" ref="dao"></property>  
  18.     </bean>  
  19.     <bean id="login" class="last.soul.action.LoginAction">  
  20.         <property name="userService" ref="userService"></property>  
  21.     </bean>  
  22.     <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"  
  23.         destroy-method="close">  
  24.         <property name="driverClassName" value="com.mysql.jdbc.Driver" />  
  25.         <property name="url" value="jdbc:mysql://localhost:3306/test" />  
  26.         <property name="username" value="root" />  
  27.         <property name="password" value="root" />  
  28.     </bean>  
  29.     <bean id="sqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">  
  30.         <property name="configLocation" value="WEB-INF/sqlMapConfig.xml" />  
  31.         <property name="dataSource" ref="dataSource" />  
  32.     </bean>  
  33.     <bean id="transactionManager"  
  34.         class="org.springframework.jdbc.datasource.DataSourceTransactionManager">  
  35.         <property name="dataSource">  
  36.             <ref bean="dataSource" />  
  37.         </property>  
  38.     </bean>  
  39.     <!--  
  40.         利用spring的TransactionProxyFactoryBean去對事務進行自動管理 <bean id="daoTr"  
  41.         class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">  
  42.         <property name="transactionManager"> <ref local="transactionManager"  
  43.         /> </property> <property name="target"> <list><ref local="userService"  
  44.         /></list> </property> <property name="transactionAttributes"> <props>  
  45.         <prop key="*">PROPAGATION_REQUIRED,-Exception</prop> </props>  
  46.         </property> </bean>  
  47.     -->  
  48.     <tx:advice id="txAdvice" transaction-manager="transactionManager">  
  49.         <!-- 哪些方法有 事務的傳播性-->  
  50.         <tx:attributes>  
  51.             <tx:method name="*" isolation="READ_COMMITTED" propagation="REQUIRED" rollback-for="java.lang.Exception"/>  
  52.         </tx:attributes>  
  53.     </tx:advice>  
  54.     <!-- 哪些類的方法參與了事務 -->  
  55.     <aop:config>  
  56.         <!-- execution(* com.east.spring.managerimpl.*.*(..))這個類的所有方法都用事務 -->  
  57.         <aop:pointcut id="allManagerMethod"  
  58.             expression="execution(* last.soul.service.UserServiceImpl.*.*(..))" />  
  59.         <!-- 引用 pointcut 和 advice-->  
  60.         <aop:advisor pointcut-ref="allManagerMethod" advice-ref="txAdvice" />  
  61.     </aop:config>  
  62. </beans>  

UserServiceImpl:

package last.soul.service;

import last.soul.common.beans.User;

import last.soul.dao.DAO;

public class UserServiceImpl implements UserService {

    DAO dao;

    public boolean login(String username, String password) throws Exception {

        User u1=new User();

        u1.setEmail("[email protected]");

        u1.setPassword("ccc");

        u1.setUsername("aaaa");

        dao.insert("user.insert", u1);

        User u2=new User();

        u2.setEmail("[email protected]");

        u2.setPassword("he");

        u2.setUsername(null);

        dao.insert("user.insert", u2);

        return true;

    }

    public DAO getDao() {

        return dao;

    }

    public void setDao(DAO dao) {

        this.dao = dao;

    }

    public boolean isExist(String username) {

        return false;

    }

    public Integer register(User record) {

        return null;

    }

}

DAOImpl:

package last.soul.dao;

import java.sql.SQLException;

import org.springframework.orm.ibatis.support.SqlMapClientDaoSupport;

public class DAOImpl  extends SqlMapClientDaoSupport implements DAO{

    @SuppressWarnings("unchecked")

     public void insert(String sql,Object map) throws SQLException

    {

            this.getSqlMapClientTemplate().insert(sql, map);

    }

}

問題補充: liwenjie 寫道 spring預設進行復原的異常是uncheckedException,而lz的SQLException 是checkedexception,無法進行復原是以需要進行聲明

<tx:advice id="txAdvice" transaction-manager="txManager">

  <tx:attributes>

         <tx:method name="get*" read-only="false" />

         <tx:method name="*" rollback-for="SQLException"/>

  </tx:attributes>

</tx:advice>

詳情請參見 spring參考手冊 復原

我配的Exception不也一樣而且我就是配置上了SQLException也是不復原。

後來我又用了另外一個方法就是讓他抛出我自己的異常,我讓我自己的這個異常繼承RuntimeException也是不復原啊