ghb5371548 2014-09-30 16:41 采纳率: 0%
浏览 238
已采纳

ibatis+spring mvc事务不能回滚

最近才接触spring MVC和ibates,现在我需要配置事务回滚,但是倒腾了一天还是不行,熟悉的朋友帮忙看看: applicationContext.xml文件配置:

 

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"   
       xmlns:aop="http://www.springframework.org/schema/aop" 
       xmlns:tx="http://www.springframework.org/schema/tx"   
       xsi:schemaLocation="http://www.springframework.org/schema/beans   http://www.springframework.org/schema/beans/spring-beans-4.0.xsd   
          http://www.springframework.org/schema/context   http://www.springframework.org/schema/context/spring-context-4.0.xsd   
          http://www.springframework.org/schema/aop    http://www.springframework.org/schema/aop/spring-aop-4.0.xsd   
          http://www.springframework.org/schema/tx     http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
          http://www.springframework.org/schema/cache  http://www.springframework.org/schema/cache/spring-cache-4.0.xsd
          http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd 
          http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd"
          default-autowire="byName">
    
    <context:component-scan base-package="com.suneee">           
       <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
    </context:component-scan>
          
    <bean id="propertyConfigurer"
        class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="location">
            <value>classpath:com/suneee/config/mysql/jdbc.properties</value>
        </property>
    </bean>
    
    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
        <property name="driverClassName" value="${db.driverClassName}"/>
        <property name="url" value="${db.url}"/>
        <property name="username" value="${db.username}"/>
        <property name="password" value="${db.password}"/>
    </bean>   
    
    <!-- 配置数据源 -->  
    <bean id="sqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
        <property name="dataSource" ref="dataSource"/>
        <property name="configLocation">
            <value>classpath:com/suneee/config/ibatis/SqlMapConfig.xml</value>
        </property>
    </bean>
    
    <!-- 配置事务管理器 -->
    <bean id="transactionManager"
        class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource" /> 
    </bean>
    
    <!-- 配置哪些类的方法需要进行事务管理 proxy-target-class="true" -->
    <aop:config >
       <aop:pointcut id="crudMethos" expression="execution(* com.suneee.service.*.*(..))"/>   
       <aop:advisor advice-ref="txAdvice" pointcut-ref="crudMethos" /> 
    </aop:config>
    
    <!--配置哪些方法,什么情况下需要回滚-->
    <tx:advice id="txAdvice" transaction-manager="transactionManager">
         <tx:attributes>
           <tx:method name="insert*" propagation="REQUIRED" rollback-for="java.lang.Exception"/>  
           <tx:method name="delete*" propagation="REQUIRED" rollback-for="java.lang.Exception"/>   
           <tx:method name="update*" propagation="REQUIRED" rollback-for="java.lang.Exception"/>  
            
          <!--   <tx:method name="*" propagation="REQUIRED" read-only="true"/> -->  <!-- 除了上面标识的方法,其他方法全是只读方法 -->    
        
         </tx:attributes>
    </tx:advice> 
    
</beans>

 

springMVC.XML文件内容如下:

 

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd" default-autowire="byName">


    <!-- 自动扫描的包名 --> 
    <context:component-scan base-package="com.suneee">
         <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Service"/>
    </context:component-scan>
    
    <!-- 视图解释类 -->  
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/jsp/"></property>
        <property name="suffix" value=".jsp"></property>
    </bean>
    
    <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
    </bean>
    
    <!-- 拦截器 
    <mvc:interceptors>
        <mvc:interceptor>
            <mvc:mapping path="/*.do"/>
            <bean class="com.suneee.interceptor.MyInterceptor"></bean>
        </mvc:interceptor>
    </mvc:interceptors> -->
    
</beans>

 

UserService.java内容如下:

 

package com.suneee.service.impl;

import java.util.List;

import org.springframework.stereotype.Service;

import com.suneee.bean.User;
import com.suneee.service.IUserService;
import com.suneee.service.base.BaseService;


@Service("userService")
public class UserService extends BaseService<User> implements IUserService{

    @Override
    public List<User> findUser(User user) {
        return   queryForList("user_queryAll",user);
    }

    @Override
    public User insert(User user){
      
          insert("user_insert",user);
             
          user = null;//故意设置为null,抛出异常  
          insert("user_inserts",user);
          
    
      return user;
    }

}

 

BaseService.java文件如下:

package com.suneee.service.base;

import java.sql.SQLException;
import java.util.List;

import org.springframework.orm.ibatis.SqlMapClientTemplate;

 
public class BaseService<T> extends SqlMapClientTemplate{
    public static final int PAGE_SIZE = 15;

    public Object insert(String sqlId,Object baseClass){
        Object baseClassResult = null;
        try {
            baseClassResult = (Object) super.getSqlMapClient().insert(sqlId,baseClass);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return baseClassResult;
    }
    
    public Object queryForObject(String sqlId,Object baseClass){
        Object baseClassResult = null;
        try {
            baseClassResult = (Object) super.getSqlMapClient().queryForObject(sqlId,baseClass);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return baseClassResult;
    }
    
    public Integer delete(String sqlId,Integer id){
        Integer number = null;
        try {
            number = super.getSqlMapClient().delete(sqlId, id);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return number;
    }
    
    public Integer deleteByObject(String sqlId,Object obj){
        Integer number = null;
        try {
            number = super.getSqlMapClient().delete(sqlId, obj);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return number;
    }
    
    public Object queryById(String sqlId,Integer id){
        Object baseClassResult = null;
        try {           
            baseClassResult = (Object) super.getSqlMapClient().queryForObject(sqlId,id);        
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return baseClassResult;
    }
    
    
    public List<Object> queryListByPage(String sqlId,Object obj){
        List<Object> list = null;
        try {
            list = (List<Object>) super.getSqlMapClient().queryForList(sqlId, obj);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return list;
    }
    
    public List<Object> queryListNoPage(String sqlId,Object obj){
        List<Object> list = null;
        try {
            list = (List<Object>) super.getSqlMapClient().queryForList(sqlId, obj);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return list;
    }
    
    public int update(String sqlId,Object obj){
        Integer number = null;
        try {
            number = super.getSqlMapClient().update(sqlId, obj);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return number;
    }
    
     
    
    //批量入库
    public boolean insertBatch(String sqlId,List list){
        Object obj = null;
        try {
            super.getSqlMapClient().startBatch();
            for(int i=0; i<list.size(); i++){
                obj = list.get(i);
                super.getSqlMapClient().insert(sqlId,obj);
            }
            super.getSqlMapClient().executeBatch();
            return true;
        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        }       
    }
    
    
    public int getCount(String sqlId){
        Integer i = null;
        try {
            i =  (Integer) super.getSqlMapClient().queryForObject(sqlId);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return i;
    }
    
}

 

以上就是我项目各个地方的代码,第二个插入数据方法我故意设置为null,执行报错,但是第一个插入数据方法执行了数据插入,没有回滚,怎样才能回滚啊,声明式的事务配置有错还是什么原因,熟悉的兄弟看看,困扰了我一天多了

  • 写回答

1条回答 默认 最新

  • freedamjustice 2014-09-30 17:03
    关注

    ibatis没有用过,不过从你的代码来看,insert方法里面有把异常捕抓了。你看是不是捕抓成功,如果是捕抓了,那么没有回滚是正常的。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 鼠标右键,撤销删除 复制 移动,要怎样删除
  • ¥15 使用MATLAB进行余弦相似度计算加速
  • ¥15 服务器安装php5.6版本
  • ¥15 我想用51单片机和数码管做一个从0开始的计数表 我写了一串代码 但是放到单片机里面数码管只闪烁一下然后熄灭
  • ¥20 系统工程中,状态空间模型中状态方程的应用。请猛男来完整讲一下下面所有问题
  • ¥15 我想在WPF的Model Code中获取ViewModel Code中的一个参数
  • ¥15 arcgis处理土地利用道路 建筑 林地分类
  • ¥20 使用visual studio 工具用C++语音,调用openslsx库读取excel文件的sheet问题
  • ¥100 寻会做云闪付tn转h5支付链接的技术
  • ¥15 DockerSwarm跨节点无法访问问题