찾다

 >  Q&A  >  본문

java - Spring +Mybatis 事务 不能回滚

配置事务的文件 spring-mybatis.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:p="http://www.springframework.org/schema/p"
    xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="http://www.springframework.org/schema/aop   
    http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
    http://www.springframework.org/schema/beans    
    http://www.springframework.org/schema/beans/spring-beans-4.3.xsd    
    http://www.springframework.org/schema/context    
    http://www.springframework.org/schema/context/spring-context-4.3.xsd
    http://www.springframework.org/schema/tx   
    http://www.springframework.org/schema/tx/spring-tx-4.3.xsd    
    http://www.springframework.org/schema/mvc    
    http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd">

    <context:component-scan base-package="com.ys.attendance.mapper"/>
 
     <bean id="propertyConfigurer"
        class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="location" value="classpath:conf/jdbc.properties" />
    </bean>

    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
        destroy-method="close">
        <property name="driverClassName" value="${driver}" />
        <property name="url" value="${url}" />
        <property name="username" value="${username}" />
        <property name="password" value="${password}" />
        <!-- 初始化连接大小 -->
        <property name="initialSize" value="${initialSize}"></property>
        <!-- 连接池最大数量 -->
        <property name="maxActive" value="${maxActive}"></property>
        <!-- 连接池最大空闲 -->
        <property name="maxIdle" value="${maxIdle}"></property>
        <!-- 连接池最小空闲 -->
        <property name="minIdle" value="${minIdle}"></property>
        <!-- 获取连接最大等待时间 -->
        <property name="maxWait" value="${maxWait}"></property>
    </bean>

    <!-- spring和MyBatis完美整合,不需要mybatis的配置映射文件 -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="configLocation" value="classpath:conf/mybatis/mybatis-config.xml"></property>
        <!-- 自动扫描mapping.xml文件 -->
        <property name="mapperLocations" value="classpath:conf/SqlMapper/sqlmap.xml"></property>
    </bean>

    <!-- DAO接口所在包名,Spring会自动查找其下的类 -->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.ys.attendance.mapper" />
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
    </bean>

    <!-- (事务管理)transaction manager, use JtaTransactionManager for global tx -->
    <bean id="transactionManager"
        class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource" />
    </bean>
    
    <!-- 事务注解驱动,标注@Transactional的类和方法将具有事务性 -->
    <tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true"/>

</beans>

在service 中写了一个方法 是使用注解 在 这个service类上加了 @Transactional

@Transactional("transactionManager")
@Service
public class AttendServiceImpl implements AttendService{

@Override
public void traincation() {
    Map<String,String> map1 = new HashMap<String, String>();
    map1.put("userid", "1111");
    map1.put("username","肖总");
    map1.put("date", "2017-03-15");
    map1.put("state", "/");
    
    Map<String,String> map2 = new HashMap<String, String>();
    map2.put("userid", "1131");
    map2.put("username","小名");
    map2.put("date", "2017-03-15");
    map2.put("state", "/");
    dao.add_attend_api(map1);
    
    try {
        int i = 1/0;//设置了一个异常
    } catch (Exception e) {
        System.out.println(e.getMessage());
        TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
    }
    dao.add_attend_api(map2);
    
    }
}

执行该方法 出现异常 没有回滚,第一条数据插入到了数据库中
org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.springframework.transaction.NoTransactionException: No transaction aspect-managed TransactionStatus in scope

是哪里配置有问题么?

怪我咯怪我咯2805일 전1422

모든 응답(3)나는 대답할 것이다

  • PHPz

    PHPz2017-04-18 10:52:34

    으아아아

    배송물 스캔 범위가 너무 작습니다. 귀하의 서비스는 해당 패키지에 포함되어 있습니다.

    회신하다
    0
  • PHP中文网

    PHP中文网2017-04-18 10:52:34

    메서드 선언 시 예외 발생을 추가해 보세요

    공개 무효 훈련에서 Exception()이 발생함{

    으아아아

    }

    회신하다
    0
  • PHPz

    PHPz2017-04-18 10:52:34

    Spring이 예외를 잡아서 롤백할 수 있도록 예외를 던져야 합니다.
    코드를 다음과 같이 변경하세요.

    으아악

    회신하다
    0
  • 취소회신하다