【问题标题】:Cannot use an EntityTransaction while using JTA. Using non-jta使用 JTA 时无法使用 EntityTransaction。使用非 jta
【发布时间】:2020-11-22 10:16:32
【问题描述】:

我正在尝试执行 CRUD,并且我的 persistence.xml 具有非 JTA 数据源,但是当我尝试执行创建、更新或删除之类的操作时,我收到一条消息:使用时无法使用 EntityTransaction JTA

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>

【问题讨论】:

    标签: java jpa jta


    【解决方案1】:

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

    要么坚持 Spring 的声明式事务管理方式(恕我直言,更好的主意),要么移除 TransactionalEntityManager 注入并自己管理 PU 和事务。

    【讨论】:

      猜你喜欢
      • 2012-06-10
      • 1970-01-01
      • 1970-01-01
      • 2015-05-12
      • 2014-09-07
      • 1970-01-01
      • 2012-06-03
      • 1970-01-01
      • 2016-07-18
      相关资源
      最近更新 更多