List of usage examples for org.springframework.transaction.support TransactionCallback TransactionCallback
TransactionCallback
From source file:org.apache.camel.processor.aggregate.jdbc.OptimisticLockingJdbcAggregationRepository.java
@Override public Set<String> getKeys() { return getTransactionTemplate().execute(new TransactionCallback<LinkedHashSet<String>>() { @Override/* w w w .j a v a 2 s . c om*/ public LinkedHashSet<String> doInTransaction(TransactionStatus status) { List<String> keys = getJdbcTemplate().query( String.format("SELECT %s FROM %s", ID_KEY, getRepositoryName()), new RowMapper<String>() { @Override public String mapRow(ResultSet rs, int rowNum) throws SQLException { String id = rs.getString(ID_KEY); log.trace(String.format("getKey [%s]", id)); return id; } }); return new LinkedHashSet<>(keys); } }); }
From source file:org.ensembl.gti.seqstore.database.JdbcSeqStore.java
@Override public void storeGenomeSequence(long sessionId, GenomeSequence genomeSequence) { log.debug("Storing genome sequence " + genomeSequence.getStableId()); transactionTemplate.execute(new TransactionCallback<Void>() { @Override// ww w . ja v a2s.c o m public Void doInTransaction(TransactionStatus status) { String chk = storeSequence(sessionId, genomeSequence.getSequence()); template.update(STORE_GENOME_SEQUENCE_SQL, genomeSequence.getGenomeId(), genomeSequence.getStableId(), chk, sessionId); return null; } }); }
From source file:com.vladmihalcea.HibernateCriteriaTest.java
private List<ImageProductDTO> getImageProductDTOs() { return transactionTemplate.execute(new TransactionCallback<List<ImageProductDTO>>() { @Override/* ww w . ja va 2s. c o m*/ public List<ImageProductDTO> doInTransaction(TransactionStatus transactionStatus) { CriteriaBuilder cb = entityManager.getCriteriaBuilder(); CriteriaQuery<ImageProductDTO> query = cb.createQuery(ImageProductDTO.class); Root<Image> imageRoot = query.from(Image.class); Join<Image, Product> productJoin = imageRoot.join(Image_.product); query.distinct(true); List<Predicate> criteria = new ArrayList<Predicate>(); criteria.add(cb.like(cb.lower(productJoin.get(Product_.name)), "%tv%")); criteria.add(cb.gt(imageRoot.get(Image_.index), 0)); query.where(cb.and(criteria.toArray(new Predicate[criteria.size()]))); query.select(cb.construct(ImageProductDTO.class, imageRoot.get(Image_.name), productJoin.get(Product_.name))).orderBy(cb.asc(imageRoot.get(Image_.name))); return entityManager.createQuery(query).getResultList(); } }); }
From source file:com.github.rholder.spring.transaction.TransactionBindingSupportTest.java
@Test @Ignore("TransactionTemplate use here doesn't exactly match semantics of RetryingTransactionCallback yet") public void testReadWriteStateRetrieval() throws Exception { final TxnReadState[] postCommitReadState = new TxnReadState[1]; final TransactionListenerAdapter getReadStatePostCommit = new TransactionListenerAdapter() { @Override/*from w ww .j a v a 2 s . c o m*/ public void afterCommit() { postCommitReadState[0] = TransactionBindingSupport.getTransactionReadState(); } }; TransactionCallback callback = new TransactionCallback() { public Object doInTransaction(TransactionStatus status) { // Register to list to post-commit TransactionBindingSupport.bindListener(getReadStatePostCommit); return TransactionBindingSupport.getTransactionReadState(); } }; // TODO fix this behavior //TxnReadState a = (TxnReadState) transactionTemplate.execute(); // RetryingTransactionCallback<TxnReadState> getReadStateWork = new RetryingTransactionCallback<TxnReadState>() // { // public TxnReadState execute() throws Exception // { // // Register to list to post-commit // TransactionBindingSupport.bindListener(getReadStatePostCommit); // // return TransactionBindingSupport.getTransactionReadState(); // } // }; // Check TXN_NONE TxnReadState checkTxnReadState = TransactionBindingSupport.getTransactionReadState(); Assert.assertEquals("Expected 'no transaction'", TxnReadState.TXN_NONE, checkTxnReadState); Assert.assertNull("Expected no post-commit read state", postCommitReadState[0]); // Check TXN_READ_ONLY transactionTemplate.setReadOnly(true); checkTxnReadState = (TxnReadState) transactionTemplate.execute(callback); Assert.assertEquals("Expected 'read-only transaction'", TxnReadState.TXN_READ_ONLY, checkTxnReadState); Assert.assertEquals("Expected 'no transaction'", TxnReadState.TXN_NONE, postCommitReadState[0]); // check TXN_READ_WRITE transactionTemplate.setReadOnly(false); checkTxnReadState = (TxnReadState) transactionTemplate.execute(callback); Assert.assertEquals("Expected 'read-write transaction'", TxnReadState.TXN_READ_WRITE, checkTxnReadState); Assert.assertEquals("Expected 'no transaction'", TxnReadState.TXN_NONE, postCommitReadState[0]); }
From source file:edu.wisc.jmeter.dao.JdbcMonitorDao.java
@Override public HostStatus getHostStatus(final String hostName) { final Object lock = this.getHostLock(hostName); synchronized (lock) { HostStatus hostStatus = this.hostStatusCache.get(hostName); if (hostStatus != null) { return hostStatus; }//from ww w. j a v a2 s . c o m final Map<String, Object> params = new LinkedHashMap<String, Object>(); params.put("hostName", hostName); try { hostStatus = this.transactionTemplate.execute(new TransactionCallback<HostStatus>() { @Override public HostStatus doInTransaction(TransactionStatus transactionStatus) { final List<HostStatus> results = jdbcTemplate.query( "SELECT STATUS, FAILURE_COUNT, MESSAGE_COUNT, LAST_NOTIFICATION, LAST_UPDATED " + "FROM MONITOR_HOST_STATUS " + "WHERE HOST_NAME = :hostName", params, new RowMapper<HostStatus>() { @Override public HostStatus mapRow(ResultSet rs, int row) throws SQLException { final HostStatus hostStatus = new HostStatus(); hostStatus.setHost(hostName); hostStatus.setStatus(Status.valueOf(rs.getString("STATUS"))); hostStatus.setFailureCount(rs.getInt("FAILURE_COUNT")); hostStatus.setMessageCount(rs.getInt("MESSAGE_COUNT")); hostStatus.setLastMessageSent(rs.getTimestamp("LAST_NOTIFICATION")); hostStatus.setLastUpdated(rs.getTimestamp("LAST_UPDATED")); return hostStatus; } }); HostStatus hostStatus = DataAccessUtils.singleResult(results); if (hostStatus != null) { return hostStatus; } hostStatus = new HostStatus(); hostStatus.setHost(hostName); hostStatus.setLastUpdated(new Date()); params.put("status", hostStatus.getStatus().toString()); params.put("failureCount", hostStatus.getFailureCount()); params.put("messageCount", hostStatus.getMessageCount()); params.put("lastNotification", hostStatus.getLastMessageSent()); params.put("lastUpdated", hostStatus.getLastUpdated()); jdbcTemplate.update( "INSERT INTO MONITOR_HOST_STATUS (HOST_NAME, STATUS, FAILURE_COUNT, MESSAGE_COUNT, LAST_NOTIFICATION, LAST_UPDATED) " + "VALUES (:hostName, :status, :failureCount, :messageCount, :lastNotification, :lastUpdated)", params); return hostStatus; } }); } catch (RuntimeException re) { //Want things to still work if the database is broken so create an empty HostStatus to work with in memory only if (hostStatus == null) { hostStatus = new HostStatus(); hostStatus.setHost(hostName); hostStatus.setLastUpdated(new Date()); } log.warn("Failed to retrieve/create HostStatus via database, using memory storage only", re); } this.hostStatusCache.put(hostName, hostStatus); return hostStatus; } }
From source file:ca.uhn.fhir.jpa.dao.dstu3.FhirResourceDaoSubscriptionDstu3.java
@Scheduled(fixedDelay = DateUtils.MILLIS_PER_MINUTE) @Transactional(propagation = Propagation.NOT_SUPPORTED) @Override/*from w w w.j av a2 s. c o m*/ public void purgeInactiveSubscriptions() { if (getConfig().isSchedulingDisabled()) { return; } Long purgeInactiveAfterMillis = getConfig().getSubscriptionPurgeInactiveAfterMillis(); if (getConfig().isSubscriptionEnabled() == false || purgeInactiveAfterMillis == null) { return; } Date cutoff = new Date(System.currentTimeMillis() - purgeInactiveAfterMillis); Collection<SubscriptionTable> toPurge = mySubscriptionTableDao.findInactiveBeforeCutoff(cutoff); for (SubscriptionTable subscriptionTable : toPurge) { final IdDt subscriptionId = subscriptionTable.getSubscriptionResource().getIdDt(); ourLog.info("Deleting inactive subscription {} - Created {}, last client poll {}", new Object[] { subscriptionId.toUnqualified(), subscriptionTable.getCreated(), subscriptionTable.getLastClientPoll() }); TransactionTemplate txTemplate = new TransactionTemplate(myTxManager); txTemplate.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRES_NEW); txTemplate.execute(new TransactionCallback<Void>() { @Override public Void doInTransaction(TransactionStatus theStatus) { delete(subscriptionId, null); return null; } }); } }
From source file:com.opengamma.masterdb.batch.DbBatchWriter.java
public HbComputationTargetSpecification getComputationTarget(final ComputationTargetSpecification spec) { return getTransactionTemplateRetrying(getMaxRetries()) .execute(new TransactionCallback<HbComputationTargetSpecification>() { @Override//w ww .ja va2s . co m public HbComputationTargetSpecification doInTransaction(final TransactionStatus status) { return getComputationTargetIntransaction(spec); } }); }
From source file:com.devnexus.ting.core.service.impl.BusinessServiceImpl.java
@Override public Sponsor getSponsorWithPicture(final Long sponsorId) { final Sponsor sponsor = transactionTemplate.execute(new TransactionCallback<Sponsor>() { public Sponsor doInTransaction(TransactionStatus status) { return sponsorDao.getSponsorWithPicture(sponsorId); }//www . ja va 2 s . c o m }); return sponsor; }
From source file:org.ensembl.gti.seqstore.database.JdbcSeqStore.java
@Override public void clearSession(long sessionId) { log.debug("Clearing all entries for session " + sessionId); transactionTemplate.execute(new TransactionCallback<Void>() { @Override//from www . ja v a2 s. co m public Void doInTransaction(TransactionStatus status) { for (String obj : new String[] { "translation", "exon", "transcript", "gene", "genome_sequence", "genome" }) { String sql = CLEAR_SESSION_OBJ.replaceAll("OBJ", obj); template.update(sql, sessionId); } return null; } }); }
From source file:org.openvpms.archetype.rules.party.CustomerMergerTestCase.java
/** * Merges two customers in a transaction, and verifies the 'from' customer * has been deleted./* w w w.j a v a 2 s . c om*/ * * @param from the customer to merge from * @param to the customer to merge to * @return the merged customer */ private Party checkMerge(final Party from, final Party to) { template.execute(new TransactionCallback<Object>() { public Object doInTransaction(TransactionStatus transactionStatus) { customerRules.mergeCustomers(from, to); return null; } }); // verify the from customer has been deleted assertNull(get(from)); Party merged = get(to); assertNotNull(merged); return merged; }