UserTransaction.SetTransactionTimeout无法正常工作? [英] UserTransaction.SetTransactionTimeout not working?

查看:658
本文介绍了UserTransaction.SetTransactionTimeout无法正常工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在下面的代码中,我试图强制特定bean设置的事务超时,其事务时间短于方法完成所需的时间。

In the following code I am trying to force a transaction timeout for a specific bean setting its transaction time shorter than the time it takes for the method to complete.

超时设置为 3秒,方法完成所需的时间 5秒

The timeout is set for 3 seconds, and the time it takes for the method to complete is 5 seconds.

我使用的是便携式解决方案,它指的是BMT的组合,并使用 setTransactionTimeout 方法。

I am using a portable solution which refers to a combination of BMT and setting the timeout with the setTransactionTimeout method.

我希望交易要失效,请给我一个例外,但这不会发生。

I would expect the transaction to be invalidated an throw me an Exception, but that is not happening.

我做错了什么?

@Singleton
@Startup
@TransactionManagement(TransactionManagementType.BEAN)
public class TimerSingleton {

    @Inject
    private UserTransaction ut;

    @PostConstruct
    public void execute() throws Exception {

        ut.begin();
        ut.setTransactionTimeout(3); // Transaction should timeout after 3 seconds

        System.out.println(">>> Executing...");
        Thread.sleep(5000); // Block for 5 seconds

        ut.commit();

        System.out.println(">>> Completed");
    }
}

该方法已完全执行:

17:00:12,138 INFO  [stdout] (ServerService Thread Pool -- 85) >>> Executing...

17:00:17,139 INFO  [stdout] (ServerService Thread Pool -- 85) >>> Completed

我使用的是Wildfly 8.2,我知道 @TransactionTimeout 注释,但它是专有的,我想知道如何以可移植的方式控制它。

I am using Wildfly 8.2 and I know about the @TransactionTimeout annotation, but it is proprietary and I would like to know how to control it in a portable manner.

推荐答案

在调用 begin 方法之前必须调用 setTransactionTimeout 方法,这是因为 setTransactionTimeout 使用 begin 方法修改与当前线程启动的事务关联的超时值。

The setTransactionTimeout method must invoked before you call begin method, this is because setTransactionTimeout modify the timeout value that is associated with transactions started by the current thread with the begin method.

这篇关于UserTransaction.SetTransactionTimeout无法正常工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆