简体   繁体   中英

Using custom timeout in Spring transaction management

I have several methods with spring @Transactional in my project as following:

@Transactional(value = "sys.tx.mngr", propagation = Propagation.REQUIRES_NEW)
public void addMember(InputParam input) 
{
       // do somthing...
}

@Transactional(value = "sys.tx.mngr", propagation = Propagation.REQUIRES_NEW)
public void blockMember(InputBlockParam param) 
{
      // do somthing...
}

Then I set different timeout per method as following:

@Transactional(value = "sys.tx.mngr", propagation = Propagation.REQUIRES_NEW,timeout = 40)
public void addMember(InputParam input) 
{
       // do somthing...
}

@Transactional(value = "sys.tx.mngr", propagation = Propagation.REQUIRES_NEW, timeout = 20)
public void blockMember(InputBlockParam param) 
{
     // do somthing...
}

I want in last step set timeout as configurable by a properties file but I don't know what. Is there any solution for set timeout in spring Transactional annotaion configurable or dynamically?

EDIT: I define sys.tx.mngr in spring context file as following:

<bean id="sys.tx.mngr" class="org.springframework.orm.jpa.JpaTransactionManager">
    <property name="entityManagerFactory" ref="emf"/>
</bean>

<tx:annotation-driven transaction-manager="sys.tx.mngr" />

Or is there alternative way for define timeout in spring context file per method?

It can be done on following ways only :-

a) Using Reflection.
b) Using Instrumentation.
c) Using TransactionTemplate (Programatically transaction).

For (a) & (b) you can have your class like following :-

public class Test implements InitializingBean {

@Autowired
private Environment env;

public void afterPropertiesSet() throws Exception {
System.out.println("Sample prop 1 value : "+env.resolvePlaceholders("${prop1.value}"));
//Code to set/modify Transactional annotation "timeout" 
// attribute values for all methods
}

}

Link on how to set/modify values can be found here

Modify field annotation value dynamically
Modify a class definition's annotation string parameter at runtime

For (c) you can have config like :-

public class MemberDaoImpl {

@Autowired
private Environment env;
@Autowired
private TransactionTemplate transactionTemplate;

        public void addMember(InputParam input) {  
  transactionTemplate.setTimeout(Integer.parseInt(env.resolvePlaceholders("${addmember.timeout}")));
               // do somthing...
        }

}

<bean id="memberDao" class="com.xxx.impl.MemberDaoImpl">
    <property name="transactionTemplate">
        <bean class="org.springframework.transaction.support.TransactionTemplate">
            <property name="transactionManager" ref="sys.tx.mngr" />
        </bean>
    </property>
</bean>

<bean id="sys.tx.mngr" class="org.springframework.orm.jpa.JpaTransactionManager">
    <property name="entityManagerFactory" ref="emf"/>
</bean>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM