List of usage examples for org.springframework.transaction.support TransactionTemplate TransactionTemplate
public TransactionTemplate(PlatformTransactionManager transactionManager)
From source file:database.DataLoader.java
private void moveOrders() throws SQLException, ClassNotFoundException, Exception { // /*from w w w .j ava 2s . c om*/ final String tableName = ORDER; final ResultSet orderSet = getFromOldBase(getSelectAll(tableName, "id")); while (orderSet.next()) { // ? TransactionTemplate temp = new TransactionTemplate(transactionManager); temp.execute(new TransactionCallbackWithoutResult() { @Override protected void doInTransactionWithoutResult(TransactionStatus ts) { Long oldId = 0L; try { oldId = orderSet.getLong("id"); String subject = orderSet.getString("theme"); if (subject.length() > 250) { subject = subject.substring(0, 250); } Long oldBranchId = orderSet.getLong("branch_id"); Long oldOrderTypeId = orderSet.getLong("type_id"); String city = orderSet.getString("university"); Double price = orderSet.getDouble("price"); Double authorSalary = orderSet.getDouble("reward"); Date redline = orderSet.getDate("redline"); Date deadline = orderSet.getDate("deadline"); Long oldAuthorId = orderSet.getLong("author_id"); Long clientId = orderSet.getLong("client_id"); String rewardComment = orderSet.getString("reward_comment"); Boolean flag_0 = orderSet.getBoolean("flag_0"); Boolean flag_1 = orderSet.getBoolean("flag_1"); Timestamp timestamp = orderSet.getTimestamp("ready_date"); String readyComment = orderSet.getString("ready_comment"); String comment = orderSet.getString("description"); Date readyDate = null; if (timestamp != null) { readyDate = new Date(timestamp.getTime()); } Long newBranchId = getNewId(oldBranchId, BRANCH); Branch branch = branchDao.find(newBranchId); Long newAuthorId = getNewId(oldAuthorId, USERS); Author author = authorDao.find(newAuthorId); Long newOrderTypeId = getNewId(oldOrderTypeId, TYPE); OrderType orderType = orderTypeDao.find(newOrderTypeId); Integer statusInt = orderSet.getInt("status"); OrderStatus status = getStatus(statusInt); if (status == null) { status = OrderStatus.NEW; } Date orderDate = getDateOrToday(orderSet, "created"); Order order = new Order(); order.setStatus(status); order.setOrderDate(orderDate); order.setAuthor(author); order.setBranch(branch); order.setOrderType(orderType); order.setSubject(subject); order.setCity(city); order.setCost(price); order.setAuthor_salary(authorSalary); // ? ? , ? order.setRealDate(deadline); order.setDeadlineDate(redline); order.setAuthorComment(readyComment); order.setComment(comment); order.setReadyDate(readyDate); order.setFirstFlag(flag_0); order.setSecondFlag(flag_1); order.setCommentToAuthorSalary(rewardComment); order.setOldId(StringAdapter.getString(oldId)); ResultSet clientSet = getFromOldBase("select * from " + CLIENT + " where id = " + clientId); String clientEmail = null; String clientFio = null; String clientPhone = null; while (clientSet.next()) { clientEmail = trim(clientSet.getString("email")); clientFio = clientSet.getString("firstname") + " " + clientSet.getString("fathername") + " " + clientSet.getString("lastname"); clientPhone = clientSet.getString("phone"); } if (ValidatorUtils.isEmail(clientEmail)) { order.setClientEmail(clientEmail); } order.setClientFio(clientFio); order.setClientPhone(clientPhone); saveObjectAndLink(order, oldId, tableName); } catch (Throwable e) { ts.setRollbackOnly(); addErrorMessage("order: " + oldId + " " + StringAdapter.getStackExeption(e)); log.warn("order: " + oldId + " " + StringAdapter.getStackExeption(e)); } } }); } }
From source file:dao.DatasetsDAO.java
public static ObjectNode getPagedDatasetComments(String userName, int id, int page, int size) { ObjectNode result = Json.newObject(); javax.sql.DataSource ds = getJdbcTemplate().getDataSource(); DataSourceTransactionManager tm = new DataSourceTransactionManager(ds); TransactionTemplate txTemplate = new TransactionTemplate(tm); result = txTemplate.execute(new TransactionCallback<ObjectNode>() { public ObjectNode doInTransaction(TransactionStatus status) { List<DatasetComment> pagedComments = getJdbcTemplate().query(GET_COMMENTS_BY_DATASET_ID, new DatasetCommentRowMapper(), id, (page - 1) * size, size); long count = 0; try { count = getJdbcTemplate().queryForObject("SELECT FOUND_ROWS()", Long.class); } catch (EmptyResultDataAccessException e) { Logger.error("Exception = " + e.getMessage()); }//from w w w .j ava 2 s . co m if (pagedComments != null) { for (DatasetComment dc : pagedComments) { if (StringUtils.isNotBlank(userName) && userName.equalsIgnoreCase(dc.authorUserName)) { dc.isAuthor = true; } } } ObjectNode resultNode = Json.newObject(); resultNode.set("comments", Json.toJson(pagedComments)); resultNode.put("count", count); resultNode.put("page", page); resultNode.put("itemsPerPage", size); resultNode.put("totalPages", (int) Math.ceil(count / ((double) size))); return resultNode; } }); return result; }
From source file:database.DataLoader.java
private void moveDirections() throws SQLException, ClassNotFoundException, Exception { try {/*from www . j av a 2s . co m*/ 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:database.DataLoader.java
private void moveAuthorToDirections() throws SQLException, ClassNotFoundException, Exception { try {/*from w w w . j a va2 s.c o m*/ 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:com.bitsofproof.supernode.core.CachedBlockStore.java
@Override public void issueColor(final StoredColor color) throws ValidationException { try {//from w w w.j a va 2 s.c o m // have to lock before transaction starts and unlock after finished. // otherwise updates to transient vs. persistent structures are out of sync for a // concurrent tx. This does not apply to methods using MANDATORY transaction annotation // context since they must have been invoked within a transaction. lock.writeLock().lock(); ValidationException e = new TransactionTemplate(transactionManager) .execute(new TransactionCallback<ValidationException>() { @Override public ValidationException doInTransaction(TransactionStatus status) { try { startBatch(); Tx tx = getTransaction(color.getTxHash()); if (tx == null) { throw new ValidationException("Unknown transaction for new color"); } TxOut out = tx.getOutputs().get(0); if (!out.isAvailable()) { throw new ValidationException("The color genesis was already spent."); } if (!ScriptFormat.isPayToAddress(out.getScript())) { throw new ValidationException("Color output should pay to address"); } List<Token> tokens = ScriptFormat.parse(out.getScript()); if (!Arrays.equals(tokens.get(2).data, Hash.keyHash(color.getPubkey()))) { throw new ValidationException("Color key does not match output address"); } if (!color.verify()) { storeColor(color); } else { throw new ValidationException("Color is not valid"); } updateColor(out, color.getFungibleName()); endBatch(); } catch (ValidationException e) { cancelBatch(); status.setRollbackOnly(); return e; } catch (Exception e) { cancelBatch(); status.setRollbackOnly(); return new ValidationException(e); } return null; } }); if (e != null) { throw e; } } finally { lock.writeLock().unlock(); } }
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); }//from w w w . j a v a 2 s . co 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 o m*/ 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:dao.DatasetsDAO.java
public static ObjectNode getPagedDatasetColumnComments(String userName, int datasetId, int columnId, int page, int size) { ObjectNode result = Json.newObject(); javax.sql.DataSource ds = getJdbcTemplate().getDataSource(); DataSourceTransactionManager tm = new DataSourceTransactionManager(ds); TransactionTemplate txTemplate = new TransactionTemplate(tm); result = txTemplate.execute(new TransactionCallback<ObjectNode>() { public ObjectNode doInTransaction(TransactionStatus status) { ObjectNode resultNode = Json.newObject(); long count = 0; int start = (page - 1) * size; int end = start + size; List<DatasetColumnComment> pagedComments = new ArrayList<DatasetColumnComment>(); List<Map<String, Object>> rows = null; rows = getJdbcTemplate().queryForList(GET_COLUMN_COMMENTS_BY_DATASETID_AND_COLUMNID, datasetId, columnId, start, end); for (Map row : rows) { Long id = (Long) row.get("id"); String author = (String) row.get("author"); String authorEmail = (String) row.get("authorEmail"); String authorUsername = (String) row.get("authorUsername"); String text = (String) row.get("text"); String created = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss") .format((Timestamp) row.get("created")); String modified = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss") .format((Timestamp) row.get("modified")); Long columnId = (Long) row.get("field_id"); boolean isDefault = (Boolean) row.get("is_default"); DatasetColumnComment datasetColumnComment = new DatasetColumnComment(); datasetColumnComment.id = id; datasetColumnComment.author = author; datasetColumnComment.authorEmail = authorEmail; datasetColumnComment.authorUsername = authorUsername; datasetColumnComment.text = text; datasetColumnComment.created = created; datasetColumnComment.modified = modified; datasetColumnComment.columnId = columnId; datasetColumnComment.isDefault = isDefault; pagedComments.add(datasetColumnComment); }//from w ww .j av a 2 s .c om try { count = getJdbcTemplate().queryForObject("SELECT FOUND_ROWS()", Long.class); } catch (EmptyResultDataAccessException e) { Logger.error("Exception = " + e.getMessage()); } if (pagedComments != null) { for (DatasetColumnComment dc : pagedComments) { if (StringUtils.isNotBlank(userName) && userName.equalsIgnoreCase(dc.authorUsername)) { dc.isAuthor = true; } } } resultNode.set("comments", Json.toJson(pagedComments)); resultNode.put("count", count); resultNode.put("page", page); resultNode.put("itemsPerPage", size); resultNode.put("totalPages", (int) Math.ceil(count / ((double) size))); return resultNode; } }); return result; }
From source file:dao.AdvSearchDAO.java
public static ObjectNode searchFlows(JsonNode searchOpt, int page, int size) { ObjectNode resultNode = Json.newObject(); int count = 0; List<String> appcodeInList = new ArrayList<String>(); List<String> appcodeNotInList = new ArrayList<String>(); List<String> flowInList = new ArrayList<String>(); List<String> flowNotInList = new ArrayList<String>(); List<String> jobInList = new ArrayList<String>(); List<String> jobNotInList = new ArrayList<String>(); if (searchOpt != null && (searchOpt.isContainerNode())) { if (searchOpt.has("appcode")) { JsonNode appcodeNode = searchOpt.get("appcode"); if (appcodeNode != null && appcodeNode.isContainerNode()) { if (appcodeNode.has("in")) { JsonNode appcodeInNode = appcodeNode.get("in"); if (appcodeInNode != null) { String appcodeInStr = appcodeInNode.asText(); if (StringUtils.isNotBlank(appcodeInStr)) { String[] appcodeInArray = appcodeInStr.split(","); if (appcodeInArray != null) { for (String value : appcodeInArray) { if (StringUtils.isNotBlank(value)) { appcodeInList.add(value.trim()); }//from w ww .j a v a 2s . c o m } } } } } if (appcodeNode.has("not")) { JsonNode appcodeNotInNode = appcodeNode.get("not"); if (appcodeNotInNode != null) { String appcodeNotInStr = appcodeNotInNode.asText(); if (StringUtils.isNotBlank(appcodeNotInStr)) { String[] appcodeNotInArray = appcodeNotInStr.split(","); if (appcodeNotInArray != null) { for (String value : appcodeNotInArray) { if (StringUtils.isNotBlank(value)) { appcodeNotInList.add(value.trim()); } } } } } } } } if (searchOpt.has("flow")) { JsonNode flowNode = searchOpt.get("flow"); if (flowNode != null && flowNode.isContainerNode()) { if (flowNode.has("in")) { JsonNode flowInNode = flowNode.get("in"); if (flowInNode != null) { String flowInStr = flowInNode.asText(); if (StringUtils.isNotBlank(flowInStr)) { String[] flowInArray = flowInStr.split(","); if (flowInArray != null) { for (String value : flowInArray) { if (StringUtils.isNotBlank(value)) { flowInList.add(value.trim()); } } } } } } if (flowNode.has("not")) { JsonNode flowNotInNode = flowNode.get("not"); if (flowNotInNode != null) { String flowNotInStr = flowNotInNode.asText(); if (StringUtils.isNotBlank(flowNotInStr)) { String[] flowNotInArray = flowNotInStr.split(","); if (flowNotInArray != null) { for (String value : flowNotInArray) { if (StringUtils.isNotBlank(value)) { flowNotInList.add(value.trim()); } } } } } } } } if (searchOpt.has("job")) { JsonNode jobNode = searchOpt.get("job"); if (jobNode != null && jobNode.isContainerNode()) { if (jobNode.has("in")) { JsonNode jobInNode = jobNode.get("in"); if (jobInNode != null) { String jobInStr = jobInNode.asText(); if (StringUtils.isNotBlank(jobInStr)) { String[] jobInArray = jobInStr.split(","); if (jobInArray != null) { for (String value : jobInArray) { if (StringUtils.isNotBlank(value)) { jobInList.add(value.trim()); } } } } } } if (jobNode.has("not")) { JsonNode jobNotInNode = jobNode.get("not"); if (jobNotInNode != null) { String jobNotInStr = jobNotInNode.asText(); if (StringUtils.isNotBlank(jobNotInStr)) { String[] jobNotInArray = jobNotInStr.split(","); if (jobNotInArray != null) { for (String value : jobNotInArray) { if (StringUtils.isNotBlank(value)) { jobNotInList.add(value.trim()); } } } } } } } } boolean needAndKeyword = false; final List<FlowJob> pagedFlows = new ArrayList<FlowJob>(); final JdbcTemplate jdbcTemplate = getJdbcTemplate(); javax.sql.DataSource ds = jdbcTemplate.getDataSource(); DataSourceTransactionManager tm = new DataSourceTransactionManager(ds); TransactionTemplate txTemplate = new TransactionTemplate(tm); ObjectNode result; String query = null; if (jobInList.size() > 0 || jobNotInList.size() > 0) { query = ADV_SEARCH_JOB; } else { query = ADV_SEARCH_FLOW; } if (appcodeInList.size() > 0 || appcodeNotInList.size() > 0) { boolean appcodeNeedAndKeyword = false; if (appcodeInList.size() > 0) { int indexForAppcodeInList = 0; for (String appcode : appcodeInList) { if (indexForAppcodeInList == 0) { query += "WHERE a.app_code in ('" + appcode + "'"; } else { query += ", '" + appcode + "'"; } indexForAppcodeInList++; } query += ") "; appcodeNeedAndKeyword = true; } if (appcodeNotInList.size() > 0) { if (appcodeNeedAndKeyword) { query += " AND "; } else { query += " WHERE "; } int indexForAppcodeNotInList = 0; for (String appcode : appcodeNotInList) { if (indexForAppcodeNotInList == 0) { query += "a.app_code not in ('" + appcode + "'"; } else { query += ", '" + appcode + "'"; } indexForAppcodeNotInList++; } query += ") "; } needAndKeyword = true; } if (flowInList.size() > 0 || flowNotInList.size() > 0) { if (needAndKeyword) { query += " AND "; } else { query += " WHERE "; } boolean flowNeedAndKeyword = false; if (flowInList.size() > 0) { query += "( "; int indexForFlowInList = 0; for (String flow : flowInList) { if (indexForFlowInList == 0) { query += "f.flow_name LIKE '%" + flow + "%'"; } else { query += " or f.flow_name LIKE '%" + flow + "%'"; } indexForFlowInList++; } query += ") "; flowNeedAndKeyword = true; } if (flowNotInList.size() > 0) { if (flowNeedAndKeyword) { query += " AND "; } query += "( "; int indexForFlowNotInList = 0; for (String flow : flowNotInList) { if (indexForFlowNotInList == 0) { query += "f.flow_name NOT LIKE '%" + flow + "%'"; } else { query += " and f.flow_name NOT LIKE '%" + flow + "%'"; } indexForFlowNotInList++; } query += ") "; } needAndKeyword = true; } if (jobInList.size() > 0 || jobNotInList.size() > 0) { if (needAndKeyword) { query += " AND "; } else { query += " WHERE "; } query += "( "; boolean jobNeedAndKeyword = false; if (jobInList.size() > 0) { query += "( "; int indexForJobInList = 0; for (String job : jobInList) { if (indexForJobInList == 0) { query += "j.job_name LIKE '%" + job + "%'"; } else { query += " or j.job_name LIKE '%" + job + "%'"; } indexForJobInList++; } query += ") "; jobNeedAndKeyword = true; } if (jobNotInList.size() > 0) { if (jobNeedAndKeyword) { query += " AND "; } query += "( "; int indexForJobNotInList = 0; for (String job : jobNotInList) { if (indexForJobNotInList == 0) { query += "j.job_name NOT LIKE '%" + job + "%'"; } else { query += " and j.job_name NOT LIKE '%" + job + "%'"; } indexForJobNotInList++; } query += ") "; } query += " ) "; } query += " LIMIT " + (page - 1) * size + ", " + size; final String queryString = query; result = txTemplate.execute(new TransactionCallback<ObjectNode>() { public ObjectNode doInTransaction(TransactionStatus status) { List<Map<String, Object>> rows = null; rows = jdbcTemplate.queryForList(queryString); for (Map row : rows) { FlowJob flow = new FlowJob(); flow.appCode = (String) row.get("app_code"); flow.flowName = (String) row.get("flow_name"); flow.flowPath = (String) row.get("flow_path"); flow.flowGroup = (String) row.get("flow_group"); flow.jobName = (String) row.get("job_name"); flow.jobPath = (String) row.get("job_path"); flow.flowId = (Long) row.get("flow_id"); if (StringUtils.isNotBlank(flow.jobName)) { flow.displayName = flow.jobName; } else { flow.displayName = flow.flowName; } flow.link = "#/flows/name/" + flow.appCode + "/" + Long.toString(flow.flowId) + "/page/1?urn=" + flow.flowGroup; flow.path = flow.appCode + "/" + flow.flowPath; pagedFlows.add(flow); } long count = 0; try { count = jdbcTemplate.queryForObject("SELECT FOUND_ROWS()", Long.class); } catch (EmptyResultDataAccessException e) { Logger.error("Exception = " + e.getMessage()); } ObjectNode resultNode = Json.newObject(); resultNode.put("count", count); resultNode.put("page", page); resultNode.put("isFlowJob", true); resultNode.put("itemsPerPage", size); resultNode.put("totalPages", (int) Math.ceil(count / ((double) size))); resultNode.set("data", Json.toJson(pagedFlows)); return resultNode; } }); return result; } resultNode.put("count", 0); resultNode.put("page", page); resultNode.put("itemsPerPage", size); resultNode.put("totalPages", 0); resultNode.set("data", Json.toJson("")); return resultNode; }
From source file:database.DataLoader.java
private void moveOrdersToDirections() throws SQLException, ClassNotFoundException, Exception { try {//from w ww . j a v a2s . 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)); } }