Search in sources :

Example 16 with TransactionCallback

use of org.springframework.transaction.support.TransactionCallback in project spring-framework by spring-projects.

the class WebSphereUowTransactionManagerTests method uowManagerAndUserTransactionFoundInJndi.

@Test
public void uowManagerAndUserTransactionFoundInJndi() throws Exception {
    UserTransaction ut = mock(UserTransaction.class);
    given(ut.getStatus()).willReturn(Status.STATUS_NO_TRANSACTION, Status.STATUS_ACTIVE, Status.STATUS_ACTIVE);
    MockUOWManager manager = new MockUOWManager();
    ExpectedLookupTemplate jndiTemplate = new ExpectedLookupTemplate();
    jndiTemplate.addObject(WebSphereUowTransactionManager.DEFAULT_USER_TRANSACTION_NAME, ut);
    jndiTemplate.addObject(WebSphereUowTransactionManager.DEFAULT_UOW_MANAGER_NAME, manager);
    WebSphereUowTransactionManager ptm = new WebSphereUowTransactionManager();
    ptm.setJndiTemplate(jndiTemplate);
    ptm.afterPropertiesSet();
    DefaultTransactionDefinition definition = new DefaultTransactionDefinition();
    TransactionStatus ts = ptm.getTransaction(definition);
    ptm.commit(ts);
    assertEquals("result", ptm.execute(definition, new TransactionCallback<String>() {

        @Override
        public String doInTransaction(TransactionStatus status) {
            return "result";
        }
    }));
    assertEquals(UOWManager.UOW_TYPE_GLOBAL_TRANSACTION, manager.getUOWType());
    assertFalse(manager.getJoined());
    assertFalse(manager.getRollbackOnly());
    verify(ut).begin();
    verify(ut).commit();
}
Also used : UserTransaction(javax.transaction.UserTransaction) TransactionCallback(org.springframework.transaction.support.TransactionCallback) DefaultTransactionDefinition(org.springframework.transaction.support.DefaultTransactionDefinition) ExpectedLookupTemplate(org.springframework.tests.mock.jndi.ExpectedLookupTemplate) TransactionStatus(org.springframework.transaction.TransactionStatus) Test(org.junit.Test)

Example 17 with TransactionCallback

use of org.springframework.transaction.support.TransactionCallback in project gocd by gocd.

the class MaterialRepositoryIntegrationTest method saveMaterialRevisions.

private Material saveMaterialRevisions(final MaterialRevisions revs) {
    Material hg = revs.getMaterialRevision(0).getMaterial();
    transactionTemplate.execute(new TransactionCallback() {

        public Object doInTransaction(TransactionStatus status) {
            for (MaterialRevision revision : revs) {
                repo.saveMaterialRevision(revision);
            }
            return null;
        }
    });
    return hg;
}
Also used : TransactionCallback(org.springframework.transaction.support.TransactionCallback) TransactionStatus(org.springframework.transaction.TransactionStatus) PackageMaterial(com.thoughtworks.go.config.materials.PackageMaterial) GitMaterial(com.thoughtworks.go.config.materials.git.GitMaterial) SvnMaterial(com.thoughtworks.go.config.materials.svn.SvnMaterial) PluggableSCMMaterial(com.thoughtworks.go.config.materials.PluggableSCMMaterial) HgMaterial(com.thoughtworks.go.config.materials.mercurial.HgMaterial) ScmMaterial(com.thoughtworks.go.config.materials.ScmMaterial) P4Material(com.thoughtworks.go.config.materials.perforce.P4Material) DependencyMaterial(com.thoughtworks.go.config.materials.dependency.DependencyMaterial)

Example 18 with TransactionCallback

use of org.springframework.transaction.support.TransactionCallback in project gocd by gocd.

the class MaterialRepositoryIntegrationTest method savePipelineAndCreate_DMRs.

private DependencyMaterial savePipelineAndCreate_DMRs(Date d, MaterialRevisions hgMaterialRevisions, PipelineConfig config, int... dmrStageCounters) {
    Pipeline pipeline = savePipeline(instanceFactory.createPipelineInstance(config, BuildCause.createWithModifications(hgMaterialRevisions, "Loser"), new DefaultSchedulingContext(DEFAULT_APPROVED_BY), md5, new TimeProvider()));
    CaseInsensitiveString stageName = config.get(0).name();
    DependencyMaterial material = new DependencyMaterial(config.name(), stageName);
    String label = pipeline.getLabel();
    ArrayList<Modification> mods = new ArrayList<>();
    for (int i = 0; i < dmrStageCounters.length; i++) {
        int dmrStageCounter = dmrStageCounters[i];
        StageIdentifier stageIdentifier = new StageIdentifier(pipeline.getIdentifier(), CaseInsensitiveString.str(stageName), String.valueOf(dmrStageCounter));
        mods.add(new Modification(d, stageIdentifier.getStageLocator(), label, pipeline.getId()));
    }
    final MaterialRevision materialRevision = new MaterialRevision(material, mods);
    transactionTemplate.execute(new TransactionCallback() {

        public Object doInTransaction(TransactionStatus status) {
            repo.saveMaterialRevision(materialRevision);
            return null;
        }
    });
    return material;
}
Also used : TimeProvider(com.thoughtworks.go.util.TimeProvider) TransactionStatus(org.springframework.transaction.TransactionStatus) CaseInsensitiveString(com.thoughtworks.go.config.CaseInsensitiveString) CaseInsensitiveString(com.thoughtworks.go.config.CaseInsensitiveString) TransactionCallback(org.springframework.transaction.support.TransactionCallback) DependencyMaterial(com.thoughtworks.go.config.materials.dependency.DependencyMaterial)

Example 19 with TransactionCallback

use of org.springframework.transaction.support.TransactionCallback in project ranger by apache.

the class RangerTransactionService method executeAfterTransactionComplete.

public void executeAfterTransactionComplete(final Runnable task) {
    try {
        scheduler.schedule(new Runnable() {

            @Override
            public void run() {
                if (task != null) {
                    try {
                        // Create new  transaction
                        TransactionTemplate txTemplate = new TransactionTemplate(txManager);
                        txTemplate.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRES_NEW);
                        txTemplate.execute(new TransactionCallback<Object>() {

                            public Object doInTransaction(TransactionStatus status) {
                                task.run();
                                return null;
                            }
                        });
                    } catch (Exception e) {
                        LOG.error("Failed to commit TransactionService transaction", e);
                        LOG.error("Ignoring...");
                    }
                }
            }
        }, 1000L, MILLISECONDS);
    } catch (Exception e) {
        LOG.error("Failed to schedule TransactionService transaction:", e);
        LOG.error("Ignroing...");
    }
}
Also used : TransactionCallback(org.springframework.transaction.support.TransactionCallback) TransactionTemplate(org.springframework.transaction.support.TransactionTemplate) TransactionStatus(org.springframework.transaction.TransactionStatus)

Example 20 with TransactionCallback

use of org.springframework.transaction.support.TransactionCallback in project gocd by gocd.

the class UserSqlMapDao method deleteUsers.

@Override
public boolean deleteUsers(List<String> userNames) {
    return (Boolean) transactionTemplate.execute(new TransactionCallback() {

        @Override
        public Object doInTransaction(TransactionStatus status) {
            String queryString = "delete from User where name in (:userNames)";
            Query query = sessionFactory.getCurrentSession().createQuery(queryString);
            query.setParameterList("userNames", userNames);
            query.executeUpdate();
            return Boolean.TRUE;
        }
    });
}
Also used : TransactionCallback(org.springframework.transaction.support.TransactionCallback) Query(org.hibernate.Query) TransactionStatus(org.springframework.transaction.TransactionStatus)

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