gpt4 book ai didi

java - 使用 JTA 和 JPA EntityManager 关闭自动事务启动

转载 作者:太空宇宙 更新时间:2023-11-04 11:42:30 27 4
gpt4 key购买 nike

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

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

最佳答案

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

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

而不是:

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

// ...
}

关于java - 使用 JTA 和 JPA EntityManager 关闭自动事务启动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42650484/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com