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:org.fireflow.pdl.bpel.IfTest.java

@Test
public void testStartProcess() {
    final WorkflowSession session = WorkflowSessionFactory.createWorkflowSession(runtimeContext,
            FireWorkflowSystem.getInstance());
    final WorkflowStatement stmt = session.createWorkflowStatement(PROCESS_TYPE);
    transactionTemplate.execute(new TransactionCallback() {
        public Object doInTransaction(TransactionStatus arg0) {
            //?/*from  www . j a va  2s  . c o  m*/
            BpelProcess process = new BpelProcess(processId);
            process.setStartActivity((new Sequence("Sequence1"))
                    .addChild((new If("If", "processVars.x==1", new EmptyActivity("EmptyActivity_In_If_1")))
                            .addElseIf("processVars.x==2", new EmptyActivity("EmptyActivity_In_If_2"))
                            .setElse(new EmptyActivity("EmptyActivity_In_If_3")))
                    .addChild(new EmptyActivity("EmptyActivity_In_Sequence1")));

            // ??
            try {
                Map<String, Object> vars = new HashMap<String, Object>();
                vars.put("x", new Integer(2));

                stmt.startProcess(process, null, vars);

            } catch (InvalidModelException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (WorkflowProcessNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (InvalidOperationException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            return null;
        }

    });
}

From source file:com.googlecode.starflow.core.key.SingleSequence.java

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

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

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

/**
 * @author Amr/* w w w .  j a v a2  s .co  m*/
 * @param user
 * @return user Id
 */
@Override
public int create(User user) {
    return (int) transactionTemplate.execute(new TransactionCallback() {

        @Override
        public Object doInTransaction(TransactionStatus ts) {

            try {
                Session session = getHibernateTemplate().getSessionFactory().getCurrentSession();
                System.out.println("user befor persist in create : " + user.toString());

                session.persist(user);

                System.out.println("user after persist in create : " + user.toString());
                return user.getId();

            } catch (HibernateException e) {
                e.printStackTrace();
                ts.setRollbackOnly();

                return -2; //means DB error 

            } catch (Exception e) {

                e.printStackTrace();
                ts.setRollbackOnly();

                return -1; //means Server error 
            }

        }
    });

}

From source file:com.opensymphony.able.demo.service.LoadDatabaseService.java

public void afterPropertiesSet() throws Exception {
    transactionTemplate.execute(new TransactionCallback() {
        public Object doInTransaction(TransactionStatus status) {
            return jpaTemplate.execute(new JpaCallback() {
                public Object doInJpa(EntityManager em) throws PersistenceException {
                    entityManager = em;/*from   www  .java2  s .  co  m*/

                    populateUsers();
                    populateBugs();

                    return null;

                }
            });
        }
    });
}

From source file:net.chrisrichardson.foodToGo.restaurantNotificationService.tsImpl.dao.OrderDAOIBatisImplTests.java

public void testFindOrdersToSend_pessimistic() throws Exception {
    DataSourceTransactionManager tm = new DataSourceTransactionManager(ds);
    TransactionTemplate tt = new TransactionTemplate(tm);

    // This has to be done in a transaction

    tt.execute(new TransactionCallback() {

        public Object doInTransaction(TransactionStatus status) {
            dao.setLockingMode(OrderDAOIBatisImpl.PESSIMISTIC_LOCKING);
            List orders = dao.findOrdersToSend();
            assertFalse(orders.isEmpty());
            return null;
        }/*from ww w.  j  a  v  a 2  s .  c  o  m*/
    });
}

From source file:org.fireflow.pdl.bpel.WhileTest.java

@Test
public void testStartProcess() {
    final WorkflowSession session = WorkflowSessionFactory.createWorkflowSession(runtimeContext,
            FireWorkflowSystem.getInstance());
    final WorkflowStatement stmt = session.createWorkflowStatement(PROCESS_TYPE);
    transactionTemplate.execute(new TransactionCallback() {
        public Object doInTransaction(TransactionStatus arg0) {

            //?// w w  w.  j a  v a2s .c o m
            BpelProcess process = new BpelProcess(processId);
            process.setStartActivity((new While("While", "processVars.x<3", (new Sequence("While.Seq1"))
                    .addChild((new If("While.Seq1.If", "processVars.y==1",
                            new EmptyActivity("EmptyActivity_In_If_1")))
                                    .addElseIf("processVars.y==2", new EmptyActivity("EmptyActivity_In_If_2"))
                                    .setElse(new EmptyActivity("EmptyActivity_In_If_3")))
                    .addChild(new XYZActivity("While.Seq1.XYZActivity")))));

            //??
            try {
                Map<String, Object> vars = new HashMap<String, Object>();
                vars.put("x", new Integer(1));
                vars.put("y", new Integer(2));

                stmt.startProcess(process, null, vars);

            } catch (InvalidModelException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (WorkflowProcessNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (InvalidOperationException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            return null;
        }

    });
}

From source file:com.javaetmoi.core.persistence.hibernate.TestIssue1.java

@Test
public void nestedListInEmbeddable() {

    Foo dbFoo = transactionTemplate.execute(new TransactionCallback<Foo>() {

        public Foo doInTransaction(TransactionStatus status) {
            Foo foo = (Foo) sessionFactory.getCurrentSession().get(Foo.class, 1);
            LazyLoadingUtil.deepHydrate(sessionFactory.getCurrentSession(), foo);
            return foo;
        }// w  ww.j a va 2  s. c  o m
    });
    assertNotNull(dbFoo.getBar());
    assertNotNull(dbFoo.getBar().getBizs());
    assertEquals("Fix the LazyInitializationException", 2, dbFoo.getBar().getBizs().size());
    assertNotNull(dbFoo.getBar().getBizs().get(0));
}

From source file:org.statefulj.demo.ddd.customer.domain.impl.CustomerServiceImpl.java

@PostConstruct
private void init() {
    TransactionTemplate tt = new TransactionTemplate(transactionManager);
    tt.execute(new TransactionCallback<Object>() {

        @Override/*ww w.ja  v a2  s. c  o  m*/
        public Object doInTransaction(TransactionStatus status) {
            return entityManager.createNativeQuery("CREATE SEQUENCE customer_sequence AS BIGINT START WITH 1")
                    .executeUpdate();
        }

    });
}

From source file:com.javaetmoi.core.persistence.hibernate.TestIssue2.java

@Test
public void nestedListUsingMappedSuperclass() {

    Parent dbParent = transactionTemplate.execute(new TransactionCallback<Parent>() {

        public Parent doInTransaction(TransactionStatus status) {
            Parent parent = (Parent) sessionFactory.getCurrentSession().get(Parent.class, 1L);
            LazyLoadingUtil.deepHydrate(sessionFactory.getCurrentSession(), parent);
            return parent;
        }/*from   ww  w. j a  v  a2s  .c om*/
    });
    assertEquals(Long.valueOf(1), dbParent.getId());
    assertEquals("Parent 1", dbParent.getName());
    assertEquals(2, dbParent.getChildren().size());
    assertNotNull("Child 10", dbParent.getChildren().get(0).getName());
    assertNotNull("Parent 1", dbParent.getChildren().get(0).getParent().getName());
}

From source file:dao.SchemaHistoryDAO.java

public static ObjectNode getPagedSchemaDataset(String name, Long datasetId, int page, int size) {
    ObjectNode result = Json.newObject();

    javax.sql.DataSource ds = getJdbcTemplate().getDataSource();
    DataSourceTransactionManager tm = new DataSourceTransactionManager(ds);
    TransactionTemplate txTemplate = new TransactionTemplate(tm);

    result = txTemplate.execute(new TransactionCallback<ObjectNode>() {
        public ObjectNode doInTransaction(TransactionStatus status) {

            List<SchemaDataset> pagedScripts = null;
            if (StringUtils.isNotBlank(name)) {
                if (datasetId != null && datasetId > 0) {
                    pagedScripts = getJdbcTemplate().query(GET_SPECIFIED_SCHEMA_DATASET_WITH_FILTER,
                            new SchemaDatasetRowMapper(), datasetId, "%" + name + "%", (page - 1) * size, size);

                } else {
                    pagedScripts = getJdbcTemplate().query(GET_PAGED_SCHEMA_DATASET_WITH_FILTER,
                            new SchemaDatasetRowMapper(), "%" + name + "%", (page - 1) * size, size);
                }//from w  ww  . j a  va2  s  . c  o m
            } else {
                if (datasetId != null && datasetId > 0) {
                    pagedScripts = getJdbcTemplate().query(GET_SPECIFIED_SCHEMA_DATASET,
                            new SchemaDatasetRowMapper(), datasetId, (page - 1) * size, size);
                } else {
                    pagedScripts = getJdbcTemplate().query(GET_PAGED_SCHEMA_DATASET,
                            new SchemaDatasetRowMapper(), (page - 1) * size, size);
                }
            }

            long count = 0;
            try {
                count = getJdbcTemplate().queryForObject("SELECT FOUND_ROWS()", Long.class);
            } catch (EmptyResultDataAccessException e) {
                Logger.error("Exception = " + e.getMessage());
            }

            ObjectNode resultNode = Json.newObject();
            resultNode.put("count", count);
            resultNode.put("page", page);
            resultNode.put("itemsPerPage", size);
            resultNode.put("totalPages", (int) Math.ceil(count / ((double) size)));
            resultNode.set("datasets", Json.toJson(pagedScripts));

            return resultNode;
        }
    });

    return result;
}