我正在使用 JPA Toplink-essential 并开发 RESTful Web 应用程序。
这里首先要提一件事。
不使用 JTA
所以我的persistences.xml 被定义为不使用JTA。
<persistence-unit name="kojoPU">
<provider>oracle.toplink.essentials.PersistenceProvider</provider>
<non-jta-data-source>machinePrototype</non-jta-data-source>
这允许实体管理器不立即使用 executeUpdate() 执行查询,它将等待直到提交。
em.getTransaciton().begin();
Query query = em.createNativeQuery("DELETE FROM table1 WHERE theId = 10;");
query.executeUpdate(); //not yet executed until transaction is commited.
//continue do something...
em.getTransaction().commit(); //the query above is executed here finally
但是有一个大问题,在transaction.begin()之后,有没有像JSONException
or这样的错误indexOutOfBoundsException
,事务没有关闭,并保持打开一段时间。
在这种情况下是否可以以某种方式强制关闭交易?