0

I'm use JPA (Hibernate 4 vendor) and Spring 3.2.x. I use this code for get Session and re-attach my detached entity.

Session session = entityManager.unwrap(Session.class);

My code look like this :

@Service
public class SchedulerServiceImpl implements SchedulerService {
    @PersistenceContext
    private EntityManager entityManager;

    @Override
    @Transactional
    @Scheduled(fixedDelay = 5000)
    public void executeTasks() { 
       .. code ..
       while (tasksIterator.hasNext()) {

            SchedulerTask readyTask = recalculation(currentTask);
      }
      .. code ...
    }

   @Override
   @Transactional(readOnly = false, propagation = Propagation.REQUIRES_NEW)
    public Deposit recalculation(SchedulerTask schedulerTask) {
         boolean asda = entityManager.isOpen(); // get TRUE
         Session session = entityManager.unwrap(Session.class); // Exception here
         session.update(schedulerTask);
         ... code ...
  }
}

What's wrong?

error :

   00:21:52,180 ERROR [org.springframework.scheduling.support.TaskUtils$LoggingErrorHandler]
 (pool-10-thread-1) Unexpected error occurred in scheduled task.:
 java.lang.IllegalStateException: No transactional EntityManager
 available
        at org.springframework.orm.jpa.SharedEntityManagerCreator$SharedEntityManagerInvocationHandler.invokeSharedEntityManagerCreator.java:224)
 [spring-orm-3.2.4.RELEASE.jar:3.2.4.RELEASE]
        at com.sun.proxy.$Proxy36.unwrap(Unknown Source)
        at com.jar.dom.service.SchedulerServiceImpl.recalculation(SchedulerServiceImpl.java:133)
 [classes:]
        at com.jar.dom.service.SchedulerServiceImpl.executeTasks(SchedulerServiceImpl.java:92)
 [classes:]
Benjamin
  • 531
  • 2
  • 6
  • 18
  • if i move recalculation method in other class all work fine but why? – Benjamin Oct 20 '13 at 11:01
  • 1
    Have a look here: http://stackoverflow.com/questions/3423972/spring-transaction-method-call-by-the-method-within-the-same-class-does-not-wo – Alan Hay Oct 20 '13 at 11:17

1 Answers1

0

I solved this adding this lines on spring configuration

@Configuration
@EnableTransactionManagement
@EnableJpaRepositories("com.blablabla")
class ApplicationConfig {
     ....
}
Federico Traiman
  • 1,191
  • 1
  • 13
  • 18