java在使用JTA时不能使用EntityTransaction。使用非JTA
我正在努力做一个积垢和我的坚持。xml有非JTA数据源,但当我尝试创建、更新或删除之类的操作时,我收到一条消息,上面说:在使用JTA时不能使用EntityTransaction
muy代码的一个示例:
@Transactional
public void destroy(T entity) throws Exception
{
EntityManager em = getEntityManager();
try
{
em.getTransaction().begin();
em.remove(em.merge(entity));
em.getTransaction().commit();
}
catch(Exception e)
{
em.getTransaction().rollback();
throw new Exception(e);
}
finally
{
if (em.isOpen())
{
em.close();
}
}
}
我的坚持:
<persistence-unit name="namePU" transaction-type="RESOURCE_LOCAL">
<non-jta-data-source>database</non-jta-data-source>
<exclude-unlisted-classes>false</exclude-unlisted-classes>
<properties/>
</persistence-unit>
# 1 楼答案
基于
Transactional
注释,您似乎正在使用Spring的事务管理。在这种情况下,尝试通过em.getTransaction()
手动控制事务是没有意义的。此外,我不知道如何获得EntityManager
,但这也可能会干扰Spring的事务管理要么坚持声明性事务管理的Spring方式(IMHO更好的主意),要么删除
Transactional
和EntityManager
注入,自己管理PU和事务