Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/hibernate/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Spring事务-防止未检查异常后回滚(RuntimeException)_Spring_Hibernate_Websphere_Jta_Spring Transactions - Fatal编程技术网

Spring事务-防止未检查异常后回滚(RuntimeException)

Spring事务-防止未检查异常后回滚(RuntimeException),spring,hibernate,websphere,jta,spring-transactions,Spring,Hibernate,Websphere,Jta,Spring Transactions,我无法阻止事务在运行时异常后回滚。我的环境是运行在WebSphere8.0上的Spring4.1+Hibernate3.6+JTA(WebSphereUowTransactionManager) 首先,是一个简单的案例,其行为符合预期。由于我捕获了RuntimeException,事务提交并成功创建了新资源 @Service("fooService") public class FooServiceImpl implements IFooService { @Transactional

我无法阻止事务在运行时异常后回滚。我的环境是运行在WebSphere8.0上的Spring4.1+Hibernate3.6+JTA(WebSphereUowTransactionManager)

首先,是一个简单的案例,其行为符合预期。由于我捕获了RuntimeException,事务提交并成功创建了新资源

@Service("fooService")
public class FooServiceImpl implements IFooService {

    @Transactional
    @Override
    public void doStuff(Resource res){
        authService.createResource(res, "ADMIN");
        try {
            throw new RuntimeException("SOMETHING");
        } catch (RuntimeException e) {
            e.printStackTrace();
        }
    }
下一个也可以。我声明norollboor并让事务提交:

    @Transactional(noRollbackFor=RuntimeException.class)
    @Override
    public void doStuff2(Resource res){
        authService.createResource(res, "ADMIN");
        throw new RuntimeException("SOMETHING");
    }
最后是有问题的。区别在于,在这种情况下,对
authService.createResource
的第二次调用引发了异常。仅供参考,
authService.createResource
仅标记为@Transactional,因此默认传播配置适用,并且应该加入调用服务的事务

    @Transactional(noRollbackFor=RuntimeException.class)
    @Override
    public void doStuff12(Resource res){

        authService.createResource(res, "ADMIN");
        try{
            res.setName("EXISTING-RESOURCE");
            authService.createResource(res, "ADMIN");
        }catch(RuntimeException e){
            e.printStackTrace();
        }
    }
尽管捕获RuntimeException并声明noRollbackFor属性,事务始终回滚。有什么解释吗

日志跟踪信息:

org.springframework.transaction.jta.WebSphereUowTransactionManager DEBUG - Creating new transaction with name [null]: PROPAGATION_REQUIRED,ISOLATION_DEFAULT; '',+com.myorg.webapps.exception.ElementoYaExistente
org.springframework.transaction.jta.WebSphereUowTransactionManager DEBUG - Invoking WebSphere UOW action: type=1, join=false
org.springframework.transaction.support.TransactionSynchronizationManager TRACE - Initializing transaction synchronization
org.springframework.transaction.interceptor.TransactionInterceptor TRACE - Getting transaction for [com.myorg.test.service.impl.FooServiceImpl.doStuff12]
org.springframework.transaction.jta.WebSphereUowTransactionManager DEBUG - Creating new transaction with name [null]: PROPAGATION_REQUIRED,ISOLATION_DEFAULT; ''
org.springframework.transaction.jta.WebSphereUowTransactionManager DEBUG - Invoking WebSphere UOW action: type=1, join=true
org.springframework.transaction.interceptor.TransactionInterceptor TRACE - Getting transaction for [com.myorg.authmgr.service.impl.AuthorizationServiceImpl.createResource]
org.springframework.transaction.jta.WebSphereUowTransactionManager DEBUG - Returned from WebSphere UOW action: type=1, join=true
org.springframework.transaction.jta.WebSphereUowTransactionManager DEBUG - Creating new transaction with name [null]: PROPAGATION_REQUIRED,ISOLATION_DEFAULT; ''
org.springframework.transaction.jta.WebSphereUowTransactionManager DEBUG - Invoking WebSphere UOW action: type=1, join=true
org.springframework.transaction.interceptor.TransactionInterceptor TRACE - Getting transaction for [com.myorg.authmgr.service.impl.AuthorizationServiceImpl.createResource]
org.springframework.transaction.interceptor.RuleBasedTransactionAttribute TRACE - Applying rules to determine whether transaction should rollback on java.lang.Runtime: Couldn't create the resource, it already exists: EXISTING-RESOURCE
org.springframework.transaction.interceptor.RuleBasedTransactionAttribute TRACE - Winning rollback rule is: null
org.springframework.transaction.interceptor.RuleBasedTransactionAttribute TRACE - No relevant rollback rule found: applying default rules
org.springframework.transaction.jta.WebSphereUowTransactionManager DEBUG - Returned from WebSphere UOW action: type=1, join=true
org.springframework.transaction.jta.WebSphereUowTransactionManager TRACE - Triggering beforeCommit synchronization
org.springframework.transaction.jta.WebSphereUowTransactionManager TRACE - Triggering beforeCompletion synchronization
org.springframework.transaction.support.TransactionSynchronizationManager TRACE - Clearing transaction synchronization
org.springframework.transaction.jta.WebSphereUowTransactionManager DEBUG - Returned from WebSphere UOW action: type=1, join=false

据我所知,一旦事务方法抛出运行时异常并被事务拦截器截获,事务就被标记为仅回滚。即使此事务性方法是从另一个事务性方法调用的

这对我来说很有意义:如果内部方法不能从异常中恢复,它就不能恢复,而外部方法不应该像什么都没发生一样

如果您希望事务不会回滚,则可以

  • 使内部方法成为非事务性的
  • 将内部方法配置为不回滚此异常
  • 有两种内部方法:
    • 它是事务性的,并且打算在还没有事务时调用,它只是委托给第二个事务
    • 一种非事务性的,并且打算作为已经存在的事务的一部分调用的事务

你说的有点道理。然而,奇怪的是,加入的事务没有考虑父的<代码> NORLROBACOF= = RunTimeExpReal.class < /Cult>规则。对我来说,它遵循整个事务范围的配置更符合逻辑。。。在这种情况下,内部方法必须是事务性的,并且在单独调用时必须回滚,因此我必须排除前两个建议。关于第三种情况,Spring迫使我们在API中提供两种实现,一种是事务性的,另一种不是事务性的,以管理这种(并非罕见的)情况,这是不合法的