EntityManager em = _em; //acquired from OpenJPA em.getTransaction().begin(); Connection conn; try { OpenJPAEntityManager oem = (OpenJPAEntityManager) em.getDelegate(); conn = oem.getConnection(); PreparedStatement ps = conn.prepareStatement(myStatement); //initialize parameters in ps ps.executeUpdate(); em.merge(myJpaObject); em.getTransaction().commit(); } finally { if (ps != null && !ps.isClosed()) { ps.close(); } if (em.getTransaction().isActive()) { em.getTransaction().rollback(); } }
谢谢!
解决方法
值得注意的是what the OpenJPA documentation has to say about it:
The returned connection is only guaranteed to be transactionally consistent with other EntityManager operations if the EntityManager is in a managed or non-optimistic transaction,if the EntityManager has flushed in the current transaction,or if you have used the OpenJPAEntityManager.beginStore method to ensure that a datastore transaction is in progress. Always close the returned connection before attempting any other EntityManager operations. OpenJPA will ensure that the underlying native connection is not released if a datastore transaction is in progress.
您的交易不受管理,但我认为这不是乐观的.此外,在启动事务和执行JDBC操作之间,您还没有完成任何JPA工作,因此我认为您可能属于“如果EntityManager在当前事务中已刷新”的情况.
你没有做的一件事就是在继续使用JPA之前关闭连接.我假设OpenJPA没有返回它正在使用的实际连接,但是在OpenJPA恢复工作之前应该关闭它的一些包装器.