Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UserTransaction.SetTransactionTimeout not working?

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.

like image 236
Evandro Pomatti Avatar asked Jan 22 '15 19:01

Evandro Pomatti


1 Answers

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.

like image 91
Federico Sierra Avatar answered Oct 02 '22 15:10

Federico Sierra