Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Aren't multiple transactions supposed to concur with em.getTransaction()?

When I execute:

public void beginTransaction() {
    em.getTransaction().begin();
}

following an active transaction started in the same way, I get the following exception:

Exception Description: Transaction is currently active
java.lang.IllegalStateException: 
Exception Description: Transaction is currently active
        at org.eclipse.persistence.internal.jpa.transaction.EntityTransactionImpl.begin(EntityTransactionImpl.java:45)
        at com.mysimpatico.memoplatform.persistence.Database.beginTransaction(Database.java:44)
        at com.mysimpatico.memoplatform.persistence.tests.PersistenceTest.setUp(PersistenceTest.java:39)

The underlying DBMS is embedded apache derby which to my knowledge doesn't support multiple connections from different JVMS, but otherwise supports concurrent transactions.Documentation

This test code fails:

public <Entity> int getNo(final Class<Entity> entityClass) {
    final CriteriaBuilder qb = em.getCriteriaBuilder();
    final CriteriaQuery<Long> cq = qb.createQuery(Long.class);
    cq.select(qb.count(cq.from(entityClass)));
    final int ret = em.createQuery(cq).getSingleResult().intValue();
    return ret;
}

protected void insertWords(final List<String[]> expressionDefTriples, MUser usr, final MeaningsDatabase mengsDB) throws Exception {
    for (String[] expressionDefTriple : expressionDefTriples) {
        mengsDB.persistSynonyms(MeaningsDatabase.toEnumMap(expressionDefTriple, usr));
        testSynonymsPersistance(expressionDefTriple,mengsDB);
    }
}

protected void testSynonymsPersistance(final String[] expDefTriple, final MeaningsDatabase mengsDB) {
    final Expression exp = mengsDB.get(Expression.class, expDefTriple[0]);
    testGender(exp, expDefTriple[2]);
    final Expression def = mengsDB.get(Expression.class, expDefTriple[1]);
    assertNotNull(def);
    assertTrue(exp.getMengs().size() > 0);
    assertTrue(def.getMengs().size() > 0);
    assertTrue(mengsDB.getNo(Meaning.class) > 0); //fails here
}

public void persistSynonyms(final EnumMap<Input, MemoEntity> input) throws MultipleMengsException {
    final Expression def = extractExp(input, Input.definition);
    final Expression exp = extractExp(input, Input.expression);
    final MUser usr = extractUsr(input);

    beginTransaction();
    final Meaning meng = persistMeng(exp, usr, def);
    commitTransaction();
    signalContentAvailability();
    logInsertion(meng, exp, def);
}

Essentially I start and commit a transaction within the same em, but then when I come to execute the getNo() method using the same em, it doesn't find what has been committed by the previous. I don't see where's the problem. There's clearly an issue since 'local' code like em.find(..) finds the committed changes, but not the criteria query.

like image 294
simpatico Avatar asked Oct 23 '10 05:10

simpatico


1 Answers

EntityManager#getTransaction() actually returns the current resource local transaction and you cannot call EntityTransaction#begin() when a transaction is already active:

/**
* Start a resource transaction.
* @throws IllegalStateException if isActive() is true
*/
public void begin();

What you can do is create serial local transactions as hinted in the javadoc of EntityManager#getTransaction():

/**
 * Return the resource-level <code>EntityTransaction</code> object.
 * The <code>EntityTransaction</code> instance may be used serially to
 * begin and commit multiple transactions.
 * @return EntityTransaction instance
 * @throws IllegalStateException if invoked on a JTA
 *         entity manager
 */
public EntityTransaction getTransaction();

To sum up, JPA (and JTA) doesn't support nested transactions.

References

  • JPA 2.0 specification
    • Section 3.1.1 "EntityManager Interface"
    • Section 7.5.3 "The EntityTransaction Interface"
  • JPA wiki book
    • Resource Local Transactions
    • Nested Transactions
like image 183
Pascal Thivent Avatar answered Nov 02 '22 14:11

Pascal Thivent