Search in sources :

Example 26 with EntityManager

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);
}
Also used : EntityManager(jakarta.persistence.EntityManager) Test(org.junit.jupiter.api.Test)

Example 27 with EntityManager

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");
}
Also used : EntityManager(jakarta.persistence.EntityManager) Test(org.junit.jupiter.api.Test)

Example 28 with EntityManager

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);
}
Also used : EntityManager(jakarta.persistence.EntityManager) Test(org.junit.jupiter.api.Test)

Example 29 with EntityManager

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");
}
Also used : EntityManager(jakarta.persistence.EntityManager) Person(org.springframework.orm.jpa.domain.Person) Test(org.junit.jupiter.api.Test)

Example 30 with EntityManager

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();
}
Also used : BeforeEach(org.junit.jupiter.api.BeforeEach) TransactionCallbackWithoutResult(org.springframework.transaction.support.TransactionCallbackWithoutResult) BDDMockito.willThrow(org.mockito.BDDMockito.willThrow) TransactionDefinition(org.springframework.transaction.TransactionDefinition) RollbackException(jakarta.persistence.RollbackException) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) EntityTransaction(jakarta.persistence.EntityTransaction) Mockito.times(org.mockito.Mockito.times) InvalidIsolationLevelException(org.springframework.transaction.InvalidIsolationLevelException) EntityManagerFactory(jakarta.persistence.EntityManagerFactory) TransactionSynchronizationManager(org.springframework.transaction.support.TransactionSynchronizationManager) ArrayList(java.util.ArrayList) Mockito.verify(org.mockito.Mockito.verify) Test(org.junit.jupiter.api.Test) List(java.util.List) AfterEach(org.junit.jupiter.api.AfterEach) TransactionSystemException(org.springframework.transaction.TransactionSystemException) TransactionSynchronization(org.springframework.transaction.support.TransactionSynchronization) BDDMockito.given(org.mockito.BDDMockito.given) EntityManager(jakarta.persistence.EntityManager) TransactionTemplate(org.springframework.transaction.support.TransactionTemplate) TransactionStatus(org.springframework.transaction.TransactionStatus) Assertions.assertThatExceptionOfType(org.assertj.core.api.Assertions.assertThatExceptionOfType) Mockito.mock(org.mockito.Mockito.mock) EntityTransaction(jakarta.persistence.EntityTransaction) EntityManager(jakarta.persistence.EntityManager) TransactionSynchronization(org.springframework.transaction.support.TransactionSynchronization) Test(org.junit.jupiter.api.Test)

Aggregations

EntityManager (jakarta.persistence.EntityManager)55 Test (org.junit.jupiter.api.Test)52 EntityManagerFactory (jakarta.persistence.EntityManagerFactory)25 Query (jakarta.persistence.Query)9 RootBeanDefinition (org.springframework.beans.factory.support.RootBeanDefinition)9 TransactionRequiredException (jakarta.persistence.TransactionRequiredException)7 Person (org.springframework.orm.jpa.domain.Person)7 StoredProcedureQuery (jakarta.persistence.StoredProcedureQuery)6 Properties (java.util.Properties)5 GenericApplicationContext (org.springframework.context.support.GenericApplicationContext)5 EntityManagerHolder (org.springframework.orm.jpa.EntityManagerHolder)5 EntityTransaction (jakarta.persistence.EntityTransaction)4 HashMap (java.util.HashMap)4 DefaultListableBeanFactory (org.springframework.beans.factory.support.DefaultListableBeanFactory)4 ExpectedLookupTemplate (org.springframework.context.testfixture.jndi.ExpectedLookupTemplate)4 TransactionStatus (org.springframework.transaction.TransactionStatus)4 NoResultException (jakarta.persistence.NoResultException)3 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)3 AfterEach (org.junit.jupiter.api.AfterEach)3 BeforeEach (org.junit.jupiter.api.BeforeEach)3