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.util.DatabaseScriptLifecycleHandler.java

public void destroyDatabase() {
    if (transactional) {
        transactionTemplate.execute(new TransactionCallback<Void>() {
            @Override//from w  w  w  .  j ava2  s .  c  o m
            public Void doInTransaction(TransactionStatus status) {
                runDestroyScripts();
                return null;
            }
        });
    } else {
        runDestroyScripts();
    }
}

From source file:com.mothsoft.alexis.engine.retrieval.TwitterRetrievalTaskImpl.java

private List<Source> findSourcesToProcess() {
    try {/*from  w  ww. java 2s.  co m*/
        return this.transactionTemplate.execute(new TransactionCallback<List<Source>>() {

            public List<Source> doInTransaction(TransactionStatus status) {
                return TwitterRetrievalTaskImpl.this.sourceDao
                        .listSourcesWithRetrievalDateMoreThanXMinutesAgo(15, TwitterSource.class);

            }
        });
    } catch (final Exception e) {
        logger.error("Listing sources for retrieval failed: " + e, e);
        return Collections.emptyList();
    }
}

From source file:org.motechproject.server.omod.sdsched.TxSyncManWrapperImplTest.java

public void testUnbindResource() {
    final String resourceName = "A Resource";
    final String resourceValue = "A Resource value";
    Object retVal = txTempl.execute(new TransactionCallback() {
        public Object doInTransaction(TransactionStatus status) {
            try {
                TransactionSynchronizationManager.bindResource(resourceName, resourceValue);
                txSyncManWrapper.unbindResource(resourceName);
                return txSyncManWrapper.getResource(resourceName);
            } finally {
                TransactionSynchronizationManager.unbindResourceIfPossible(resourceName);
            }/*from   ww w  . ja v  a 2s  . com*/
        }
    });
    assertNull(resourceValue, retVal);
}

From source file:org.openremote.beehive.configuration.www.DevicesAPI.java

@PUT
@Path("/{deviceId}")
public Response udpateDevice(@PathParam("deviceId") Long deviceId, final DeviceDTOIn deviceDTO) {
    final Device existingDevice = account.getDeviceById(deviceId);

    Device optionalDeviceWithSameName = account.getDeviceByName(deviceDTO.getName());

    if (optionalDeviceWithSameName != null
            && !optionalDeviceWithSameName.getId().equals(existingDevice.getId())) {
        return Response.status(Response.Status.CONFLICT)
                .entity(new ErrorDTO(409, "A device with the same name already exists")).build();
    }//from   w  ww  .j a v  a  2s  .c o m

    return Response.ok(new DeviceDTOOut(
            new TransactionTemplate(platformTransactionManager).execute(new TransactionCallback<Device>() {
                @Override
                public Device doInTransaction(TransactionStatus transactionStatus) {
                    existingDevice.setName(deviceDTO.getName());
                    existingDevice.setModel(deviceDTO.getModel());
                    existingDevice.setVendor(deviceDTO.getVendor());
                    deviceRepository.save(existingDevice);
                    return existingDevice;
                }
            }))).build();
}

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

@Test
public void retrieveEntityWhenAlreadyInsSessionOnAccountOfSave() {
    List<System> dbContainer = transactionTemplate.execute(new TransactionCallback<List<System>>() {
        public List<System> doInTransaction(TransactionStatus status) {
            Holder holder = new Holder();
            System system = new System();
            system.setName("system1");
            system.setSystemNumber("1");
            SubSystem subSystem1 = new SubSystem();
            subSystem1.setName("subsystem1");
            subSystem1.setParent(system);
            subSystem1.setSystemNumber("1-1");
            SubSystem subSystem2 = new SubSystem();
            subSystem2.setName("subsystem2");
            subSystem2.setParent(system);
            subSystem2.setSystemNumber("1-2");
            holder.setSystem(system);/*from   ww w.ja  va 2 s.c o  m*/
            List<SubSystem> subSystems = new ArrayList<SubSystem>();
            subSystems.add(subSystem1);
            subSystems.add(subSystem2);
            system.setSubSystems(subSystems);
            sessionFactory.getCurrentSession().save(subSystem1);
            sessionFactory.getCurrentSession().save(subSystem2);
            sessionFactory.getCurrentSession().save(system);
            sessionFactory.getCurrentSession().save(holder);

            List<System> retrievedSystems = (List<System>) sessionFactory.getCurrentSession()
                    .createCriteria(System.class).addOrder(Order.asc("systemNumber")).list();
            LazyLoadingUtil.deepHydrate(sessionFactory.getCurrentSession(), system);
            return retrievedSystems;
        }
    });
}

From source file:org.pentaho.custom.authentication.provider.userroledao.hibernate.UserRoleDaoTransactionDecorator.java

public IUser getUser(final String username) throws UncategorizedUserRoleDaoException {
    return (IUser) transactionTemplate.execute(new TransactionCallback() {
        public Object doInTransaction(TransactionStatus status) {
            return userRoleDao.getUser(username);
        }/*  ww  w.  ja  v a  2  s .c o  m*/
    });
}

From source file:jp.go.aist.six.util.core.persist.castor.CastorDatastore.java

public <K, T extends Persistable<K>> List<T> syncAll(final Class<T> type, final List<? extends T> objects) {
    List<T> p_objects = _executeTx("syncAll", type, new TransactionCallback<List<T>>() {
        public List<T> doInTransaction(final TransactionStatus status) {
            return getDao(type).syncAll(objects);
        }//  w w  w. jav  a2 s.co  m
    });

    return p_objects;
}

From source file:com.mothsoft.alexis.engine.numeric.TopicActivityDataSetImporter.java

private void importTopicDataForUser(final Long userId, final Date startDate, final Date endDate) {
    logger.debug(String.format("Importing topic activity for user: %d between %s and %s", userId,
            startDate.toString(), endDate.toString()));

    final List<Long> topicIds = this.transactionTemplate.execute(new TransactionCallback<List<Long>>() {
        @SuppressWarnings("unchecked")
        @Override//ww  w  . j  av  a2  s.com
        public List<Long> doInTransaction(TransactionStatus txStatus) {
            final Query query = TopicActivityDataSetImporter.this.em
                    .createQuery("SELECT id FROM Topic WHERE userId = :userId ORDER BY id ASC");
            query.setParameter("userId", userId);
            return query.getResultList();
        }
    });

    BigInteger total = BigInteger.ZERO;

    for (final Long topicId : topicIds) {
        BigInteger count = importTopicDataForTopic(topicId, startDate, endDate);
        total = total.add(count);
    }

    recordAggregateTopicActivity(userId, startDate, total);

}

From source file:com.mothsoft.alexis.engine.predictive.OpenNLPMaxentModelExecutorTask.java

private boolean execute(final Long modelId) {
    return this.transactionTemplate.execute(new TransactionCallback<Boolean>() {
        @Override/*ww  w  . jav a 2 s.  com*/
        public Boolean doInTransaction(TransactionStatus arg0) {
            final Model model = OpenNLPMaxentModelExecutorTask.this.modelDao.get(modelId);
            return OpenNLPMaxentModelExecutorTask.this.doExecute(model);
        }
    });
}