Example usage for org.springframework.transaction.support TransactionCallback TransactionCallback

List of usage examples for org.springframework.transaction.support TransactionCallback TransactionCallback

Introduction

In this page you can find the example usage for org.springframework.transaction.support TransactionCallback TransactionCallback.

Prototype

TransactionCallback

Source Link

Usage

From source file:com.vladmihalcea.HibernateCascadeLockComponentTest.java

@Test
public void test() {

    final Long parentId = cleanAndSaveParent();

    transactionTemplate.execute(new TransactionCallback<Void>() {
        @Override/*from ww  w . j  a va 2  s  .  c om*/
        public Void doInTransaction(TransactionStatus transactionStatus) {
            Post post = entityManager.find(Post.class, parentId);
            entityManager.lock(post, LockModeType.PESSIMISTIC_WRITE, Collections
                    .singletonMap("javax.persistence.lock.scope", (Object) PessimisticLockScope.EXTENDED));
            return null;
        }
    });
}

From source file:cn.edu.ahpu.common.dao.key.SingleSequence.java

private CacheValue getNewValFromDB(final String name) {
    transactionTemplate.setPropagationBehavior(TransactionAttribute.PROPAGATION_REQUIRES_NEW);
    return transactionTemplate.execute(new TransactionCallback<CacheValue>() {

        @Override/*  w  w w.ja  va 2s .co m*/
        public CacheValue doInTransaction(TransactionStatus status) {
            return app.getCacheValue(cacheNum, name);
        }
    });
}

From source file:org.ambraproject.user.EnsureRoleInterceptor.java

public String intercept(final ActionInvocation actionInvocation) throws Exception {
    log.debug("EnsureRoleInterceptor called");
    Map session = actionInvocation.getInvocationContext().getSession();
    final String authId = (String) session.get(Constants.AUTH_KEY);
    Boolean allowAdminAction = (Boolean) new TransactionTemplate(transactionManager)
            .execute(new TransactionCallback() {
                @Override//from   w ww  .  j  a v a  2 s. com
                public Object doInTransaction(TransactionStatus transactionStatus) {
                    return userService.allowAdminAction(authId);
                }
            });
    if (allowAdminAction)
        return actionInvocation.invoke();

    return Constants.ReturnCode.NOT_SUFFICIENT_ROLE;
}

From source file:com.vladmihalcea.HibernateEagerSetTest.java

@Test
public void test() {

    final Long parentId = cleanAndSaveParent();

    SetParent parent = transactionTemplate.execute(new TransactionCallback<SetParent>() {
        @Override//from  w w  w.  j a  v  a  2  s.c  o  m
        public SetParent doInTransaction(TransactionStatus transactionStatus) {
            SetParent parent = loadParent(parentId);
            SetChild child1 = new SetChild();
            child1.setName("child1");
            SetChild child2 = new SetChild();
            child2.setName("child2");
            parent.addChild(child1);
            parent.addChild(child2);
            entityManager.merge(parent);
            return parent;
        }
    });
    transactionTemplate.execute(new TransactionCallback<Void>() {
        @Override
        public Void doInTransaction(TransactionStatus transactionStatus) {
            SetParent parent = loadParent(parentId);
            assertEquals(2, loadParent(parentId).getChildren().size());
            assertTrue(parent.getChildren().contains(parent.getChildren().iterator().next()));
            return null;
        }
    });
}

From source file:com.vladmihalcea.HibernateBagDuplicateTest.java

@Test
public void test() {

    final Long parentId = cleanAndSaveParent();

    Parent parent = transactionTemplate.execute(new TransactionCallback<Parent>() {
        @Override//from   w ww  . ja  v a  2  s.c  o m
        public Parent doInTransaction(TransactionStatus transactionStatus) {
            Parent parent = loadParent(parentId);
            Child child1 = new Child();
            child1.setName("child1");
            Child child2 = new Child();
            child2.setName("child2");
            parent.addChild(child1);
            parent.addChild(child2);
            entityManager.merge(parent);
            entityManager.flush();
            if (parent.getChildren().size() == 4) {
                LOG.error("Duplicates rows generated!");
            }
            return parent;
        }
    });
    //https://hibernate.atlassian.net/browse/HHH-3332
    //https://hibernate.atlassian.net/browse/HHH-5855
    //assertEquals(2, parent.getChildren().size());
    if (parent.getChildren().size() == 4) {
        LOG.error("Duplicates rows generated!");
    }
    transactionTemplate.execute(new TransactionCallback<Void>() {
        @Override
        public Void doInTransaction(TransactionStatus transactionStatus) {
            assertEquals(4, loadParent(parentId).getChildren().size());
            return null;
        }
    });
}

From source file:mojo.core.test.RepositoryTest.java

public void testCRUD() {
    TransactionTemplate template = new TransactionTemplate(transactionManager);
    template.execute(new TransactionCallback<Object>() {

        public Object doInTransaction(TransactionStatus status) {
            status.setRollbackOnly();//from w ww.  j av a 2  s .  c om
            String tmp = null;

            // simple typed repository
            // entity class has been set on the repository instance

            log("Creating Person A");
            Person personA = new Person(tmp = "Person A");
            Insert<Person> insertPerson = new Insert<Person>(personA);
            personA = personRepository.insert(insertPerson);
            assertValidPerson(personA, tmp);

            log("Retrieving Person A #" + personA.getId());
            Select<Person> selectPerson = new Select<Person>(new ByKey(personA.getId()));
            personA = personRepository.select(selectPerson).unique();
            assertValidPerson(personA, tmp);

            log("Modifying Person A #" + personA.getId());
            personA.setName(tmp = "Mickey Mouse");
            Update<Person> updatePerson = new Update<Person>(personA);
            personA = personRepository.update(updatePerson);
            assertValidPerson(personA, tmp);

            log("Retrieving Person A #" + personA.getId());
            personA = personRepository.select(selectPerson).unique();
            assertValidPerson(personA, tmp);

            // simple untyped repository
            // entity class is being set on each of the statement instances

            log("Creating Person B");
            Person personB = new Person(tmp = "Person B");
            Insert<Object> insertObject = new Insert<Object>(Person.class, personB);
            personB = (Person) repository.insert(insertObject);
            assertValidPerson(personB, tmp);

            log("Retrieving Person B #" + personB.getId());
            Select<Object> selectObject = new Select<Object>(Person.class, new ByKey(personB.getId()));
            personB = (Person) repository.select(selectObject).unique();
            assertValidPerson(personB, tmp);

            log("Modifying Person B #" + personB.getId());
            personB.setName(tmp = "Jon Arbuckle");
            Update<Object> updateObject = new Update<Object>(Person.class, personB);
            personB = (Person) repository.update(updateObject);
            assertValidPerson(personB, tmp);

            log("Retrieving Person B #" + personB.getId());
            personB = (Person) repository.select(selectObject).unique();
            assertValidPerson(personB, tmp);

            // many-to-one repository

            log("Creating Pet A");
            Pet petA = new Pet(tmp = "Pet");
            petA = petRepository.insert(new InsertDetail<Pet>(personB.getId(), petA));
            assertValidPet(petA, tmp, personB);

            log("Modifying Pet A");
            petA.setName(tmp = "Pluto");
            petA = petRepository.update(new UpdateDetail<Pet>(personA.getId(), petA));
            assertValidPet(petA, tmp, personA);

            log("Creating Pet B");
            Pet petB = new Pet(tmp = "Garfield");
            petB = petRepository.insert(new InsertDetail<Pet>(personB.getId(), petB));
            assertValidPet(petB, tmp, personB);

            log("Creating Pet C");
            Pet petC = new Pet(tmp = "Odie");
            petC = petRepository.insert(new InsertDetail<Pet>(personB.getId(), petC));
            assertValidPet(petC, tmp, personB);

            // one-to-many repository

            log("Creating Phone 1");
            Phone phoneA = new Phone(tmp = "9876543210");
            phoneA = phoneRepository.insert(new InsertDetail<Phone>(personB.getId(), phoneA));
            assertValidPhone(phoneA, tmp);

            log("Modifying Phone 1");
            phoneA.setNumber(tmp = "0123456789");
            phoneA = phoneRepository.update(new UpdateDetail<Phone>(personA.getId(), phoneA));
            assertValidPhone(phoneA, tmp);

            log("Creating Phone 2");
            Phone phoneB = new Phone(tmp = "0011223344");
            phoneB = phoneRepository.insert(new InsertDetail<Phone>(personB.getId(), phoneB));
            assertValidPhone(phoneB, tmp);

            log("Creating Phone 3");
            Phone phoneC = new Phone(tmp = "5566778899");
            phoneC = phoneRepository.insert(new InsertDetail<Phone>(personB.getId(), phoneC));
            assertValidPhone(phoneC, tmp);

            // plain selects

            log("Retrieving all persons");
            DataPage<?> page = repository.select(new Select<Object>(Person.class));
            assertValidDataPage(page, 2);

            log("Retrieving persons by property");
            page = repository.select(new Select<Object>(Person.class, new ByText("buck", "name")));
            assertValidDataPage(page, 1);

            // many-to-one selects

            log("Retrieving all pets");
            page = petRepository.select(new SelectDetail<Pet>());
            assertValidDataPage(page, 3);

            log("Retrieving pets by property");
            page = petRepository.select(new SelectDetail<Pet>(null, new ByText("lut", "name")));
            assertValidDataPage(page, 1);

            log("Retrieving PersonA's pets by property");
            page = petRepository.select(new SelectDetail<Pet>(personA.getId(), new ByText("arfiel", "name")));
            assertValidDataPage(page, 0);

            log("Retrieving PersonB's pets by property");
            page = petRepository.select(new SelectDetail<Pet>(personB.getId(), new ByText("arfiel", "name")));
            assertValidDataPage(page, 1);

            log("Retrieving PersonB's pets with pagging");
            page = petRepository.select(new SelectDetail<Pet>(personB.getId()).page(0, 1));
            assertValidDataPage(page, 1, 2);

            // one-to-many selects

            log("Retrieving all phones");
            page = phoneRepository.select(new SelectDetail<Phone>());
            assertValidDataPage(page, 3);

            log("Retrieving phones by property");
            page = phoneRepository.select(new SelectDetail<Phone>(null, new ByText("1234", "number")));
            assertValidDataPage(page, 1);

            log("Retrieving PersonA's phones by property");
            page = phoneRepository
                    .select(new SelectDetail<Phone>(personA.getId(), new ByText("1122", "number")));
            assertValidDataPage(page, 0);

            log("Retrieving PersonB's phones by property");
            page = phoneRepository
                    .select(new SelectDetail<Phone>(personB.getId(), new ByText("1122", "number")));
            assertValidDataPage(page, 1);

            log("Retrieving PersonB's phones with paging");
            page = phoneRepository.select(new SelectDetail<Phone>(personB.getId()).page(0, 1));
            assertValidDataPage(page, 1, 2);

            return null;
        }
    });
}

From source file:com.vladmihalcea.HibernateSaveIdOnlyEntityTest.java

@Test
public void test() {

    final Long parentId = cleanAndSaveParent();

    SetParent parent = transactionTemplate.execute(new TransactionCallback<SetParent>() {
        @Override//from  w w  w .j  av  a  2 s.  c  om
        public SetParent doInTransaction(TransactionStatus transactionStatus) {
            SetParent parent = loadParent(parentId);
            SetChild child1 = new SetChild();
            child1.setName("child1");
            SetChild child2 = new SetChild();
            child2.setName("child2");
            parent.addChild(child1);
            parent.addChild(child2);
            entityManager.merge(parent);
            return parent;
        }
    });
    transactionTemplate.execute(new TransactionCallback<Void>() {
        @Override
        public Void doInTransaction(TransactionStatus transactionStatus) {
            SetParent idOnlyParent = new SetParent();
            idOnlyParent.setId(parentId);
            SetChild newChild = new SetChild();
            newChild.setName("new");
            newChild.setParent(idOnlyParent);
            entityManager.persist(newChild);
            entityManager.flush();
            SetChild otherChild = new SetChild();
            otherChild.setName("Other");
            idOnlyParent.addChild(otherChild);
            entityManager.flush();
            assertEquals(1, idOnlyParent.getChildren().size());
            return null;
        }
    });

    transactionTemplate.execute(new TransactionCallback<Void>() {
        @Override
        public Void doInTransaction(TransactionStatus transactionStatus) {
            SetParent parent = entityManager.find(SetParent.class, parentId);
            assertEquals(3, parent.getChildren().size());
            return null;
        }
    });

}

From source file:at.irian.demo.jsfatwork.view.TestdataBuilder.java

private void init() {
    this.transactionTemplate.execute(new TransactionCallback() {
        public Object doInTransaction(TransactionStatus transactionStatus) {
            User guest = createGuestUser();
            createPublicGroup(guest);//from  w w w  .ja  v  a 2 s.com
            return null;
        }
    });
}

From source file:com.vladmihalcea.HibernateLinkedSetTest.java

@Test
public void test() {

    final Long parentId = cleanAndSaveParent();

    LinkedParent parent = transactionTemplate.execute(new TransactionCallback<LinkedParent>() {
        @Override//from w  w  w .  j  av a2  s  .c  o m
        public LinkedParent doInTransaction(TransactionStatus transactionStatus) {
            LinkedParent parent = loadParent(parentId);
            LinkedChild child1 = new LinkedChild();
            child1.setName("child1");
            LinkedChild child2 = new LinkedChild();
            child2.setName("child2");
            LinkedChild child3 = new LinkedChild();
            child3.setName("child3");
            LinkedChild child4 = new LinkedChild();
            child4.setName("child4");
            LinkedChild child5 = new LinkedChild();
            child5.setName("child5");
            parent.addChild(child1);
            parent.addChild(child2);
            parent.addChild(child3);
            parent.addChild(child4);
            parent.addChild(child5);
            entityManager.merge(parent);
            return parent;
        }
    });
    transactionTemplate.execute(new TransactionCallback<Void>() {
        @Override
        public Void doInTransaction(TransactionStatus transactionStatus) {
            Set<LinkedChild> children = loadParent(parentId).getChildren();
            assertEquals(5, children.size());
            Iterator<LinkedChild> childIterator = children.iterator();
            assertEquals("child1", childIterator.next().getName());
            assertEquals("child2", childIterator.next().getName());
            assertEquals("child3", childIterator.next().getName());
            assertEquals("child4", childIterator.next().getName());
            assertEquals("child5", childIterator.next().getName());
            return null;
        }
    });
}

From source file:org.iti.agrimarket.model.dao.ProductDAO.java

@Override
public void edit(Product product) {
    transactionTemplate.execute(new TransactionCallback() {
        @Override/*w ww  .jav a  2s  . c  o  m*/
        public Object doInTransaction(TransactionStatus ts) {
            try {
                Session session = getHibernateTemplate().getSessionFactory().getCurrentSession();

                session.update(product);
                return true;
            } catch (Exception e) {
                e.printStackTrace();
                ts.setRollbackOnly();
            }
            return false;
        }
    });
}