使用JTA和JPA EntityManager

时间:2017-03-07 14:10:51

标签: java jpa transactions

我们有一个设置可以让我理解。我们有一个JTA事务,意味着我们使用UserTransaction和EntityManagerFactory。它接缝一旦我使用工厂创建一个EntityManager实例并调用一个操作,用户事务接缝就会自动开始,这个事务在任何实体管理器之间共享。

在编写适当的测试时会导致很多问题。是否有一个开关,我可以自动关闭它自动启动事务,而不是在实际使用实体管理器之前需要userTransaction.begin()。

1 个答案:

答案 0 :(得分:0)

您希望在集成测试中断言事务行为吗?

如果没有,您可能希望在persistence.xml中提供替代/src/test/resources

而不是:

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

它将包含:

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

现在您可以手动管理交易。例如。借助JUnit 4测试规则,例如:

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

测试规则:

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

需要处理多个实体经理的更复杂的场景(仅在此处显示):

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

    // ...
}