Search in sources :

Example 96 with TransactionStatus

use of org.springframework.transaction.TransactionStatus in project gocd by gocd.

the class MaterialRepositoryIntegrationTest method shouldRemoveDuplicatesBeforeInsertingModifications.

@Test
public void shouldRemoveDuplicatesBeforeInsertingModifications() {
    final MaterialInstance materialInstance = repo.findOrCreateFrom(new GitMaterial(UUID.randomUUID().toString(), "branch"));
    final ArrayList<Modification> firstSetOfModifications = getModifications(3);
    transactionTemplate.execute(new TransactionCallback() {

        public Object doInTransaction(TransactionStatus status) {
            repo.saveModifications(materialInstance, firstSetOfModifications);
            return null;
        }
    });
    Modifications firstSetOfModificationsFromDb = repo.getModificationsFor(materialInstance, Pagination.pageByNumber(1, 10, 10));
    assertThat(firstSetOfModificationsFromDb.size(), is(3));
    for (Modification modification : firstSetOfModifications) {
        assertThat(firstSetOfModificationsFromDb.containsRevisionFor(modification), is(true));
    }
    final ArrayList<Modification> secondSetOfModificationsContainingDuplicateRevisions = getModifications(4);
    transactionTemplate.execute(new TransactionCallback() {

        public Object doInTransaction(TransactionStatus status) {
            repo.saveModifications(materialInstance, secondSetOfModificationsContainingDuplicateRevisions);
            return null;
        }
    });
    Modifications secondSetOfModificationsFromDb = repo.getModificationsFor(materialInstance, Pagination.pageByNumber(1, 10, 10));
    assertThat(secondSetOfModificationsFromDb.size(), is(4));
    for (final Modification fromPreviousCycle : firstSetOfModificationsFromDb) {
        Modification modification = ListUtil.find(secondSetOfModificationsFromDb, new ListUtil.Condition() {

            @Override
            public <T> boolean isMet(T item) {
                Modification modification = (Modification) item;
                return modification.getId() == fromPreviousCycle.getId();
            }
        });
        assertThat(modification, is(notNullValue()));
    }
    for (Modification modification : secondSetOfModificationsContainingDuplicateRevisions) {
        assertThat(secondSetOfModificationsFromDb.containsRevisionFor(modification), is(true));
    }
}
Also used : TransactionCallback(org.springframework.transaction.support.TransactionCallback) GitMaterial(com.thoughtworks.go.config.materials.git.GitMaterial) TransactionStatus(org.springframework.transaction.TransactionStatus) PackageMaterialInstance(com.thoughtworks.go.domain.materials.packagematerial.PackageMaterialInstance) PluggableSCMMaterialInstance(com.thoughtworks.go.domain.materials.scm.PluggableSCMMaterialInstance) SvnMaterialInstance(com.thoughtworks.go.domain.materials.svn.SvnMaterialInstance) Test(org.junit.Test)

Example 97 with TransactionStatus

use of org.springframework.transaction.TransactionStatus in project gocd by gocd.

the class MaterialRepositoryIntegrationTest method shouldAllowSavingModificationsIfRevisionsAcrossDifferentMaterialsHappenToBeSame.

@Test
public void shouldAllowSavingModificationsIfRevisionsAcrossDifferentMaterialsHappenToBeSame() {
    final MaterialInstance materialInstance1 = repo.findOrCreateFrom(new GitMaterial(UUID.randomUUID().toString(), "branch"));
    final MaterialInstance materialInstance2 = repo.findOrCreateFrom(new GitMaterial(UUID.randomUUID().toString(), "branch"));
    final ArrayList<Modification> modificationsForFirstMaterial = getModifications(3);
    transactionTemplate.execute(new TransactionCallback() {

        public Object doInTransaction(TransactionStatus status) {
            repo.saveModifications(materialInstance1, modificationsForFirstMaterial);
            return null;
        }
    });
    assertThat(repo.getModificationsFor(materialInstance1, Pagination.pageByNumber(1, 10, 10)).size(), is(3));
    final ArrayList<Modification> modificationsForSecondMaterial = getModifications(3);
    transactionTemplate.execute(new TransactionCallback() {

        public Object doInTransaction(TransactionStatus status) {
            repo.saveModifications(materialInstance2, modificationsForSecondMaterial);
            return null;
        }
    });
    Modifications modificationsFromDb = repo.getModificationsFor(materialInstance2, Pagination.pageByNumber(1, 10, 10));
    assertThat(modificationsFromDb.size(), is(3));
    for (Modification modification : modificationsForSecondMaterial) {
        assertThat(modificationsFromDb.containsRevisionFor(modification), is(true));
    }
}
Also used : TransactionCallback(org.springframework.transaction.support.TransactionCallback) GitMaterial(com.thoughtworks.go.config.materials.git.GitMaterial) TransactionStatus(org.springframework.transaction.TransactionStatus) PackageMaterialInstance(com.thoughtworks.go.domain.materials.packagematerial.PackageMaterialInstance) PluggableSCMMaterialInstance(com.thoughtworks.go.domain.materials.scm.PluggableSCMMaterialInstance) SvnMaterialInstance(com.thoughtworks.go.domain.materials.svn.SvnMaterialInstance) Test(org.junit.Test)

Example 98 with TransactionStatus

use of org.springframework.transaction.TransactionStatus in project gocd by gocd.

the class MaterialRepositoryIntegrationTest method shouldBeAbleToHandleLargeNumberOfModifications.

//Slow test - takes ~1 min to run. Will remove if it causes issues. - Jyoti
@Test
public void shouldBeAbleToHandleLargeNumberOfModifications() {
    final MaterialInstance materialInstance = repo.findOrCreateFrom(new GitMaterial(UUID.randomUUID().toString(), "branch"));
    int count = 10000;
    final ArrayList<Modification> firstSetOfModifications = getModifications(count);
    transactionTemplate.execute(new TransactionCallback() {

        public Object doInTransaction(TransactionStatus status) {
            repo.saveModifications(materialInstance, firstSetOfModifications);
            return null;
        }
    });
    assertThat(repo.getTotalModificationsFor(materialInstance), is(new Long(count)));
    final ArrayList<Modification> secondSetOfModifications = getModifications(count + 1);
    transactionTemplate.execute(new TransactionCallback() {

        public Object doInTransaction(TransactionStatus status) {
            repo.saveModifications(materialInstance, secondSetOfModifications);
            return null;
        }
    });
    assertThat(repo.getTotalModificationsFor(materialInstance), is(new Long(count + 1)));
}
Also used : TransactionCallback(org.springframework.transaction.support.TransactionCallback) GitMaterial(com.thoughtworks.go.config.materials.git.GitMaterial) TransactionStatus(org.springframework.transaction.TransactionStatus) PackageMaterialInstance(com.thoughtworks.go.domain.materials.packagematerial.PackageMaterialInstance) PluggableSCMMaterialInstance(com.thoughtworks.go.domain.materials.scm.PluggableSCMMaterialInstance) SvnMaterialInstance(com.thoughtworks.go.domain.materials.svn.SvnMaterialInstance) Test(org.junit.Test)

Example 99 with TransactionStatus

use of org.springframework.transaction.TransactionStatus in project gocd by gocd.

the class DatabaseAccessHelper method addRevisionsWithModifications.

public MaterialRevision addRevisionsWithModifications(Material material, Modification... modifications) {
    final MaterialRevision revision = filterUnsaved(new MaterialRevision(material, modifications));
    if (revision.getModifications().isEmpty()) {
        return revision;
    }
    transactionTemplate.execute(new TransactionCallbackWithoutResult() {

        @Override
        protected void doInTransactionWithoutResult(TransactionStatus status) {
            materialRepository.saveMaterialRevision(revision);
        }
    });
    return revision;
}
Also used : TransactionStatus(org.springframework.transaction.TransactionStatus) DependencyMaterialRevision(com.thoughtworks.go.domain.materials.dependency.DependencyMaterialRevision) TransactionCallbackWithoutResult(org.springframework.transaction.support.TransactionCallbackWithoutResult)

Example 100 with TransactionStatus

use of org.springframework.transaction.TransactionStatus in project gocd by gocd.

the class DatabaseAccessHelper method saveRevs.

public void saveRevs(final MaterialRevisions materialRevisions) {
    final MaterialRevisions unsavedRevisions = new MaterialRevisions();
    for (MaterialRevision materialRevision : materialRevisions) {
        unsavedRevisions.addRevision(filterUnsaved(materialRevision));
    }
    if (unsavedRevisions.isEmpty()) {
        return;
    }
    transactionTemplate.execute(new TransactionCallbackWithoutResult() {

        @Override
        protected void doInTransactionWithoutResult(TransactionStatus status) {
            materialRepository.save(unsavedRevisions);
        }
    });
}
Also used : TransactionStatus(org.springframework.transaction.TransactionStatus) DependencyMaterialRevision(com.thoughtworks.go.domain.materials.dependency.DependencyMaterialRevision) TransactionCallbackWithoutResult(org.springframework.transaction.support.TransactionCallbackWithoutResult)

Aggregations

TransactionStatus (org.springframework.transaction.TransactionStatus)340 Test (org.junit.Test)179 TransactionCallbackWithoutResult (org.springframework.transaction.support.TransactionCallbackWithoutResult)151 TransactionCallback (org.springframework.transaction.support.TransactionCallback)85 TransactionTemplate (org.springframework.transaction.support.TransactionTemplate)70 DefaultTransactionDefinition (org.springframework.transaction.support.DefaultTransactionDefinition)32 ArrayList (java.util.ArrayList)30 List (java.util.List)26 SQLException (java.sql.SQLException)24 Connection (java.sql.Connection)21 Date (java.util.Date)21 PreparedStatement (java.sql.PreparedStatement)17 TransactionSynchronizationAdapter (org.springframework.transaction.support.TransactionSynchronizationAdapter)14 ManagerException (com.alibaba.otter.manager.biz.common.exceptions.ManagerException)13 RepeatConfigureException (com.alibaba.otter.manager.biz.common.exceptions.RepeatConfigureException)13 UncategorizedSQLException (org.springframework.jdbc.UncategorizedSQLException)12 PlatformTransactionManager (org.springframework.transaction.PlatformTransactionManager)12 EntityManager (javax.persistence.EntityManager)11 DataSource (javax.sql.DataSource)11 CaseInsensitiveString (com.thoughtworks.go.config.CaseInsensitiveString)10