use of jakarta.persistence.EntityTransaction in project spring-framework by spring-projects.
the class JpaTransactionManagerTests method testTransactionWithRequiresNewInAfterCompletion.
@Test
public void testTransactionWithRequiresNewInAfterCompletion() {
tt.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRES_NEW);
EntityManager manager2 = mock(EntityManager.class);
EntityTransaction tx2 = mock(EntityTransaction.class);
given(manager.getTransaction()).willReturn(tx);
given(factory.createEntityManager()).willReturn(manager, manager2);
given(manager2.getTransaction()).willReturn(tx2);
given(manager2.isOpen()).willReturn(true);
boolean condition3 = !TransactionSynchronizationManager.hasResource(factory);
assertThat(condition3).isTrue();
boolean condition2 = !TransactionSynchronizationManager.isSynchronizationActive();
assertThat(condition2).isTrue();
tt.execute(status -> {
EntityManagerFactoryUtils.getTransactionalEntityManager(factory).flush();
TransactionSynchronizationManager.registerSynchronization(new TransactionSynchronization() {
@Override
public void afterCompletion(int status) {
tt.execute(status1 -> {
EntityManagerFactoryUtils.getTransactionalEntityManager(factory).flush();
return null;
});
}
});
return null;
});
boolean condition1 = !TransactionSynchronizationManager.hasResource(factory);
assertThat(condition1).isTrue();
boolean condition = !TransactionSynchronizationManager.isSynchronizationActive();
assertThat(condition).isTrue();
verify(tx).commit();
verify(tx2).begin();
verify(tx2).commit();
verify(manager).flush();
verify(manager).close();
verify(manager2).flush();
verify(manager2).close();
}
use of jakarta.persistence.EntityTransaction in project spring-framework by spring-projects.
the class DefaultJpaDialectTests method testDefaultBeginTransaction.
@Test
public void testDefaultBeginTransaction() throws Exception {
TransactionDefinition definition = new DefaultTransactionDefinition();
EntityManager entityManager = mock(EntityManager.class);
EntityTransaction entityTx = mock(EntityTransaction.class);
given(entityManager.getTransaction()).willReturn(entityTx);
dialect.beginTransaction(entityManager, definition);
}
use of jakarta.persistence.EntityTransaction in project spring-framework by spring-projects.
the class LocalContainerEntityManagerFactoryBeanTests method testApplicationManagedEntityManagerWithTransaction.
@Test
public void testApplicationManagedEntityManagerWithTransaction() throws Exception {
Object testEntity = new Object();
EntityTransaction mockTx = mock(EntityTransaction.class);
// This one's for the tx (shared)
EntityManager sharedEm = mock(EntityManager.class);
given(sharedEm.getTransaction()).willReturn(new NoOpEntityTransaction());
// This is the application-specific one
EntityManager mockEm = mock(EntityManager.class);
given(mockEm.getTransaction()).willReturn(mockTx);
given(mockEmf.createEntityManager()).willReturn(sharedEm, mockEm);
LocalContainerEntityManagerFactoryBean cefb = parseValidPersistenceUnit();
JpaTransactionManager jpatm = new JpaTransactionManager();
jpatm.setEntityManagerFactory(cefb.getObject());
TransactionStatus txStatus = jpatm.getTransaction(new DefaultTransactionAttribute());
EntityManagerFactory emf = cefb.getObject();
assertThat(cefb.getObject()).as("EntityManagerFactory reference must be cached after init").isSameAs(emf);
assertThat(emf).as("EMF must be proxied").isNotSameAs(mockEmf);
EntityManager em = emf.createEntityManager();
em.joinTransaction();
assertThat(em.contains(testEntity)).isFalse();
jpatm.commit(txStatus);
cefb.destroy();
verify(mockTx).begin();
verify(mockTx).commit();
verify(mockEm).contains(testEntity);
verify(mockEmf).close();
}
use of jakarta.persistence.EntityTransaction in project spring-framework by spring-projects.
the class LocalContainerEntityManagerFactoryBeanTests method testApplicationManagedEntityManagerWithTransactionAndCommitException.
@Test
public void testApplicationManagedEntityManagerWithTransactionAndCommitException() throws Exception {
Object testEntity = new Object();
EntityTransaction mockTx = mock(EntityTransaction.class);
willThrow(new OptimisticLockException()).given(mockTx).commit();
// This one's for the tx (shared)
EntityManager sharedEm = mock(EntityManager.class);
given(sharedEm.getTransaction()).willReturn(new NoOpEntityTransaction());
// This is the application-specific one
EntityManager mockEm = mock(EntityManager.class);
given(mockEm.getTransaction()).willReturn(mockTx);
given(mockEmf.createEntityManager()).willReturn(sharedEm, mockEm);
LocalContainerEntityManagerFactoryBean cefb = parseValidPersistenceUnit();
JpaTransactionManager jpatm = new JpaTransactionManager();
jpatm.setEntityManagerFactory(cefb.getObject());
TransactionStatus txStatus = jpatm.getTransaction(new DefaultTransactionAttribute());
EntityManagerFactory emf = cefb.getObject();
assertThat(cefb.getObject()).as("EntityManagerFactory reference must be cached after init").isSameAs(emf);
assertThat(emf).as("EMF must be proxied").isNotSameAs(mockEmf);
EntityManager em = emf.createEntityManager();
em.joinTransaction();
assertThat(em.contains(testEntity)).isFalse();
assertThatExceptionOfType(OptimisticLockingFailureException.class).isThrownBy(() -> jpatm.commit(txStatus));
cefb.destroy();
verify(mockTx).begin();
verify(mockEm).contains(testEntity);
verify(mockEmf).close();
}
Aggregations