open-liberty

open-liberty - extend transaction timeout for specific operation


i have a container managed transaction in a REST call with default liberty timeout of 120sec.

I want for a specific operation withtout spawning a new transaction to extend it to 5 minutes. how can i do this?

I know I can change timeout in server.xml, but this would change timeout for all operations.


Solution

  • Liberty UOWManager SPI

    You could use the Liberty UOWManager SPI, but you'd have to "clean up" after setting a custom timeout if you want later methods to revert back to the default timeout once this thread goes back to the Liberty thread pool and gets reused.

    Transactional CDI Bean

    @Dependent
    public class MyTransactional {
    
        @Transactional
        public String myMethod() { ... }
    

    JAX-RS resource class

    import static com.ibm.websphere.uow.UOWSynchronizationRegistry.UOW_TYPE_GLOBAL_TRANSACTION;
    import com.ibm.wsspi.uow.UOWManager;
    
       
     ...
    
        @Resource
        private UOWManager uowm;
    
        @Inject MyTransactional transactionalBean // annotated with @Transactional
    
        @GET
        @Produces(MediaType.TEXT_PLAIN)
        @Path("m1")
        public Response m1() throws Exception {
            String r = null;
            int CUSTOM_TIMEOUT_SECONDS = 300; 
            try {     
                uowm.setUOWTimeout(UOW_TYPE_GLOBAL_TRANSACTION, CUSTOM_TIMEOUT_SECONDS);    
                r = transactionalBean.myMethod();        
            } finally {
                // cleanup thread (so it reverts to default timeout after going back in the thread pool)
                uowm.setUOWTimeout(UOW_TYPE_GLOBAL_TRANSACTION, 0);     
            }
            return Response.ok(r).build();
        }
    

    pom.xml

    You can add this dependency to your Maven build with an entry like (the version might seem kind of random; it's tied to the Liberty fix pack version but this type of SPI won't likely change too frequently across versions).

            <dependency>
                <groupId>com.ibm.websphere.appserver.api</groupId>
                <artifactId>com.ibm.websphere.appserver.api.transaction</artifactId>
                <version>1.1.82</version>
            </dependency>