1
votes

We have a setup that provides problems for me understand. We have a JTA Transaction meaning that we use a UserTransaction along with a EntityManagerFactory. It seams that once I create a EntityManager instance using the factory and calling a operation, the user transaction seams to have an automatic begin and this transaction is shared among any entity manager.

That leads to a lot of problems when writing proper tests. Is there a switch where I can switch off this automatically start transactions automatically rather than require userTransaction.begin() before actually using the entity manager.

1
that is what the JPA specs an implementation should do. You would then need your JPA provider to allow it to be turned off. I know DataNucleus JPA does, but no idea about others. Consult the docs of your provider - Neil Stockton

1 Answers

0
votes

Is the transactional behaviour something you want to assert within your integration tests?

If not, you might want to provide an alternative persistence.xml in /src/test/resources.

Instead of:

<persistence-unit name="myPU" transaction-type="JTA">
    <jta-data-source>java:jboss/datasources/myXADS</jta-data-source>
    ...
</persistence-unit>

It would contain:

<persistence-unit name="it" transaction-type="RESOURCE_LOCAL">
    <provider>org.hibernate.ejb.HibernatePersistence</provider>
    ...
</persistence-unit>

Now you can manage the transactions manually. E.g. with the help of a JUnit 4 test rule like:

import javax.persistence.EntityManager;
import javax.persistence.EntityTransaction;
import javax.persistence.Persistence;
import javax.persistence.PersistenceException;

import org.apache.commons.lang3.exception.ExceptionUtils;
import org.assertj.core.api.Assertions;
import org.junit.rules.TestRule;
import org.junit.runner.Description;
import org.junit.runners.model.Statement;

public class EntityManagerProvider implements TestRule {

    private EntityManager em;
    private EntityTransaction tx;

    public static EntityManagerProvider forPersistenceUnit(final String unitName) {
        return new EntityManagerProvider(unitName);
    }

    public EntityManager em() {
        return em;
    }

    public EntityTransaction tx() {
        return tx;
    }

    @Override
    public Statement apply(final Statement base, final Description description) {
        return new Statement() {
            @Override
            public void evaluate() throws Throwable {
                base.evaluate();
                em().clear();
            }
        };
    }

    private EntityManagerProvider(final String unitName) {
        try {
            em = Persistence.createEntityManagerFactory(unitName).createEntityManager();
            tx = em().getTransaction();
        } catch (final PersistenceException e) {
            Assertions.fail(ExceptionUtils.getRootCause(e).getMessage(), e);
        }
    }
}

The test rule in action:

import static test.rules.EntityManagerProvider.*;

import org.junit.Rule;
import org.junit.Test;

import test.rules.EntityManagerProvider;

public class MyIT {

    @Rule
    public final EntityManagerProvider provider = forPersistenceUnit("it");

    @Test
    public void testPersist() {
        provider.tx().begin();
        provider.em().persist(new MyEntity());
        provider.tx().commit();
    }
}

A more complex scenario that needs to deal with multiple entity managers (only shown in parts here):

public static class ConcurrentChange extends EntityTestBase<Issue> {

    @Rule
    public EntityManagerProvider client1 = forPersistenceUnit("it");

    @Rule
    public EntityManagerProvider client2 = forPersistenceUnit("it");

    @Rule
    public final ExpectedException thrown = ExpectedException.none();

    @Test
    public void shouldRaiseOptimisticLockException() {

        assertThat(client2.em(), is(not(equalTo(client1.em()))));

        final MyEntity entity = persistBy(newInstance(), client1);

        entity.setName("early update");
        persistBy(entity, client2);

        expectOptimisticLockException();

        entity.setName("late update");
        persistBy(entity, client1);
    }

    private void expectOptimisticLockException() {
        thrown.expect(instanceOf(RollbackException.class));
        thrown.expectCause(instanceOf(OptimisticLockException.class));
    }
}


public abstract class EntityTestBase<E> {

    protected E persistBy(final E entity, final EntityManagerProvider client) {
        client.tx().begin();
        try {
            final E mergedEntity = client.em().merge(entity);
            client.tx().commit();
            return mergedEntity;
        } catch (final PersistenceException e) {
            if (client.tx().isActive()) {
                client.tx().rollback();
            }
            throw e;
        }
    }

    // ...
}