2

I am writing one post service, to log my activities and as it is activity service it is calling 10 times per second so i have to control it by executing asynchronously, so i went for spring task executor and i am using spring transaction manager to get session, but if i call from task executor run method i am getting following exception.

Exception in thread "executorWithPoolSizeRange-1" org.hibernate.HibernateException: Could not obtain transaction-synchronized Session for current thread

you can check following my code

public void saveActivityLogs(final List<UserActivityEntity> activityLogs,final String clientIP,final int clientPort){
    logger.info("Saving activiytlogs");
    /*for(UserActivityEntity activitylog:activityLogs){
        activitylog.setClientIp(clientIP);
        activitylog.setClientPort(clientPort);
        this.commonDAO.saveActivityLogs(activitylog);
    }   */
    executorWithPoolSizeRange.execute(new Runnable() {
        public void run() {
            addActivityLogs(activityLogs,clientIP,clientPort);
        }   
    });
}
/**
    this method will call from above one 
 */
@Transactional(propagation = Propagation.REQUIRES_NEW)
public void addActivityLogs(List<UserActivityEntity> activityLogs,String clientIP,int clientPort){
    for(UserActivityEntity activitylog:activityLogs){
        activitylog.setClientIp(clientIP);
        activitylog.setClientPort(clientPort);
        this.commonDAO.saveActivityLogs(activitylog);
    }   

}
Balaji
  • 151
  • 4
  • 15

1 Answers1

0

If you call 'addActivityLogs' method in such a way you are basically omitting Spring Transactions.

Please, check out my answer to a similar question: Spring @Transactional TransactionRequiredException or RollbackException

Long story short: try to create another bean with your 'addActivityLogs' method and inject this bean into the one that runs your async tasks. It should work then.

Community
  • 1
  • 1
Rafal G.
  • 4,252
  • 1
  • 25
  • 41
  • 2
    I am not able to access injected bean inside of run method,if i put whole logic in run method then also i am getting same exception – Balaji Nov 18 '15 at 12:18