List of usage examples for org.springframework.transaction.support TransactionCallbackWithoutResult TransactionCallbackWithoutResult
TransactionCallbackWithoutResult
From source file:org.jspresso.hrsample.backend.JspressoUnitOfWorkTest.java
@Test public void testTranslationComponentUpdate() { final HibernateBackendController hbc = (HibernateBackendController) getBackendController(); EnhancedDetachedCriteria compCrit = EnhancedDetachedCriteria.forClass(Company.class); final Company company = hbc.findFirstByCriteria(compCrit, EMergeMode.MERGE_KEEP, Company.class); hbc.getTransactionTemplate().execute(new TransactionCallbackWithoutResult() { @Override//from w w w .ja v a 2s .c om public void doInTransactionWithoutResult(TransactionStatus status) { Company companyClone = hbc.cloneInUnitOfWork(company); companyClone.setName("Translation"); } }); for (Company.Translation translation : company.getPropertyTranslations()) { if ("Translation".equals(translation.getTranslatedValue())) { translation.setTranslatedValue("UpdatedTranslation"); } } IPropertyTranslation translationBefore = null; for (Company.Translation translation : company.getPropertyTranslations()) { if ("UpdatedTranslation".equals(translation.getTranslatedValue())) { translationBefore = translation; } } hbc.getTransactionTemplate().execute(new TransactionCallbackWithoutResult() { @Override public void doInTransactionWithoutResult(TransactionStatus status) { hbc.cloneInUnitOfWork(company); } }); IPropertyTranslation translationAfter = null; for (Company.Translation translation : company.getPropertyTranslations()) { if ("UpdatedTranslation".equals(translation.getTranslatedValue())) { translationAfter = translation; } } assertSame("translationBefore/After should be the same reference", translationBefore, translationAfter); Company reloadedCompany = hbc.findById(company.getId(), EMergeMode.MERGE_CLEAN_EAGER, Company.class); assertEquals("Translation component not correctly updated", "UpdatedTranslation", reloadedCompany.getName()); IPropertyTranslation translationReloaded = null; for (Company.Translation translation : reloadedCompany.getPropertyTranslations()) { if ("UpdatedTranslation".equals(translation.getTranslatedValue())) { translationReloaded = translation; } } assertNotNull("Updated translation not found", translationReloaded); assertSame("translationBefore/After/Reloaded should be the same reference", translationAfter, translationReloaded); }
From source file:nl.ordina.bag.etl.dao.AbstractBAGDAO.java
@Override public void insertStandplaatsen(final List<Standplaats> standplaatsen) throws DAOException { try {//from ww w . ja v a 2 s .co m transactionTemplate.execute(new TransactionCallbackWithoutResult() { @Override protected void doInTransactionWithoutResult(TransactionStatus status) { jdbcTemplate.batchUpdate("insert into bag_standplaats (" + "bag_standplaats_id," + "aanduiding_record_inactief," + "aanduiding_record_correctie," + "officieel," + "standplaats_status," + "standplaats_geometrie," + "begindatum_tijdvak_geldigheid," + "einddatum_tijdvak_geldigheid," + "in_onderzoek," + "bron_documentdatum," + "bron_documentnummer," + "bag_nummeraanduiding_id" + ") values (?,?,?,?,?,?,?,?,?,?,?,?)", new BatchPreparedStatementSetter() { @Override public void setValues(PreparedStatement ps, int i) throws SQLException { ps.setLong(1, standplaatsen.get(i).getIdentificatie()); ps.setInt(2, standplaatsen.get(i).getAanduidingRecordInactief().ordinal()); ps.setLong(3, standplaatsen.get(i).getAanduidingRecordCorrectie()); ps.setInt(4, standplaatsen.get(i).getOfficieel().ordinal()); ps.setInt(5, standplaatsen.get(i).getStandplaatsStatus().ordinal()); ps.setString(6, standplaatsen.get(i).getStandplaatsGeometrie()); ps.setTimestamp(7, new Timestamp( standplaatsen.get(i).getBegindatumTijdvakGeldigheid().getTime())); if (standplaatsen.get(i).getEinddatumTijdvakGeldigheid() == null) ps.setNull(8, Types.TIMESTAMP); else ps.setTimestamp(8, new Timestamp( standplaatsen.get(i).getEinddatumTijdvakGeldigheid().getTime())); ps.setInt(9, standplaatsen.get(i).getInOnderzoek().ordinal()); ps.setDate(10, new Date(standplaatsen.get(i).getDocumentdatum().getTime())); ps.setString(11, standplaatsen.get(i).getDocumentnummer()); ps.setLong(12, standplaatsen.get(i).getHoofdAdres()); } @Override public int getBatchSize() { return standplaatsen.size(); } }); insertNevenadressen(TypeAdresseerbaarObject.STANDPLAATS, standplaatsen); } }); } catch (DataAccessException e) { throw new DAOException("Error inserting standplaatsen", e); } }
From source file:database.DataLoader.java
private void moveDirections() throws SQLException, ClassNotFoundException, Exception { try {/*from w w w. j a v a 2s . c om*/ final String tableName = SPHERE; final ResultSet set = getFromOldBase(getSelectAll(tableName)); while (set.next()) { TransactionTemplate temp = new TransactionTemplate(transactionManager); temp.execute(new TransactionCallbackWithoutResult() { @Override protected void doInTransactionWithoutResult(TransactionStatus ts) { Long oldId = null; try { oldId = set.getLong("id"); String name = set.getString("name"); Direction dir = new Direction(); dir.setName(name); saveObjectAndLink(dir, oldId, tableName); } catch (Exception e) { addErrorMessage("direction: " + oldId + " " + StringAdapter.getStackExeption(e)); log.warn("direction: " + oldId + " " + StringAdapter.getStackExeption(e)); } } }); } } catch (Throwable th) { log.warn("moveDirections " + StringAdapter.getStackExeption(th)); } }
From source file:org.jspresso.hrsample.backend.JspressoUnitOfWorkTest.java
/** * Test multiple uow cloning./*from w w w .j av a 2 s . co m*/ */ @Test(timeout = 300) public void testMultipleUOWCloning() { final HibernateBackendController hbc = (HibernateBackendController) getBackendController(); EnhancedDetachedCriteria empCrit = EnhancedDetachedCriteria.forClass(Employee.class); final List<Employee> employees = hbc.findByCriteria(empCrit, EMergeMode.MERGE_KEEP, Employee.class); hbc.getTransactionTemplate().execute(new TransactionCallbackWithoutResult() { @Override public void doInTransactionWithoutResult(TransactionStatus status) { List<Employee> employeeClones = hbc.cloneInUnitOfWork(employees); List<Employee> newEmployeeClones = null; for (int i = 0; i < 100; i++) { newEmployeeClones = hbc.cloneInUnitOfWork(employees); } for (int i = 0; i < employeeClones.size(); i++) { assertSame("Multiple cloning did not clone to the same instance. Cloning is not idempotent.", employeeClones.get(i), newEmployeeClones.get(i)); } for (int i = 0; i < 100; i++) { newEmployeeClones = hbc.cloneInUnitOfWork(employeeClones); } for (int i = 0; i < employeeClones.size(); i++) { assertSame("Multiple cloning did not clone to the same instance. Cloning is not idempotent.", employeeClones.get(i), newEmployeeClones.get(i)); } } }); }
From source file:database.DataLoader.java
private void moveAuthorToDirections() throws SQLException, ClassNotFoundException, Exception { try {//from w w w . j a v a 2 s. com final String tableName = AUTHOR_SPHERE; final ResultSet set = getFromOldBase(getSelectAll(tableName)); while (set.next()) { TransactionTemplate temp = new TransactionTemplate(transactionManager); temp.execute(new TransactionCallbackWithoutResult() { @Override protected void doInTransactionWithoutResult(TransactionStatus ts) { Long oldAuthorId = 0L; Long oldSphereId = 0L; try { oldAuthorId = set.getLong("author_id"); oldSphereId = set.getLong("sphere_id"); Long newAuthorId = getNewId(oldAuthorId, USERS); Long newSphereId = getNewId(oldSphereId, SPHERE); Direction dir = directionDao.find(newSphereId); Author author = authorDao.find(newAuthorId); List<Direction> directionList = author.getDirections(); if (directionList == null) { directionList = new ArrayList(); } directionList.add(dir); author.setDirections(directionList); // ?? , ? ?? , dao authorDao.update(author); } catch (Exception e) { ts.setRollbackOnly(); addErrorMessage("authorTodirection: " + oldAuthorId + " " + oldSphereId + " " + StringAdapter.getStackExeption(e)); log.warn("authorTodirection: " + oldAuthorId + " " + oldSphereId + " " + StringAdapter.getStackExeption(e)); } } }); } } catch (Throwable th) { log.warn("moveAuthorToDirections " + StringAdapter.getStackExeption(th)); } }
From source file:service.OrderService.java
@Scheduled(fixedDelay = 60000) public void unloadInShopInCron() { final TransactionTemplate transactionTemplate = new TransactionTemplate(transactionManager); transactionTemplate.execute(new TransactionCallbackWithoutResult() { protected void doInTransactionWithoutResult(TransactionStatus status) { try { List<Order> orders = orderDao.getAssignedUnloadedInShop(); for (Order order : orders) { ServiceResult res = new ServiceResult(); unloadInShop(order, res); if (res.hasErrors()) { String error = res.getErrors().toString(); log.error("unloadInShop " + error); order.setErrorUnloading(error); orderDao.update(order); } else { order.setErrorUnloading(""); orderDao.update(order); }// w w w . ja va 2 s .c o m } } catch (Throwable e) { log.error("unloadInShop " + StringAdapter.getStackExeption(e)); } } }); }
From source file:database.DataLoader.java
private void moveAuthorViews() throws Exception { try {/*from w w w . j av a 2 s . c om*/ final String tableName = AUTHOR_VIEWS; final ResultSet set = getFromOldBase(getSelectAll(tableName)); final List<OrderView> allList = orderViewDao.getAll(); while (set.next()) { TransactionTemplate temp = new TransactionTemplate(transactionManager); temp.execute(new TransactionCallbackWithoutResult() { @Override protected void doInTransactionWithoutResult(TransactionStatus ts) { Long oldOrderId = 0L; Long oldAuthorId = 0L; try { oldOrderId = set.getLong("order_id"); oldAuthorId = set.getLong("author_id"); Long newOrderId = getNewId(oldOrderId, ORDER); Long newAuthorId = getNewId(oldAuthorId, USERS); Order order = getOrder(newOrderId); User user = getUser(newAuthorId); if (!contains(allList, user, order)) { OrderView orderView = new OrderView(); orderView.setOrder(order); orderView.setUser(user); orderViewDao.save(orderView); } } catch (Throwable e) { ts.setRollbackOnly(); addErrorMessage("moveAuthorViews: " + oldOrderId + " " + oldAuthorId + " " + StringAdapter.getStackExeption(e)); log.warn("moveAuthorViews: " + oldOrderId + " " + oldAuthorId + " " + StringAdapter.getStackExeption(e)); } } }); } } catch (Throwable th) { log.warn("moveAuthorViews " + StringAdapter.getStackExeption(th)); } }
From source file:nl.strohalm.cyclos.services.transactions.PaymentServiceImpl.java
@Override public void notifyTransferProcessed(final Transfer transfer) { if (transfer.getProcessDate() == null) { // Transfer is not processed return;/* w ww . j a v a 2 s. co m*/ } final Collection<TransferListener> listeners = getTransferListeners(transfer); if (CollectionUtils.isNotEmpty(listeners)) { CurrentTransactionData.addTransactionCommitListener(new TransactionCommitListener() { @Override public void onTransactionCommit() { transactionHelper.runInCurrentThread(new TransactionCallbackWithoutResult() { @Override protected void doInTransactionWithoutResult(final TransactionStatus status) { Transfer fetchedTransfer = fetchService.fetch(transfer, Payment.Relationships.FROM, Payment.Relationships.TO); for (TransferListener listener : listeners) { try { listener.onTransferProcessed(fetchedTransfer); } catch (Exception e) { LOG.warn("Error running TransferListener " + listener, e); } } } }); } }); } }
From source file:database.DataLoader.java
private void moveOrdersToDirections() throws SQLException, ClassNotFoundException, Exception { try {// w w w . j a v a 2 s. c o m final String tableName = ORDER_SPHERE; final ResultSet set = getFromOldBase(getSelectAll(tableName)); while (set.next()) { TransactionTemplate temp = new TransactionTemplate(transactionManager); temp.execute(new TransactionCallbackWithoutResult() { @Override protected void doInTransactionWithoutResult(TransactionStatus ts) { Long oldOrderId = 0L; Long oldSphereId = 0L; try { oldOrderId = set.getLong("order_id"); oldSphereId = set.getLong("sphere_id"); Long newOrderId = getNewId(oldOrderId, ORDER); Long newSphereId = getNewId(oldSphereId, SPHERE); Direction dir = directionDao.find(newSphereId); Order order = orderDao.find(newOrderId); List<Direction> directionList = order.getDirections(); if (directionList == null) { directionList = new ArrayList(); } if (!contains(directionList, dir)) { directionList.add(dir); order.setDirections(directionList); // ?? , ? ?? , dao orderDao.update(order); } } catch (Throwable e) { ts.setRollbackOnly(); addErrorMessage("moveOrdersToDirections: " + oldOrderId + " " + oldSphereId + " " + StringAdapter.getStackExeption(e)); log.warn("moveOrdersToDirections: " + oldOrderId + " " + oldSphereId + " " + StringAdapter.getStackExeption(e)); } } }); } } catch (Throwable th) { log.warn("moveOrdersToDirections " + StringAdapter.getStackExeption(th)); } }
From source file:database.DataLoader.java
private void moveAuthorSalary() throws Exception { try {// w w w . j a v a2s . c om final String tableName = Z_BID; final ResultSet set = getFromOldBase(getSelectAll(tableName)); while (set.next()) { TransactionTemplate temp = new TransactionTemplate(transactionManager); temp.execute(new TransactionCallbackWithoutResult() { @Override protected void doInTransactionWithoutResult(TransactionStatus ts) { Long oldId = 0L; try { oldId = set.getLong("id"); Long oldOrderId = set.getLong("order_id"); Long oldAuthorId = set.getLong("author_id"); Double cost = set.getDouble("reward"); Long newOrderId = getNewId(oldOrderId, ORDER); Long newAuthorId = getNewId(oldAuthorId, USERS); Order order = orderDao.find(newOrderId); Author author = authorDao.find(newAuthorId); AuthorSalary authorSalary = new AuthorSalary(); authorSalary.setOrder(order); authorSalary.setAuthor(author); authorSalary.setCost(cost); saveObjectAndLink(authorSalary, oldId, tableName); } catch (Exception e) { ts.setRollbackOnly(); addErrorMessage("authorSalary: " + oldId + " " + StringAdapter.getStackExeption(e)); log.warn("authorSalary: " + oldId + " " + StringAdapter.getStackExeption(e)); } } }); } } catch (Throwable th) { log.warn("moveAuthorToDirections " + StringAdapter.getStackExeption(th)); } }