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

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

Introduction

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

Prototype

public TransactionTemplate(PlatformTransactionManager transactionManager) 

Source Link

Document

Construct a new TransactionTemplate using the given transaction manager.

Usage

From source file:cherry.sqlapp.service.sqltool.exec.ExecLoadFileProcessHandler.java

@Override
public FileProcessResult handleFile(final File file, String name, String originalFilename, String contentType,
        long size, long asyncId, String... args) throws IOException {
    final DataSource dataSource = dataSourceDef.getDataSource(args[0]);
    final String sql = args[1];
    TransactionOperations txOp = new TransactionTemplate(new DataSourceTransactionManager(dataSource));
    return txOp.execute(new TransactionCallback<FileProcessResult>() {
        @Override/*from www. ja  va 2s  .  c  o  m*/
        public FileProcessResult doInTransaction(TransactionStatus status) {
            try (InputStream in = new FileInputStream(file);
                    Reader reader = new InputStreamReader(in, charset)) {

                LoadResult r = loader.load(dataSource, sql, new CsvProvider(reader, true), new NoneLimiter());

                FileProcessResult result = new FileProcessResult();
                result.setTotalCount(r.getTotalCount());
                result.setOkCount(r.getSuccessCount());
                result.setNgCount(r.getFailedCount());
                return result;

            } catch (IOException ex) {
                throw new IllegalStateException(ex);
            }
        }
    });
}

From source file:org.terasoluna.tourreservation.domain.service.tourinfo.PriceCalculateSharedServiceImpl.java

@PostConstruct
public void loadAges() {
    // use TransactionTemplate to avoid SQLException which tells set-readonly is not allowed
    // see https://github.com/terasolunaorg/terasoluna-tourreservation/issues/163
    TransactionTemplate transactionTemplate = new TransactionTemplate(transactionManager);
    transactionTemplate.execute(new TransactionCallbackWithoutResult() {
        @Override/* w w w.  j a va2s  .c  om*/
        protected void doInTransactionWithoutResult(TransactionStatus status) {
            childAge = ageRepository.findOne("1");
            adultAge = ageRepository.findOne("0");
        }
    });
}

From source file:com.mothsoft.alexis.engine.textual.TopicDocumentMatcherImpl.java

public void setTransactionManager(final PlatformTransactionManager transactionManager) {
    this.transactionTemplate = new TransactionTemplate(transactionManager);
}

From source file:com.bitsofproof.supernode.core.GetHeadersHandler.java

@Override
public void process(GetHeadersMessage m, BitcoinPeer peer) {
    log.trace("received getheader from " + peer.getAddress());
    List<String> locator = new ArrayList<String>();
    for (byte[] h : m.getLocators()) {
        locator.add(new Hash(h).toString());
    }/*from  w  ww.  j av  a2 s.com*/
    List<String> inventory = store.getInventory(locator, new Hash(m.getStop()).toString(), 2000);
    final HeadersMessage hm = (HeadersMessage) peer.createMessage("headers");
    for (final String h : inventory) {
        new TransactionTemplate(transactionManager).execute(new TransactionCallbackWithoutResult() {
            @Override
            protected void doInTransactionWithoutResult(TransactionStatus status) {
                status.setRollbackOnly();

                Blk b;
                try {
                    b = store.getBlock(h);
                    if (b != null) {
                        WireFormat.Writer writer = new WireFormat.Writer();
                        b.toWireHeaderOnly(writer);
                        hm.getBlockHeader().add(writer.toByteArray());
                    }
                } catch (ValidationException e) {
                }
            }
        });
    }
    if (hm.getBlockHeader().size() > 0) {
        peer.send(hm);
        log.debug("sent  " + hm.getBlockHeader().size() + " block headers to " + peer.getAddress());
    }
}

From source file:org.osiam.auth.oauth_client.OsiamAuthServerClientProvider.java

@PostConstruct
private void createAuthServerClient() {
    TransactionTemplate tmpl = new TransactionTemplate(txManager);
    tmpl.execute(new TransactionCallbackWithoutResult() {
        @Override//from  w ww.j a  va2 s  .c o m
        protected void doInTransactionWithoutResult(TransactionStatus status) {
            ClientEntity clientEntity = null;

            try {
                clientEntity = clientDao.getClient(AUTH_SERVER_CLIENT_ID);
            } catch (ResourceNotFoundException e) {
                LOGGER.log(Level.INFO, "No auth server found. The auth server will be created.");
            }

            if (clientEntity == null) {
                int validity = 10;

                clientEntity = new ClientEntity();
                Set<String> scopes = new HashSet<String>();
                scopes.add(Scope.GET.toString());
                scopes.add(Scope.POST.toString());
                scopes.add(Scope.PATCH.toString());

                Set<String> grants = new HashSet<String>();
                grants.add(GrantType.CLIENT_CREDENTIALS.toString());

                clientEntity.setId(AUTH_SERVER_CLIENT_ID);
                clientEntity.setRefreshTokenValiditySeconds(validity);
                clientEntity.setAccessTokenValiditySeconds(validity);
                clientEntity.setRedirectUri(authServerHome);
                clientEntity.setScope(scopes);
                clientEntity.setImplicit(true);
                clientEntity.setValidityInSeconds(validity);
                clientEntity.setGrants(grants);

                clientEntity = clientDao.create(clientEntity);
            }

            authServerClientSecret = clientEntity.getClientSecret();
        }
    });
}

From source file:com.zonekey.ssm.common.log.appender.JdbcLogWriter.java

/**
 * ?PlatformTransactionManagertransactionTemplate.
 *///w  w  w.j  a v  a2  s.c  o  m
@Resource
public void setDefaultTransactionManager(PlatformTransactionManager defaultTransactionManager) {
    transactionTemplate = new TransactionTemplate(defaultTransactionManager);
}

From source file:org.openmrs.module.imbmetadata.deploy.bundle.ImbMetadataBundle.java

/**
 * Setting multiple GPs is much faster in a single transaction
 */// w  w w  . j  a v a  2s . c o m
protected void setGlobalProperties(final Map<String, String> properties) {
    TransactionTemplate transactionTemplate = new TransactionTemplate(platformTransactionManager);
    transactionTemplate.execute(new TransactionCallbackWithoutResult() {
        @Override
        protected void doInTransactionWithoutResult(TransactionStatus status) {
            for (Map.Entry<String, String> entry : properties.entrySet()) {
                setGlobalProperty(entry.getKey(), entry.getValue());
            }
        }
    });
}

From source file:ca.uhn.fhir.jpa.search.StaleSearchDeletingSvc.java

@Scheduled(fixedDelay = 10 * DateUtils.MILLIS_PER_SECOND)
@Transactional(propagation = Propagation.NOT_SUPPORTED)
public synchronized void pollForStaleSearches() {
    Date cutoff = new Date(System.currentTimeMillis() - myDaoConfig.getExpireSearchResultsAfterMillis());
    ourLog.debug("Searching for searches which are before {}", cutoff);

    Collection<Search> toDelete = mySearchDao.findWhereCreatedBefore(cutoff);
    if (toDelete.isEmpty()) {
        return;//ww w.  java  2  s  .com
    }

    TransactionTemplate tt = new TransactionTemplate(myTransactionManager);
    for (final Search next : toDelete) {
        tt.execute(new TransactionCallbackWithoutResult() {
            @Override
            protected void doInTransactionWithoutResult(TransactionStatus theArg0) {
                Search searchToDelete = mySearchDao.findOne(next.getId());
                ourLog.info("Expiring stale search {} / {}", searchToDelete.getId(), searchToDelete.getUuid());
                mySearchIncludeDao.deleteForSearch(searchToDelete.getId());
                mySearchResultDao.deleteForSearch(searchToDelete.getId());
                mySearchDao.delete(searchToDelete);
            }
        });
    }

    ourLog.info("Deleted {} searches, {} remaining", toDelete.size(), mySearchDao.count());

}

From source file:com.example.test.HibernateBookRepositoryTest.java

@Before
public void setUp() {
    transactionTemplate = new TransactionTemplate(transactionManager);
    transactionTemplate.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRES_NEW);
}

From source file:se.inera.intyg.intygstjanst.web.integration.test.CertificateResource.java

@Autowired
public void setTxManager(PlatformTransactionManager txManager) {
    this.transactionTemplate = new TransactionTemplate(txManager);
}