Search in sources :

Example 1 with TransactionCallback

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;
}
Also used : TransactionCallback(org.springframework.transaction.support.TransactionCallback) TimerTask(java.util.TimerTask) Timer(java.util.Timer) TransactionStatus(org.springframework.transaction.TransactionStatus) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET)

Example 2 with TransactionCallback

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();
}
Also used : TransactionCallback(org.springframework.transaction.support.TransactionCallback) TransactionStatus(org.springframework.transaction.TransactionStatus) Test(org.junit.Test)

Example 3 with TransactionCallback

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();
}
Also used : TransactionCallback(org.springframework.transaction.support.TransactionCallback) ArrayList(java.util.ArrayList) TransactionStatus(org.springframework.transaction.TransactionStatus) Test(org.junit.Test)

Example 4 with TransactionCallback

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();
}
Also used : TransactionCallback(org.springframework.transaction.support.TransactionCallback) ArrayList(java.util.ArrayList) TransactionStatus(org.springframework.transaction.TransactionStatus) DataSource(javax.sql.DataSource) Test(org.junit.Test)

Example 5 with TransactionCallback

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();
}
Also used : TransactionCallback(org.springframework.transaction.support.TransactionCallback) ArrayList(java.util.ArrayList) TransactionStatus(org.springframework.transaction.TransactionStatus) Test(org.junit.Test)

Aggregations

TransactionCallback (org.springframework.transaction.support.TransactionCallback)101 TransactionStatus (org.springframework.transaction.TransactionStatus)76 Test (org.junit.Test)28 ArrayList (java.util.ArrayList)16 Test (org.junit.jupiter.api.Test)14 TransactionTemplate (org.springframework.transaction.support.TransactionTemplate)11 DefaultTransactionDefinition (org.springframework.transaction.support.DefaultTransactionDefinition)10 CaseInsensitiveString (com.thoughtworks.go.config.CaseInsensitiveString)9 PreparedStatement (java.sql.PreparedStatement)8 List (java.util.List)8 GitMaterial (com.thoughtworks.go.config.materials.git.GitMaterial)7 MaterialInstance (com.thoughtworks.go.domain.MaterialInstance)7 Modification (com.thoughtworks.go.domain.materials.Modification)7 PackageMaterialInstance (com.thoughtworks.go.domain.materials.packagematerial.PackageMaterialInstance)7 PluggableSCMMaterialInstance (com.thoughtworks.go.domain.materials.scm.PluggableSCMMaterialInstance)7 Query (org.hibernate.Query)7 BaseDbTest (com.alibaba.otter.node.etl.BaseDbTest)6 DbDialect (com.alibaba.otter.node.etl.common.db.dialect.DbDialect)6 Modifications (com.thoughtworks.go.domain.materials.Modifications)6 HgMaterialInstance (com.thoughtworks.go.domain.materials.mercurial.HgMaterialInstance)6