use of jakarta.persistence.EntityManager in project spring-framework by spring-projects.
the class ContainerManagedEntityManagerIntegrationTests method testRollbackOccurs.
@Test
public void testRollbackOccurs() {
EntityManager em = createContainerManagedEntityManager();
doInstantiateAndSave(em);
// Should rollback
endTransaction();
assertThat(countRowsInTable(em, "person")).as("Tx must have been rolled back").isEqualTo(0);
}
use of jakarta.persistence.EntityManager in project spring-framework by spring-projects.
the class ContainerManagedEntityManagerIntegrationTests method testCommitOccurs.
@Test
public void testCommitOccurs() {
EntityManager em = createContainerManagedEntityManager();
doInstantiateAndSave(em);
setComplete();
// Should rollback
endTransaction();
assertThat(countRowsInTable(em, "person")).as("Tx must have committed back").isEqualTo(1);
// Now clean up the database
deleteFromTables("person");
}
use of jakarta.persistence.EntityManager in project spring-framework by spring-projects.
the class ContainerManagedEntityManagerIntegrationTests method testInstantiateAndSave.
@Test
public void testInstantiateAndSave() {
EntityManager em = createContainerManagedEntityManager();
doInstantiateAndSave(em);
}
use of jakarta.persistence.EntityManager in project spring-framework by spring-projects.
the class ContainerManagedEntityManagerIntegrationTests method testReuseInNewTransaction.
@Test
public void testReuseInNewTransaction() {
EntityManager em = createContainerManagedEntityManager();
doInstantiateAndSave(em);
endTransaction();
// assertFalse(em.getTransaction().isActive());
startNewTransaction();
// Call any method: should cause automatic tx invocation
assertThat(em.contains(new Person())).isFalse();
// assertTrue(em.getTransaction().isActive());
doInstantiateAndSave(em);
setComplete();
// Should rollback
endTransaction();
assertThat(countRowsInTable(em, "person")).as("Tx must have committed back").isEqualTo(1);
// Now clean up the database
deleteFromTables("person");
}
use of jakarta.persistence.EntityManager 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();
}
Aggregations