为什么Spring事务管理的传播级别“REQURIES_NEW”不起作用?

时间:2018-06-17 08:26:47

标签: java spring transactions propagation

我对Spring事务管理的传播机制特别关于REQUIRES_NEW感到困惑。作为文档here

  与PROPAGATION_REQUIRES_NEW相比,

PROPAGATION_REQUIRED对每个受影响的事务范围使用完全独立的事务。在这种情况下,底层物理事务是不同的,因此可以独立提交或回滚,外部事务不受内部事务的回滚状态的影响。

我理解的是,当使用@Transactional(propagation=Propagation.REQUIRES_NEW)注释任何方法时,始终会创建新的物理事务,如果它回滚,则不会影响外部事务。

现在我有以下情况,我调用两个事务方法,一个是默认传播级别,另一个是需要新传播。现在,当我从第二个方法抛出RuntimeException它应该仅回滚该事务而不是它回滚整个事务。以下是示例代码。

@Service
public class UserService {

@Autowired
private UserRepository userRepository;


@Transactional
public void firstTransaction() {
    User user = new User();
    user.setFirstname("Monica");
    user.setLastname("Galler");

    userRepository.save(user);

    secondTransaction();
}

@Transactional(propagation=Propagation.REQUIRES_NEW)
public void secondTransaction() {
    User user = new User();
    user.setFirstname("Rachel");
    user.setLastname("Green");

    userRepository.save(user);

    throw new RuntimeException("thown exception!!");
}

}

和我称之为此方法的类

@Component
public class TxHandler {

@Autowired
private UserService userService;

public void handleTransaction() {
    userService.firstTransaction();
}

}

请告诉我,我在这里遗失了什么?

以下是为事务管理器打印的日志

2018-06-17 13:37:52.553 DEBUG 903 --- [           main] o.s.orm.jpa.JpaTransactionManager        : Creating new transaction with name [com.example.SpringTutorial.service.UserService.firstTransaction]: PROPAGATION_REQUIRED,ISOLATION_DEFAULT; ''
2018-06-17 13:37:52.554 DEBUG 903 --- [           main] o.s.orm.jpa.JpaTransactionManager        : Opened new EntityManager [SessionImpl(PersistenceContext[entityKeys=[],collectionKeys=[]];ActionQueue[insertions=ExecutableList{size=0} updates=ExecutableList{size=0} deletions=ExecutableList{size=0} orphanRemovals=ExecutableList{size=0} collectionCreations=ExecutableList{size=0} collectionRemovals=ExecutableList{size=0} collectionUpdates=ExecutableList{size=0} collectionQueuedOps=ExecutableList{size=0} unresolvedInsertDependencies=null])] for JPA transaction
2018-06-17 13:37:52.561 DEBUG 903 --- [           main] o.s.orm.jpa.JpaTransactionManager        : Exposing JPA transaction as JDBC transaction [org.springframework.orm.jpa.vendor.HibernateJpaDialect$HibernateConnectionHandle@2e766822]
2018-06-17 13:37:52.582 DEBUG 903 --- [           main] o.s.orm.jpa.JpaTransactionManager        : Found thread-bound EntityManager [SessionImpl(PersistenceContext[entityKeys=[],collectionKeys=[]];ActionQueue[insertions=ExecutableList{size=0} updates=ExecutableList{size=0} deletions=ExecutableList{size=0} orphanRemovals=ExecutableList{size=0} collectionCreations=ExecutableList{size=0} collectionRemovals=ExecutableList{size=0} collectionUpdates=ExecutableList{size=0} collectionQueuedOps=ExecutableList{size=0} unresolvedInsertDependencies=null])] for JPA transaction
2018-06-17 13:37:52.582 DEBUG 903 --- [           main] o.s.orm.jpa.JpaTransactionManager        : Participating in existing transaction
2018-06-17 13:37:52.674 DEBUG 903 --- [           main] o.s.orm.jpa.JpaTransactionManager        : Found thread-bound EntityManager [SessionImpl(PersistenceContext[entityKeys=[EntityKey[com.example.SpringTutorial.entity.User#26]],collectionKeys=[]];ActionQueue[insertions=ExecutableList{size=1} updates=ExecutableList{size=0} deletions=ExecutableList{size=0} orphanRemovals=ExecutableList{size=0} collectionCreations=ExecutableList{size=0} collectionRemovals=ExecutableList{size=0} collectionUpdates=ExecutableList{size=0} collectionQueuedOps=ExecutableList{size=0} unresolvedInsertDependencies=null])] for JPA transaction
2018-06-17 13:37:52.674 DEBUG 903 --- [           main] o.s.orm.jpa.JpaTransactionManager        : Participating in existing transaction
2018-06-17 13:37:52.680 DEBUG 903 --- [           main] o.s.orm.jpa.JpaTransactionManager        : Initiating transaction rollback
2018-06-17 13:37:52.680 DEBUG 903 --- [           main] o.s.orm.jpa.JpaTransactionManager        : Rolling back JPA transaction on EntityManager [SessionImpl(PersistenceContext[entityKeys=[EntityKey[com.example.SpringTutorial.entity.User#26], EntityKey[com.example.SpringTutorial.entity.User#27]],collectionKeys=[]];ActionQueue[insertions=ExecutableList{size=2} updates=ExecutableList{size=0} deletions=ExecutableList{size=0} orphanRemovals=ExecutableList{size=0} collectionCreations=ExecutableList{size=0} collectionRemovals=ExecutableList{size=0} collectionUpdates=ExecutableList{size=0} collectionQueuedOps=ExecutableList{size=0} unresolvedInsertDependencies=null])]
2018-06-17 13:37:52.683 DEBUG 903 --- [           main] o.s.orm.jpa.JpaTransactionManager        : Closing JPA EntityManager [SessionImpl(PersistenceContext[entityKeys=[],collectionKeys=[]];ActionQueue[insertions=ExecutableList{size=0} updates=ExecutableList{size=0} deletions=ExecutableList{size=0} orphanRemovals=ExecutableList{size=0} collectionCreations=ExecutableList{size=0} collectionRemovals=ExecutableList{size=0} collectionUpdates=ExecutableList{size=0} collectionQueuedOps=ExecutableList{size=0} unresolvedInsertDependencies=null])] after transaction

0 个答案:

没有答案