use of org.springframework.transaction.support.TransactionCallback in project opennms by OpenNMS.
the class RemotePollerAvailabilityService method getAvailability.
@GET
@Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
@Path("availability")
public OnmsLocationAvailDefinitionList getAvailability(@Context final UriInfo uriInfo) throws InterruptedException {
if (m_timer == null) {
final MultivaluedMap<String, String> queryParameters = uriInfo.getQueryParameters();
m_defList = getAvailabilityList(createTimeChunker(queryParameters), getSortedApplications(), null, getSelectedNodes(queryParameters));
TimerTask task = new TimerTask() {
@Override
public void run() {
m_defList = m_transactionTemplate.execute(new TransactionCallback<OnmsLocationAvailDefinitionList>() {
@Override
public OnmsLocationAvailDefinitionList doInTransaction(TransactionStatus status) {
return getAvailabilityList(getTimeChunkerFromMidnight(), getSortedApplications(), null, null);
}
});
}
};
m_timer = new Timer("AvailCalculator-Timer");
m_timer.scheduleAtFixedRate(task, TimeChunker.MINUTE, TimeChunker.MINUTE);
}
return m_defList;
}
use of org.springframework.transaction.support.TransactionCallback in project spring-framework by spring-projects.
the class JpaTransactionManagerTests method testTransactionRollbackWithPropagationSupports.
@Test
public void testTransactionRollbackWithPropagationSupports() {
given(manager.isOpen()).willReturn(true);
tt.setPropagationBehavior(TransactionDefinition.PROPAGATION_SUPPORTS);
assertTrue(!TransactionSynchronizationManager.hasResource(factory));
assertTrue(!TransactionSynchronizationManager.isSynchronizationActive());
tt.execute(new TransactionCallback() {
@Override
public Object doInTransaction(TransactionStatus status) {
assertTrue(!TransactionSynchronizationManager.hasResource(factory));
assertTrue(TransactionSynchronizationManager.isSynchronizationActive());
assertTrue(!status.isNewTransaction());
EntityManagerFactoryUtils.getTransactionalEntityManager(factory).flush();
status.setRollbackOnly();
return null;
}
});
assertTrue(!TransactionSynchronizationManager.hasResource(factory));
assertTrue(!TransactionSynchronizationManager.isSynchronizationActive());
verify(manager).flush();
verify(manager).close();
}
use of org.springframework.transaction.support.TransactionCallback in project spring-framework by spring-projects.
the class JpaTransactionManagerTests method testTransactionCommitWithPrebound.
@Test
public void testTransactionCommitWithPrebound() {
given(manager.getTransaction()).willReturn(tx);
final List<String> l = new ArrayList<>();
l.add("test");
assertTrue(!TransactionSynchronizationManager.hasResource(factory));
assertTrue(!TransactionSynchronizationManager.isSynchronizationActive());
TransactionSynchronizationManager.bindResource(factory, new EntityManagerHolder(manager));
try {
Object result = tt.execute(new TransactionCallback() {
@Override
public Object doInTransaction(TransactionStatus status) {
assertTrue(TransactionSynchronizationManager.hasResource(factory));
assertTrue(TransactionSynchronizationManager.isSynchronizationActive());
EntityManagerFactoryUtils.getTransactionalEntityManager(factory);
return l;
}
});
assertSame(l, result);
assertTrue(TransactionSynchronizationManager.hasResource(factory));
assertTrue(!TransactionSynchronizationManager.isSynchronizationActive());
} finally {
TransactionSynchronizationManager.unbindResource(factory);
}
verify(tx).begin();
verify(tx).commit();
}
use of org.springframework.transaction.support.TransactionCallback in project spring-framework by spring-projects.
the class JpaTransactionManagerTests method testTransactionCommitWithDataSource.
@Test
public void testTransactionCommitWithDataSource() throws SQLException {
DataSource ds = mock(DataSource.class);
tm.setDataSource(ds);
given(manager.getTransaction()).willReturn(tx);
final List<String> l = new ArrayList<>();
l.add("test");
assertTrue(!TransactionSynchronizationManager.hasResource(factory));
assertTrue(!TransactionSynchronizationManager.isSynchronizationActive());
Object result = tt.execute(new TransactionCallback() {
@Override
public Object doInTransaction(TransactionStatus status) {
assertTrue(TransactionSynchronizationManager.hasResource(factory));
assertTrue(TransactionSynchronizationManager.isSynchronizationActive());
EntityManagerFactoryUtils.getTransactionalEntityManager(factory).flush();
return l;
}
});
assertTrue(result == l);
assertTrue(!TransactionSynchronizationManager.hasResource(factory));
assertTrue(!TransactionSynchronizationManager.isSynchronizationActive());
verify(tx).commit();
verify(manager).flush();
verify(manager).close();
}
use of org.springframework.transaction.support.TransactionCallback in project spring-framework by spring-projects.
the class JpaTransactionManagerTests method testTransactionCommit.
@Test
public void testTransactionCommit() {
given(manager.getTransaction()).willReturn(tx);
final List<String> l = new ArrayList<>();
l.add("test");
assertTrue(!TransactionSynchronizationManager.hasResource(factory));
assertTrue(!TransactionSynchronizationManager.isSynchronizationActive());
Object result = tt.execute(new TransactionCallback() {
@Override
public Object doInTransaction(TransactionStatus status) {
assertTrue(TransactionSynchronizationManager.hasResource(factory));
EntityManagerFactoryUtils.getTransactionalEntityManager(factory).flush();
return l;
}
});
assertSame(l, result);
assertTrue(!TransactionSynchronizationManager.hasResource(factory));
assertTrue(!TransactionSynchronizationManager.isSynchronizationActive());
verify(tx).commit();
verify(manager).flush();
verify(manager).close();
}
Aggregations