1

我想请你帮忙解决以下问题。我有方法:

String sql = "INSERT INTO table ...."
Query query = em.createNativeQuery(sql);
query.executeUpdate();
sql = "SELECT max(id) FROM ......";
query = em.createNativeQuery(sql);
Integer importId = ((BigDecimal) query.getSingleResult()).intValue();

for (EndurDealItem item : deal.getItems()) {
        String sql2 = "INSERT INTO another_table";
        em.createNativeQuery(sql2).executeUpdate();
    }

并且在执行它之后,数据不会被提交(大约需要 10 或 15 分钟才能提交数据)。有什么方法可以显式提交数据或触发提交?是什么导致交易在这么长时间内保持未提交状态?

我们使用 nativeQueries 的原因是,我们在某个共享接口上导出数据,并且我们不再使用这些数据。

我想提一下,该事务是容器管理的(由 Geronimo 提供)。EntityManager 是通过链接创建的:

 @PersistenceContext(unitName = "XXXX", type = PersistenceContextType.TRANSACTION)
 private EntityManager em;
4

1 回答 1

0

Use explicitly the transaction commit:

EntityManager em = /* get an entity manager */;
em.getTransaction().begin();
// make some changes
em.getTransaction().commit();

This should work. The time of execution of all operation between .begin() and .end() depends of course also from the cycle you're performing, the number of row you're inserting, from the position of the database (the speed of the network matters) and so on...

于 2013-06-18T08:26:33.057 回答