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