1

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?

Sam
  • 6,770
  • 7
  • 50
  • 91

1 Answers1

1

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>
Community
  • 1
  • 1
Avis
  • 2,197
  • 18
  • 28
  • Thank. Isn't there exist simple solution? I edited my question. – Sam Sep 22 '15 at 15:47
  • Define your own transaction manager, override these determineTimeout(..), use a threadlocal that sets the timeout based on a custom annotation, or AOP, several ways to achieve it. – kisna Mar 30 '16 at 02:35