Not allowed to create transaction on shared EntityManager - use Spring transactions or EJB CMT

自作多情 提交于 2019-11-30 04:28:30
Siddhartha Negi

I guess the problem here is that although you have defined the bean for the transaction manager, you haven't annotated the create() method with @Transactional which enables spring transactions.

Also remove the entityManager.getTransaction().commit(); statement as now all the transaction management will be handled by spring, if you leave the statement as it is then you will get the same error again.

Injecting EntityManagerFactory instead of EntityManager and javax.transaction.Transactional annotation on method solved my issue as shown below.

//Autowire EntityManagerFactory
@PersistenceUnit(unitName = "readwrite.config")
private EntityManagerFactory entityManagerFactory;


//Use below code on create/update
EntityManager entityManager = entityManagerFactory.createEntityManager();

entityManager.getTransaction().begin();
if (!ObjectUtils.isEmpty(entity) && !entityManager.contains(entity)) {
   entityManager.persist(entity);
   entityManager.flush();
}
entityManager.getTransaction().commit();
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!