有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

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) 个答案

  1. # 1 楼答案

    基于Transactional注释,您似乎正在使用Spring的事务管理。在这种情况下,尝试通过em.getTransaction()手动控制事务是没有意义的。此外,我不知道如何获得EntityManager,但这也可能会干扰Spring的事务管理

    要么坚持声明性事务管理的Spring方式(IMHO更好的主意),要么删除TransactionalEntityManager注入,自己管理PU和事务