List of usage examples for org.springframework.transaction.support TransactionCallbackWithoutResult TransactionCallbackWithoutResult
TransactionCallbackWithoutResult
From source file:org.openvpms.archetype.rules.stock.ChargeStockUpdaterTestCase.java
/** * Verifies that the stock is updated correctly if referred to by two different items in a transaction. *//*from ww w .j ava 2s . com*/ @Test public void testMultipleStockUpdatesInTxn() { final List<FinancialAct> acts = new ArrayList<FinancialAct>(createInvoice()); final FinancialAct item1 = acts.get(1); final FinancialAct item2 = FinancialTestHelper.createChargeItem(CustomerAccountArchetypes.INVOICE_ITEM, patient, product, BigDecimal.ONE); addStockLocation(item2); acts.add(item2); BigDecimal initialQuantity = BigDecimal.ZERO; BigDecimal quantity = BigDecimal.valueOf(5); item1.setQuantity(quantity); item2.setQuantity(quantity); checkEquals(initialQuantity, getStock(stockLocation, product)); BigDecimal expected = getQuantity(initialQuantity, quantity.add(quantity), false); TransactionTemplate template = new TransactionTemplate(txnManager); template.execute(new TransactionCallbackWithoutResult() { @Override protected void doInTransactionWithoutResult(TransactionStatus status) { save(acts); } }); checkEquals(expected, getStock(stockLocation, product)); template.execute(new TransactionCallbackWithoutResult() { @Override protected void doInTransactionWithoutResult(TransactionStatus status) { item1.setQuantity(BigDecimal.ONE); save(item1); remove(item2); } }); expected = getQuantity(initialQuantity, BigDecimal.ONE, false); checkEquals(expected, getStock(stockLocation, product)); }
From source file:com.hs.mail.imap.mailbox.DefaultMailboxManager.java
public void deleteMailbox(final long ownerID, final long mailboxID, final boolean delete) { getTransactionTemplate().execute(new TransactionCallbackWithoutResult() { public void doInTransactionWithoutResult(TransactionStatus status) { try { MailboxDao dao = DaoFactory.getMailboxDao(); List<PhysMessage> danglings = dao.getDanglingMessageIDList(ownerID, mailboxID); // We must evict objects for these deleted messages. // But, how we can do it? dao.deleteMessages(ownerID, mailboxID); if (delete) { dao.deleteMailbox(ownerID, mailboxID); } else { // prevent the mailbox from selecting dao.forbidSelectMailbox(ownerID, mailboxID); }/*from w ww .j av a 2 s . c om*/ if (CollectionUtils.isNotEmpty(danglings)) { for (PhysMessage pm : danglings) { if (hdCache != null) { hdCache.remove(pm.getPhysMessageID()); } deletePhysicalMessage(pm); } } } catch (DataAccessException ex) { status.setRollbackOnly(); throw ex; } } }); }
From source file:org.jspresso.hrsample.backend.JspressoUnitOfWorkTest.java
/** * Clone/merge of entity lists with holes. See bug 757. *///from w w w . j a v a 2 s. co m @Test public void testCloneEntityListWithHoles() { final HibernateBackendController hbc = (HibernateBackendController) getBackendController(); EnhancedDetachedCriteria employeeCriteria = EnhancedDetachedCriteria.forClass(Employee.class); final Employee emp = hbc.findFirstByCriteria(employeeCriteria, EMergeMode.MERGE_KEEP, Employee.class); List<Event> events = new ArrayList<Event>(); events.add(hbc.getEntityFactory().createEntityInstance(Event.class)); events.add(null); events.add(hbc.getEntityFactory().createEntityInstance(Event.class)); emp.setAlternativeEvents(events); hbc.getTransactionTemplate().execute(new TransactionCallbackWithoutResult() { @Override protected void doInTransactionWithoutResult(TransactionStatus status) { hbc.cloneInUnitOfWork(emp); } }); emp.addToAlternativeEvents(hbc.getEntityFactory().createEntityInstance(Event.class)); hbc.getTransactionTemplate().execute(new TransactionCallbackWithoutResult() { @Override protected void doInTransactionWithoutResult(TransactionStatus status) { hbc.cloneInUnitOfWork(emp); } }); }
From source file:com.mothsoft.alexis.engine.numeric.TopicActivityDataSetImporter.java
private BigInteger importTopicDataForTopic(final Long topicId, final Date startDate, final Date endDate) { logger.debug(String.format("Importing topic activity for topic: %d between %s and %s", topicId, startDate.toString(), endDate.toString())); final String queryString = "SELECT DATE_FORMAT(td.creation_date, '%Y-%m-%d %H:00:00') as the_hour, " + " COUNT(td.id) from topic_document td INNER JOIN topic on topic.id = td.topic_id " + " WHERE td.creation_date >= ? AND td.creation_date <= ? AND td.topic_id = ? " + " GROUP BY the_hour ORDER BY td.creation_date"; final BigInteger count = this.transactionTemplate.execute(new TransactionCallback<BigInteger>() { @Override/*from w w w . j av a2 s . c om*/ public BigInteger doInTransaction(TransactionStatus txStatus) { final Query query = TopicActivityDataSetImporter.this.em.createNativeQuery(queryString); query.setParameter(1, startDate); query.setParameter(2, endDate); query.setParameter(3, topicId); final List<?> results = query.getResultList(); if (results == null || results.isEmpty()) { return BigInteger.ZERO; } else { final Object[] array = (Object[]) results.get(0); return (BigInteger) array[1]; } } }); logger.debug("Data set point: (" + startDate + ", " + count + ")"); this.transactionTemplate.execute(new TransactionCallbackWithoutResult() { @Override protected void doInTransactionWithoutResult(TransactionStatus status) { TopicActivityDataSet dataSet = TopicActivityDataSetImporter.this.dataSetDao .findTopicActivityDataSet(topicId); if (dataSet == null) { final DataSetType type = TopicActivityDataSetImporter.this.dataSetTypeDao .findSystemDataSetType(DataSetType.TOPIC_ACTIVITY); final Topic topic = TopicActivityDataSetImporter.this.topicDao.get(topicId); dataSet = new TopicActivityDataSet(topic, type); TopicActivityDataSetImporter.this.em.persist(dataSet); } final DataSetPoint point = new DataSetPoint(dataSet, startDate, count.doubleValue()); TopicActivityDataSetImporter.this.dataSetPointDao.add(point); } }); return count; }
From source file:se.inera.intyg.intygstjanst.web.service.bean.IntygBootstrapBean.java
private void addSjukfall(final Resource metadata, final Resource content) { try {//from w w w . j a v a 2s .c om Certificate certificate = new CustomObjectMapper().readValue(metadata.getInputStream(), Certificate.class); if (!isSjukfallsGrundandeIntyg(certificate.getType())) { return; } transactionTemplate.execute(new TransactionCallbackWithoutResult() { @Override protected void doInTransactionWithoutResult(TransactionStatus status) { try { Certificate certificate = new CustomObjectMapper().readValue(metadata.getInputStream(), Certificate.class); certificate.setDocument(IOUtils.toString(content.getInputStream(), "UTF-8")); ModuleApi moduleApi = moduleRegistry.getModuleApi(certificate.getType()); Utlatande utlatande = moduleApi.getUtlatandeFromJson(certificate.getDocument()); if (certificateToSjukfallCertificateConverter.isConvertableFk7263(utlatande)) { SjukfallCertificate sjukfallCertificate = certificateToSjukfallCertificateConverter .convertFk7263(certificate, utlatande); entityManager.persist(sjukfallCertificate); } } catch (Throwable t) { status.setRollbackOnly(); LOG.error("Loading of Sjukfall intyg failed for {}: {}", metadata.getFilename(), t.getMessage()); } } }); } catch (IOException e) { e.printStackTrace(); } }
From source file:com.mothsoft.alexis.engine.retrieval.DocumentRetrievalTaskImpl.java
private void onErrorState(final Long documentId, final DocumentState errorState) { logger.warn("Setting error state: " + errorState.toString() + " on document " + documentId); this.transactionTemplate.execute(new TransactionCallbackWithoutResult() { @Override/*from w w w. j a va 2 s . com*/ protected void doInTransactionWithoutResult(TransactionStatus txStatus) { final Document attachedDocument = DocumentRetrievalTaskImpl.this.documentDao.get(documentId); attachedDocument.onErrorState(errorState); DocumentRetrievalTaskImpl.this.documentDao.update(attachedDocument); } }); }
From source file:org.jspresso.hrsample.backend.JspressoModelTest.java
/** * Tests adding twice the same entity to a list property. See bug 758. *//*from w w w . j ava 2 s.co m*/ @Test(expected = ComponentException.class) public void testAddToListTwice() { final HibernateBackendController hbc = (HibernateBackendController) getBackendController(); EnhancedDetachedCriteria employeeCriteria = EnhancedDetachedCriteria.forClass(Employee.class); final Employee emp = hbc.findFirstByCriteria(employeeCriteria, EMergeMode.MERGE_KEEP, Employee.class); Event evt = emp.getEvents().get(0); emp.addToEvents(evt); assertSame(emp.getEvents().get(0), evt); assertSame(emp.getEvents().get(emp.getEvents().size() - 1), evt); hbc.getTransactionTemplate().execute(new TransactionCallbackWithoutResult() { @Override protected void doInTransactionWithoutResult(TransactionStatus status) { hbc.cloneInUnitOfWork(emp); } }); hbc.reload(emp); assertSame(emp.getEvents().get(0), evt); assertSame(emp.getEvents().get(emp.getEvents().size() - 1), evt); }
From source file:com.mothsoft.alexis.engine.numeric.President2012DataSetImporter.java
private void save(final Map<Date, PollResults> pollResultsByDate) { this.transactionTemplate.execute(new TransactionCallbackWithoutResult() { @Override/*ww w . jav a 2 s .c o m*/ protected void doInTransactionWithoutResult(final TransactionStatus txStatus) { final Map<String, DataSet> dataSets = new HashMap<String, DataSet>(); for (final Map.Entry<Date, PollResults> entry : pollResultsByDate.entrySet()) { final Date date = entry.getKey(); logger.debug("Date: " + date.toString()); final PollResults pollResults = entry.getValue(); for (final String choice : pollResults.getAvailableChoices()) { // use data set if cached DataSet dataSet = dataSets.get(choice); // find or create the data set if (dataSet == null) { dataSet = findOrCreateDataSet(choice); dataSets.put(choice, dataSet); } logger.debug(String.format("%s => %f", choice, pollResults.valueOf(choice))); // save the value final DataSetPoint point = new DataSetPoint(dataSet, date, pollResults.valueOf(choice)); President2012DataSetImporter.this.dataSetPointDao.add(point); } } } }); }
From source file:com.mothsoft.alexis.engine.predictive.OpenNLPMaxentModelTrainerTask.java
private void logError(final Long modelId, final Throwable t) { this.transactionTemplate.execute(new TransactionCallbackWithoutResult() { @Override/*from w w w .ja va 2s . c om*/ protected void doInTransactionWithoutResult(final TransactionStatus status) { logger.error("Model " + modelId + " training failed: " + t, t); final Model model = OpenNLPMaxentModelTrainerTask.this.modelDao.get(modelId); model.setState(ModelState.ERROR); } }); }
From source file:com.github.inspektr.audit.support.JdbcAuditTrailManager.java
public void clean() { this.transactionTemplate.execute(new TransactionCallbackWithoutResult() { protected void doInTransactionWithoutResult(final TransactionStatus transactionStatus) { final String sql = String.format(DELETE_SQL_TEMPLATE, tableName, cleanupCriteria); final List<?> params = cleanupCriteria.getParameterValues(); JdbcAuditTrailManager.this.logger.info("Cleaning audit records with query " + sql); JdbcAuditTrailManager.this.logger.debug("Query parameters: " + params); final int count = getJdbcTemplate().update(sql, params.toArray()); JdbcAuditTrailManager.this.logger.info(count + " records deleted."); }//w w w.j a v a2s . c om }); }