2017-04-25 5 views
0

데이터베이스에 데이터를 삽입하는 메소드에 Spring Jdbc와 @Transaction annotation을 사용하고 있습니다. 코드의 코드 조각은 다음과 같습니다@Transaction은 봄에 트랜잭션을 롤백하지 않습니다.

나는 둘 이상의 테이블에서 데이터를 insrting하고 기능 tripDao.saveTripData(trip, userId) 내부
@Transactional(rollbackForClassName={"Exception"}) 
    public PreBookingResponse saveData(Tip trip, HttpServletRequest request) throws Exception { 
     PreBookingResponse preBookingResponse = null; 
     try { 
      String bookingRefNo = "89"; 
      trip.setBookingRefNo(bookingRefNo); 
      MinimumUserProfile userProfile = JwtService.getUserFromToken(request); 
      String userId = userProfile.getUserId(); 

      if (tripDao.saveTripData(trip, userId)) { 
       preBookingResponse = new PreBookingResponse(0, ""); 
       preBookingResponse.setBookingRefNo(bookingRefNo); 
       preBookingResponse.setSearchId(trip.getSearchId()); 
       preBookingResponse.setId(trip.getId()); 
      } 
     }catch(Exception e) 
     { 
      String errorMsg = "Error occur while saving trip data due to: " + e.getMessage(); 
      log.error(errorMsg); 
      preBookingResponse = new PreBookingResponse(2, errorMsg); 
      preBookingResponse.setBookingRefNo(trip.getBookingRefNo()); 
      preBookingResponse.setSearchId(trip.getSearchId()); 
      throw e; 
     } 
     return preBookingResponse; 
    } 

. 데이터를 삽입하는 동안 예외가 발생하면 모든 삽입 작업이 롤백 되지만 수행되지 않습니다.

spring-config.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:tx="http://www.springframework.org/schema/tx" 
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd 
     http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd 
     http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd"> 


    <!-- Enable Annotation based Declarative Transaction Management --> 
    <tx:annotation-driven proxy-target-class="true" 
     transaction-manager="transactionManager" /> 



    <!-- Creating TransactionManager Bean, since JDBC we are creating of type 
     DataSourceTransactionManager --> 
    <bean id="transactionManager" 
     class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> 
     <property name="dataSource" ref="dataSource" /> 
    </bean> 

    <!-- MySQL database connection --> 
    <bean id="dataSource" 
     class="org.springframework.jdbc.datasource.DriverManagerDataSource"> 
     <property name="url" value="jdbc:mysql://localhost:3306/aapi"></property> 
     <!-- Changed to mysql-connector-java 5.x since 6 is in beta phase --> 
     <property name="driverClassName" value="com.mysql.jdbc.Driver"></property> 
     <property name="username" value="root"></property> 
     <property name="password" value="root"></property> 
    </bean> 

    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"> 
     <property name="dataSource" ref="dataSource"></property> 
    </bean> 
</beans> 

내 build.gradle은 다음과 같습니다

buildscript { 
    ext { 
     springBootVersion = '1.5.2.RELEASE' 
    } 
    repositories { 
     mavenCentral() 
    } 
    dependencies { 
     classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}") 
    } 
} 


apply plugin: 'java' 
apply plugin: 'eclipse-wtp' 
apply plugin: 'org.springframework.boot' 

sourceCompatibility = 1.8 
targetCompatibility = 1.8 

repositories { 
    jcenter() 
} 

dependencies { 
    compile group: 'org.springframework.boot', name: 'spring-boot-starter-web', version: '1.5.2.RELEASE' 
    compile group: 'org.springframework.boot', name: 'spring-boot-starter-security', version: '1.5.2.RELEASE' 

    compile project(':supplier-commons') 
    runtime project(':supplier-commons') 

    compile 'org.springframework:spring-context:4.2.6.RELEASE' 
    compile 'org.springframework:spring-webmvc:4.2.6.RELEASE' 
    compile group: 'mysql', name: 'mysql-connector-java', version: '5.1.6' 
    compile group: 'org.apache.logging.log4j', name: 'log4j-api', version: '2.7' 
    compile group: 'org.apache.logging.log4j', name: 'log4j-core', version: '2.7' 
    compile group: 'com.typesafe.akka', name: 'akka-actor_2.11', version: '2.4.1' 
    compile 'io.jsonwebtoken:jjwt:0.7.0' 
    compile group: 'javax.mail', name: 'mail', version: '1.4' 
    compile group: 'freemarker', name: 'freemarker', version: '2.3.9' 
    compile group: 'org.springframework.security', name: 'spring-security-crypto', version: '4.2.2.RELEASE' 
    compile group: 'org.apache.commons', name: 'commons-email', version: '1.4' 
    compile group: 'com.thoughtworks.xstream', name: 'xstream', version: '1.4.9' 
    compile group: 'org.jmockit', name: 'jmockit', version: '1.8' 
    compile 'io.jsonwebtoken:jjwt:0.7.0' 
    compile group: 'com.ning', name: 'async-http-client', version: '1.6.4' 
    compile group: 'org.mockito', name: 'mockito-all', version: '1.8.4' 
    compile group: 'io.reactivex.rxjava2', name: 'rxjava', version: '2.0.7' 

    runtime('org.springframework.boot:spring-boot-devtools') 
    compileOnly('org.springframework.boot:spring-boot-configuration-processor') 
    compile group: 'org.aspectj', name: 'aspectjweaver', version: '1.8.8' 
    // https://mvnrepository.com/artifact/org.springframework/spring-tx 
    compile group: 'org.springframework', name: 'spring-tx', version: '4.1.4.RELEASE' 
    // https://mvnrepository.com/artifact/org.eclipse.persistence/javax.persistence 
compile group: 'org.eclipse.persistence', name: 'javax.persistence', version: '2.1.0' 
    // https://mvnrepository.com/artifact/org.hibernate/hibernate-core 

    // https://mvnrepository.com/artifact/org.hibernate/hibernate-core 
compile group: 'org.hibernate', name: 'hibernate-core', version: '3.6.5.Final' 
    // https://mvnrepository.com/artifact/org.springframework/spring-orm 
compile group: 'org.springframework', name: 'spring-orm', version: '4.3.8.RELEASE' 
    // https://mvnrepository.com/artifact/org.hibernate/hibernate 
compile group: 'org.hibernate', name: 'hibernate', version: '3.2.6.ga' 

    testCompile 'junit:junit:4.12' 

} 

springBoot { 
    executable = true 
} 

eclipse { 

    wtp { 
    facet { 
     facet name: 'jst.web', version: '2.4' 
    } 

    component { 
     contextPath = 'aggregator-api' 
    } 

    } 
} 

이유는 트랜잭션이 롤백되지 않습니다?

답변

0

이 코드는 트랜잭션을 롤백하지 않으므로 예외가 메소드에 던져지지 않습니다.

@Transactional (rollbackForClassName = { "Exception"})은 Exception이이 메소드에서 반환 될 때 트랜잭션을 롤백하는 것을 의미합니다. 그러나 귀하의 경우에는 예외를 잡아 적절한 응답을 반환하므로 트랜잭션이 커밋됩니다. Spring은 메소드 호출을 중심으로 AOP 트랜잭션 어드바이스를 생성한다.이 메소드 호출은 정상적으로 메소드가 리턴되면 성공적으로 커밋하고,이 메소드에서 예외가 발생하면 롤백한다.

롤백 중이 방법에서 예외를 던지거나 다른 방법으로 try 블록 내부의 코드를 이동하고 @Transactional과 그 방법을 주석 (rollbackForClassName = { "예외"}) 당신은 defaultAutoCommit을 중지해야

+0

이 함수에서 예외가 발생합니다. catch 블록의 마지막 줄을보십시오. –

0

dataSource.