我正在使用JPA(Hibernate 4供应商)和Spring 3.2.x。我使用这段代码来获取Session并重新附加分离实体。
Session session = entityManager.unwrap(Session.class);
我的代码看起来像这样:
@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 ...
}
}
你怎么了?
错误:
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:]
1条答案
按热度按时间mkshixfv1#
我通过在Spring配置中添加这几行代码解决了这个问题