use of cn.taketoday.transaction.support.TransactionTemplate in project today-infrastructure by TAKETODAY.
the class PersistenceContextTransactionTests method setup.
@BeforeEach
public void setup() {
factory = mock(EntityManagerFactory.class);
manager = mock(EntityManager.class);
tx = mock(EntityTransaction.class);
JpaTransactionManager tm = new JpaTransactionManager(factory);
tt = new TransactionTemplate(tm);
given(factory.createEntityManager()).willReturn(manager);
given(manager.getTransaction()).willReturn(tx);
given(manager.isOpen()).willReturn(true);
bean = new EntityManagerHoldingBean();
@SuppressWarnings("serial") PersistenceAnnotationBeanPostProcessor pabpp = new PersistenceAnnotationBeanPostProcessor() {
@Override
protected EntityManagerFactory findEntityManagerFactory(@Nullable String unitName, String requestingBeanName) {
return factory;
}
};
pabpp.processDependencies(null, bean, "bean");
assertThat(TransactionSynchronizationManager.getResourceMap().isEmpty()).isTrue();
assertThat(TransactionSynchronizationManager.isSynchronizationActive()).isFalse();
}
use of cn.taketoday.transaction.support.TransactionTemplate in project today-infrastructure by TAKETODAY.
the class JpaTransactionManagerTests method testPropagationSupportsAndRequiresNew.
@Test
public void testPropagationSupportsAndRequiresNew() {
tt.setPropagationBehavior(TransactionDefinition.PROPAGATION_SUPPORTS);
given(manager.getTransaction()).willReturn(tx);
final List<String> l = new ArrayList<>();
l.add("test");
boolean condition3 = !TransactionSynchronizationManager.hasResource(factory);
assertThat(condition3).isTrue();
boolean condition2 = !TransactionSynchronizationManager.isSynchronizationActive();
assertThat(condition2).isTrue();
Object result = tt.execute(status -> {
assertThat(TransactionSynchronizationManager.hasResource(factory)).isFalse();
TransactionTemplate tt2 = new TransactionTemplate(tm);
tt2.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRES_NEW);
return tt2.execute(status1 -> {
EntityManagerFactoryUtils.getTransactionalEntityManager(factory).flush();
return l;
});
});
assertThat(result).isSameAs(l);
boolean condition1 = !TransactionSynchronizationManager.hasResource(factory);
assertThat(condition1).isTrue();
boolean condition = !TransactionSynchronizationManager.isSynchronizationActive();
assertThat(condition).isTrue();
verify(tx).commit();
verify(manager).flush();
verify(manager).close();
}
use of cn.taketoday.transaction.support.TransactionTemplate in project today-infrastructure by TAKETODAY.
the class JpaTransactionManagerTests method testPropagationSupportsAndRequiresNewAndEarlyAccess.
@Test
public void testPropagationSupportsAndRequiresNewAndEarlyAccess() {
tt.setPropagationBehavior(TransactionDefinition.PROPAGATION_SUPPORTS);
given(factory.createEntityManager()).willReturn(manager);
given(manager.getTransaction()).willReturn(tx);
given(manager.isOpen()).willReturn(true);
final List<String> l = new ArrayList<>();
l.add("test");
boolean condition3 = !TransactionSynchronizationManager.hasResource(factory);
assertThat(condition3).isTrue();
boolean condition2 = !TransactionSynchronizationManager.isSynchronizationActive();
assertThat(condition2).isTrue();
Object result = tt.execute(status -> {
EntityManagerFactoryUtils.getTransactionalEntityManager(factory);
assertThat(TransactionSynchronizationManager.hasResource(factory)).isTrue();
TransactionTemplate tt2 = new TransactionTemplate(tm);
tt2.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRES_NEW);
return tt2.execute(status1 -> {
EntityManagerFactoryUtils.getTransactionalEntityManager(factory).flush();
return l;
});
});
assertThat(result).isSameAs(l);
boolean condition1 = !TransactionSynchronizationManager.hasResource(factory);
assertThat(condition1).isTrue();
boolean condition = !TransactionSynchronizationManager.isSynchronizationActive();
assertThat(condition).isTrue();
verify(tx).commit();
verify(manager).flush();
verify(manager, times(2)).close();
}
use of cn.taketoday.transaction.support.TransactionTemplate in project today-infrastructure by TAKETODAY.
the class JdbcTransactionManagerTests method testPropagationNeverWithExistingTransaction.
@Test
public void testPropagationNeverWithExistingTransaction() throws Exception {
final TransactionTemplate tt = new TransactionTemplate(tm);
tt.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRES_NEW);
boolean condition2 = !TransactionSynchronizationManager.hasResource(ds);
assertThat(condition2).as("Hasn't thread connection").isTrue();
boolean condition1 = !TransactionSynchronizationManager.isSynchronizationActive();
assertThat(condition1).as("Synchronization not active").isTrue();
assertThatExceptionOfType(IllegalTransactionStateException.class).isThrownBy(() -> tt.execute(new TransactionCallbackWithoutResult() {
@Override
protected void doInTransactionWithoutResult(TransactionStatus status) throws RuntimeException {
assertThat(status.isNewTransaction()).as("Is new transaction").isTrue();
tt.setPropagationBehavior(TransactionDefinition.PROPAGATION_NEVER);
tt.execute(new TransactionCallbackWithoutResult() {
@Override
protected void doInTransactionWithoutResult(TransactionStatus status) throws RuntimeException {
fail("Should have thrown IllegalTransactionStateException");
}
});
fail("Should have thrown IllegalTransactionStateException");
}
}));
boolean condition = !TransactionSynchronizationManager.hasResource(ds);
assertThat(condition).as("Hasn't thread connection").isTrue();
verify(con).rollback();
verify(con).close();
}
use of cn.taketoday.transaction.support.TransactionTemplate in project today-infrastructure by TAKETODAY.
the class JdbcTransactionManagerTests method testTransactionWithExceptionOnBegin.
/**
* Test behavior if the first operation on a connection (getAutoCommit) throws SQLException.
*/
@Test
public void testTransactionWithExceptionOnBegin() throws Exception {
willThrow(new SQLException("Cannot begin")).given(con).getAutoCommit();
TransactionTemplate tt = new TransactionTemplate(tm);
assertThatExceptionOfType(CannotCreateTransactionException.class).isThrownBy(() -> tt.execute(new TransactionCallbackWithoutResult() {
@Override
protected void doInTransactionWithoutResult(TransactionStatus status) {
// something transactional
}
}));
boolean condition = !TransactionSynchronizationManager.hasResource(ds);
assertThat(condition).as("Hasn't thread connection").isTrue();
verify(con).close();
}
Aggregations