简体   繁体   中英

Delay during commit (using JPA/JTA)

I would like to ask you for help with following problem. I have method:

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();
    }

And after executing it, data are not commited (it takes like 10 or 15 minutes until data are commited). Is there any way how to commit data explicitly or trigger commit? And what causes the transaction to remain uncommited for such a long time?

The reason we use nativeQueries is, that we are exporting data on some shared interface and we are not using the data anymore.

I would like to mention, that the transaction is Container-Managed (by Geronimo). EntityManager is created via linking:

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

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...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM