Search in sources :

Example 6 with EntityTransaction

use of javax.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(javax.persistence.EntityTransaction) EntityManager(javax.persistence.EntityManager) DefaultTransactionDefinition(org.springframework.transaction.support.DefaultTransactionDefinition) Test(org.junit.Test)

Example 7 with EntityTransaction

use of javax.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);
    assertTrue(!TransactionSynchronizationManager.hasResource(factory));
    assertTrue(!TransactionSynchronizationManager.isSynchronizationActive());
    tt.execute(new TransactionCallback() {

        @Override
        public Object doInTransaction(TransactionStatus status) {
            EntityManagerFactoryUtils.getTransactionalEntityManager(factory).flush();
            TransactionSynchronizationManager.registerSynchronization(new TransactionSynchronizationAdapter() {

                @Override
                public void afterCompletion(int status) {
                    tt.execute(new TransactionCallback() {

                        @Override
                        public Object doInTransaction(TransactionStatus status) {
                            EntityManagerFactoryUtils.getTransactionalEntityManager(factory).flush();
                            return null;
                        }
                    });
                }
            });
            return null;
        }
    });
    assertTrue(!TransactionSynchronizationManager.hasResource(factory));
    assertTrue(!TransactionSynchronizationManager.isSynchronizationActive());
    verify(tx).commit();
    verify(tx2).begin();
    verify(tx2).commit();
    verify(manager).flush();
    verify(manager).close();
    verify(manager2).flush();
    verify(manager2).close();
}
Also used : EntityTransaction(javax.persistence.EntityTransaction) EntityManager(javax.persistence.EntityManager) TransactionCallback(org.springframework.transaction.support.TransactionCallback) TransactionStatus(org.springframework.transaction.TransactionStatus) TransactionSynchronizationAdapter(org.springframework.transaction.support.TransactionSynchronizationAdapter) Test(org.junit.Test)

Example 8 with EntityTransaction

use of javax.persistence.EntityTransaction in project Truck-Factor by aserg-ufmg.

the class GenericDAO method remove.

public void remove(T o) {
    EntityTransaction tx = this.em.getTransaction();
    try {
        tx.begin();
        this.em.remove(o);
        tx.commit();
    } catch (RuntimeException e) {
        if (tx != null && tx.isActive())
            tx.rollback();
        throw e;
    } finally {
        this.em.clear();
    }
}
Also used : EntityTransaction(javax.persistence.EntityTransaction)

Example 9 with EntityTransaction

use of javax.persistence.EntityTransaction in project Truck-Factor by aserg-ufmg.

the class GenericDAO method persist.

public void persist(T o) {
    EntityTransaction tx = this.em.getTransaction();
    try {
        tx.begin();
        this.em.persist(o);
        tx.commit();
    } catch (RuntimeException e) {
        if (tx != null && tx.isActive())
            tx.rollback();
        throw e;
    }
}
Also used : EntityTransaction(javax.persistence.EntityTransaction)

Example 10 with EntityTransaction

use of javax.persistence.EntityTransaction in project Truck-Factor by aserg-ufmg.

the class GenericDAO method merge.

public void merge(T o) {
    EntityTransaction tx = this.em.getTransaction();
    try {
        tx.begin();
        this.em.merge(o);
        tx.commit();
    } catch (RuntimeException e) {
        if (tx != null && tx.isActive())
            tx.rollback();
        throw e;
    } finally {
        this.em.clear();
    }
}
Also used : EntityTransaction(javax.persistence.EntityTransaction)

Aggregations

EntityTransaction (javax.persistence.EntityTransaction)56 EntityManager (javax.persistence.EntityManager)36 Test (org.junit.Test)12 EntityManagerFactory (javax.persistence.EntityManagerFactory)9 Query (javax.persistence.Query)8 UserTestDbo (org.webpieces.plugins.hibernate.app.dbo.UserTestDbo)6 InumSqlEntry (org.gluu.oxtrust.model.sql.InumSqlEntry)4 Person (org.hibernate.userguide.model.Person)3 TransactionStatus (org.springframework.transaction.TransactionStatus)3 Transactional (com.google.inject.persist.Transactional)2 Priority (org.hibernate.envers.test.Priority)2 DefaultTransactionAttribute (org.springframework.transaction.interceptor.DefaultTransactionAttribute)2 UserDbo (WEBPIECESxPACKAGE.base.libs.UserDbo)1 UserRole (WEBPIECESxPACKAGE.base.libs.UserRole)1 FileType (aserg.gtf.model.FileType)1 Annotation (java.lang.annotation.Annotation)1 ArrayList (java.util.ArrayList)1 Date (java.util.Date)1 HashSet (java.util.HashSet)1 Set (java.util.Set)1