private void executeTransaction(final SQLTransaction<Integer> tran) { try { this.dbOperator.transaction(tran); } catch (final SQLException ex) { handleSQLException(ex); } }
public void uploadLogFile(final int execId, final String name, final int attempt, final File... files) throws ExecutorManagerException { final SQLTransaction<Integer> transaction = transOperator -> { uploadLogFile(transOperator, execId, name, attempt, files, this.defaultEncodingType); transOperator.getConnection().commit(); return 1; }; try { this.dbOperator.transaction(transaction); } catch (final SQLException e) { logger.error("uploadLogFile failed.", e); throw new ExecutorManagerException("uploadLogFile failed.", e); } }
@Override public void addProjectVersion( final int projectId, final int version, final File localFile, final String uploader, final byte[] md5, final String resourceId) throws ProjectManagerException { // when one transaction completes, it automatically commits. final SQLTransaction<Integer> transaction = transOperator -> { addProjectToProjectVersions(transOperator, projectId, version, localFile, uploader, md5, resourceId); return 1; }; try { this.dbOperator.transaction(transaction); } catch (final SQLException e) { logger.error("addProjectVersion failed.", e); throw new ProjectManagerException("addProjectVersion failed.", e); } }
final int res = this.dbOperator.transaction(cleanOlderProjectTransaction); if (res == 0) { logger.info("clean older project given project id " + projectId + " doesn't take effect.");
this.dbOperator.transaction(uploadProjectFileTransaction); } catch (final SQLException e) { logger.error("upload project files failed.", e);
public int selectAndUpdateExecution(final int executorId) throws ExecutorManagerException { final String UPDATE_EXECUTION = "UPDATE execution_flows SET executor_id = ? where exec_id = ?"; final SQLTransaction<Integer> selectAndUpdateExecution = transOperator -> { final List<Integer> execIds = transOperator.query(SelectFromExecutionFlows .SELECT_EXECUTION_FOR_UPDATE, new SelectFromExecutionFlows()); int execId = -1; if (!execIds.isEmpty()) { execId = execIds.get(0); transOperator.update(UPDATE_EXECUTION, executorId, execId); } transOperator.getConnection().commit(); return execId; }; try { return this.dbOperator.transaction(selectAndUpdateExecution); } catch (final SQLException e) { throw new ExecutorManagerException("Error selecting and updating execution with executor " + executorId, e); } }
/** * TODO: Don't understand why we need synchronized here. */ @Override public synchronized void addTrigger(final Trigger t) throws TriggerLoaderException { logger.info("Inserting trigger " + t.toString() + " into db."); final SQLTransaction<Long> insertAndGetLastID = transOperator -> { transOperator.update(ADD_TRIGGER, DateTime.now().getMillis()); // This commit must be called in order to unlock trigger table and have last insert ID. transOperator.getConnection().commit(); return transOperator.getLastInsertId(); }; try { final long id = this.dbOperator.transaction(insertAndGetLastID); t.setTriggerId((int) id); updateTrigger(t); logger.info("uploaded trigger " + t.getDescription()); } catch (final SQLException ex) { logger.error("Adding Trigger " + t.getTriggerId() + " failed."); throw new TriggerLoaderException("trigger id is not properly created.", ex); } }
@Test public void testTransaction() throws Exception { when(this.queryRunner.update(this.conn, "update blah set ? = ?", "1", 26)).thenReturn(1); when(this.queryRunner.query(this.conn, "select * from blah where ? = ?", this.handler, "id", 1)) .thenReturn(26); final SQLTransaction<Integer> transaction = transOperator -> { transOperator.update("update blah set ? = ?", "1", 26); return transOperator.query("select * from blah where ? = ?", this.handler, "id", 1); }; final int res = this.dbOperator.transaction(transaction); Assert.assertEquals(26, res); }
final int numRowsInserted = this.dbOperator.transaction(insertProject); if (numRowsInserted == 0) { throw new ProjectManagerException("No projects have been inserted.");
public synchronized void uploadExecutableFlow(final ExecutableFlow flow) throws ExecutorManagerException { final String INSERT_EXECUTABLE_FLOW = "INSERT INTO execution_flows " + "(project_id, flow_id, version, status, submit_time, submit_user, update_time) " + "values (?,?,?,?,?,?,?)"; final long submitTime = System.currentTimeMillis(); flow.setStatus(Status.PREPARING); flow.setSubmitTime(submitTime); /** * Why we need a transaction to get last insert ID? * Because "SELECT LAST_INSERT_ID()" needs to have the same connection * as inserting the new entry. * See https://dev.mysql.com/doc/refman/5.7/en/information-functions.html#function_last-insert-id */ final SQLTransaction<Long> insertAndGetLastID = transOperator -> { transOperator.update(INSERT_EXECUTABLE_FLOW, flow.getProjectId(), flow.getFlowId(), flow.getVersion(), Status.PREPARING.getNumVal(), submitTime, flow.getSubmitUser(), submitTime); transOperator.getConnection().commit(); return transOperator.getLastInsertId(); }; try { final long id = this.dbOperator.transaction(insertAndGetLastID); logger.info("Flow given " + flow.getFlowId() + " given id " + id); flow.setExecutionId((int) id); updateExecutableFlow(flow); } catch (final SQLException e) { throw new ExecutorManagerException("Error creating execution.", e); } }
public void uploadLogFile(final int execId, final String name, final int attempt, final File... files) throws ExecutorManagerException { final SQLTransaction<Integer> transaction = transOperator -> { uploadLogFile(transOperator, execId, name, attempt, files, this.defaultEncodingType); transOperator.getConnection().commit(); return 1; }; try { this.dbOperator.transaction(transaction); } catch (final SQLException e) { logger.error("uploadLogFile failed.", e); throw new ExecutorManagerException("uploadLogFile failed.", e); } }
@Override public void addProjectVersion( final int projectId, final int version, final File localFile, final String uploader, final byte[] md5, final String resourceId) throws ProjectManagerException { // when one transaction completes, it automatically commits. final SQLTransaction<Integer> transaction = transOperator -> { addProjectToProjectVersions(transOperator, projectId, version, localFile, uploader, md5, resourceId); return 1; }; try { this.dbOperator.transaction(transaction); } catch (final SQLException e) { logger.error("addProjectVersion failed.", e); throw new ProjectManagerException("addProjectVersion failed.", e); } }
this.dbOperator.transaction(uploadProjectFileTransaction); } catch (final SQLException e) { logger.error("upload project files failed.", e);
@Override public void cleanOlderProjectVersion(final int projectId, final int version) throws ProjectManagerException { final String DELETE_FLOW = "DELETE FROM project_flows WHERE project_id=? AND version<?"; final String DELETE_PROPERTIES = "DELETE FROM project_properties WHERE project_id=? AND version<?"; final String DELETE_PROJECT_FILES = "DELETE FROM project_files WHERE project_id=? AND version<?"; final String UPDATE_PROJECT_VERSIONS = "UPDATE project_versions SET num_chunks=0 WHERE project_id=? AND version<?"; // Todo jamiesjc: delete flow files final SQLTransaction<Integer> cleanOlderProjectTransaction = transOperator -> { transOperator.update(DELETE_FLOW, projectId, version); transOperator.update(DELETE_PROPERTIES, projectId, version); transOperator.update(DELETE_PROJECT_FILES, projectId, version); return transOperator.update(UPDATE_PROJECT_VERSIONS, projectId, version); }; try { final int res = this.dbOperator.transaction(cleanOlderProjectTransaction); if (res == 0) { logger.info("clean older project given project id " + projectId + " doesn't take effect."); } } catch (final SQLException e) { logger.error("clean older project transaction failed", e); throw new ProjectManagerException("clean older project transaction failed", e); } }
/** * TODO: Don't understand why we need synchronized here. */ @Override public synchronized void addTrigger(final Trigger t) throws TriggerLoaderException { logger.info("Inserting trigger " + t.toString() + " into db."); final SQLTransaction<Long> insertAndGetLastID = transOperator -> { transOperator.update(ADD_TRIGGER, DateTime.now().getMillis()); // This commit must be called in order to unlock trigger table and have last insert ID. transOperator.getConnection().commit(); return transOperator.getLastInsertId(); }; try { final long id = this.dbOperator.transaction(insertAndGetLastID); t.setTriggerId((int) id); updateTrigger(t); logger.info("uploaded trigger " + t.getDescription()); } catch (final SQLException ex) { logger.error("Adding Trigger " + t.getTriggerId() + " failed."); throw new TriggerLoaderException("trigger id is not properly created.", ex); } }
final int numRowsInserted = this.dbOperator.transaction(insertProject); if (numRowsInserted == 0) { throw new ProjectManagerException("No projects have been inserted.");
public synchronized void uploadExecutableFlow(final ExecutableFlow flow) throws ExecutorManagerException { final String INSERT_EXECUTABLE_FLOW = "INSERT INTO execution_flows " + "(project_id, flow_id, version, status, submit_time, submit_user, update_time) " + "values (?,?,?,?,?,?,?)"; final long submitTime = System.currentTimeMillis(); flow.setStatus(Status.PREPARING); /** * Why we need a transaction to get last insert ID? * Because "SELECT LAST_INSERT_ID()" needs to have the same connection * as inserting the new entry. * See https://dev.mysql.com/doc/refman/5.7/en/information-functions.html#function_last-insert-id */ final SQLTransaction<Long> insertAndGetLastID = transOperator -> { transOperator.update(INSERT_EXECUTABLE_FLOW, flow.getProjectId(), flow.getFlowId(), flow.getVersion(), Status.PREPARING.getNumVal(), submitTime, flow.getSubmitUser(), submitTime); transOperator.getConnection().commit(); return transOperator.getLastInsertId(); }; try { final long id = this.dbOperator.transaction(insertAndGetLastID); logger.info("Flow given " + flow.getFlowId() + " given id " + id); flow.setExecutionId((int) id); updateExecutableFlow(flow); } catch (final SQLException e) { throw new ExecutorManagerException("Error creating execution.", e); } }