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 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器