Search in sources :

Example 1 with EntityTransaction

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

Example 2 with EntityTransaction

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

Example 3 with EntityTransaction

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();
}
Also used : EntityTransaction(jakarta.persistence.EntityTransaction) EntityManager(jakarta.persistence.EntityManager) DefaultTransactionAttribute(org.springframework.transaction.interceptor.DefaultTransactionAttribute) EntityManagerFactory(jakarta.persistence.EntityManagerFactory) TransactionStatus(org.springframework.transaction.TransactionStatus) Test(org.junit.jupiter.api.Test)

Example 4 with EntityTransaction

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();
}
Also used : EntityTransaction(jakarta.persistence.EntityTransaction) EntityManager(jakarta.persistence.EntityManager) OptimisticLockingFailureException(org.springframework.dao.OptimisticLockingFailureException) DefaultTransactionAttribute(org.springframework.transaction.interceptor.DefaultTransactionAttribute) EntityManagerFactory(jakarta.persistence.EntityManagerFactory) OptimisticLockException(jakarta.persistence.OptimisticLockException) TransactionStatus(org.springframework.transaction.TransactionStatus) Test(org.junit.jupiter.api.Test)

Aggregations

EntityManager (jakarta.persistence.EntityManager)4 EntityTransaction (jakarta.persistence.EntityTransaction)4 Test (org.junit.jupiter.api.Test)4 EntityManagerFactory (jakarta.persistence.EntityManagerFactory)3 TransactionStatus (org.springframework.transaction.TransactionStatus)3 TransactionDefinition (org.springframework.transaction.TransactionDefinition)2 DefaultTransactionAttribute (org.springframework.transaction.interceptor.DefaultTransactionAttribute)2 OptimisticLockException (jakarta.persistence.OptimisticLockException)1 RollbackException (jakarta.persistence.RollbackException)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)1 Assertions.assertThatExceptionOfType (org.assertj.core.api.Assertions.assertThatExceptionOfType)1 AfterEach (org.junit.jupiter.api.AfterEach)1 BeforeEach (org.junit.jupiter.api.BeforeEach)1 BDDMockito.given (org.mockito.BDDMockito.given)1 BDDMockito.willThrow (org.mockito.BDDMockito.willThrow)1 Mockito.mock (org.mockito.Mockito.mock)1 Mockito.times (org.mockito.Mockito.times)1 Mockito.verify (org.mockito.Mockito.verify)1