UserTransaction.SetTransactionTimeout not working?

老子叫甜甜 提交于 2019-12-08 19:49:32

问题


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.

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

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.

What am I doing wrong?

@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");
    }
}

The method is completely executed:

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

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

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.


回答1:


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.



来源:https://stackoverflow.com/questions/28096898/usertransaction-settransactiontimeout-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!