@Override public void releaseClaim(String processorName, int segment) { Connection connection = getConnection(); try { int[] result = executeUpdates(connection, e -> { throw new JdbcException( format("Could not load token for processor [%s] and segment " + "[%d]", processorName, segment), e); }, c -> releaseClaim(c, processorName, segment)); try { if (!connection.isClosed() && !connection.getAutoCommit()) { connection.commit(); } } catch (SQLException e) { // ignore } if (result[0] < 1) { logger.warn( "Releasing claim of token {}/{} failed. It was owned by another node.", processorName, segment ); } } finally { closeQuietly(connection); } }
@Override public int[] fetchSegments(String processorName) { Connection connection = getConnection(); try { List<Integer> integers = executeQuery(connection, c -> selectForSegments(c, processorName), listResults(rs -> rs.getInt(schema.segmentColumn())), e -> new JdbcException(format( "Could not load segments for processor [%s]", processorName ), e) ); return integers.stream().mapToInt(i -> i).toArray(); } finally { closeQuietly(connection); } }
@Override public void storeToken(TrackingToken token, String processorName, int segment) throws UnableToClaimTokenException { Connection connection = getConnection(); try { executeQuery(connection, c -> selectForUpdate(c, processorName, segment), resultSet -> { insertOrUpdateToken(resultSet, token, processorName, segment); if (!connection.getAutoCommit()) { connection.commit(); } return null; }, e -> new JdbcException(format("Could not store token [%s] for processor [%s] and segment [%d]", token, processorName, segment), e)); } finally { closeQuietly(connection); } }
@Override public TrackingToken fetchToken(String processorName, int segment) throws UnableToClaimTokenException { Connection connection = getConnection(); try { return executeQuery(connection, c -> selectForUpdate(c, processorName, segment), resultSet -> { TrackingToken result = loadOrInsertToken(resultSet, processorName, segment); if (!connection.getAutoCommit()) { connection.commit(); } return result; }, e -> new JdbcException( format("Could not load token for processor [%s] and segment [%d]", processorName, segment), e)); } finally { closeQuietly(connection); } }
@Override public void initializeTokenSegments(String processorName, int segmentCount, TrackingToken initialToken) throws UnableToClaimTokenException { Connection connection = getConnection(); try { executeQuery(connection, c -> selectForUpdate(c, processorName, 0), resultSet -> { for (int segment = 0; segment < segmentCount; segment++) { insertTokenEntry(resultSet, initialToken, processorName, segment); } if (!connection.getAutoCommit()) { connection.commit(); } return null; }, e -> new UnableToClaimTokenException( "Could not initialize segments. Some segments were already present.", e )); } finally { closeQuietly(connection); } }
/** * Performs the DDL queries to create the schema necessary for this token store implementation. * * @param schemaFactory factory of the token entry schema */ public void createSchema(TokenTableFactory schemaFactory) { Connection c = getConnection(); try { executeUpdates(c, e -> { throw new JdbcException("Failed to create token tables", e); }, connection -> schemaFactory.createTable(connection, schema)); } finally { closeQuietly(c); } }
@Override public int[] fetchSegments(String processorName) { Connection connection = getConnection(); try { List<Integer> integers = executeQuery(connection, c -> selectForSegments(c, processorName), listResults(rs -> rs.getInt(schema.segmentColumn())), e -> new JdbcException( format("Could not load segments for processor [%s]", processorName), e)); return integers.stream().mapToInt(i -> i).toArray(); } finally { closeQuietly(connection); } }
@Override public void releaseClaim(String processorName, int segment) { Connection connection = getConnection(); try { int[] result = executeUpdates(connection, e -> { throw new JdbcException( format("Could not load token for processor [%s] and segment " + "[%d]", processorName, segment), e); }, c -> releaseClaim(c, processorName, segment)); try { if (!connection.isClosed() && !connection.getAutoCommit()) { connection.commit(); } } catch (SQLException e) { // ignore } if (result[0] < 1) { logger.warn( "Releasing claim of token {}/{} failed. It was owned by another node.", processorName, segment ); } } finally { closeQuietly(connection); } }
@Override public int[] fetchSegments(String processorName) { Connection connection = getConnection(); try { List<Integer> integers = executeQuery(connection, c -> selectForSegments(c, processorName), listResults(rs -> rs.getInt(schema.segmentColumn())), e -> new JdbcException(format( "Could not load segments for processor [%s]", processorName ), e) ); return integers.stream().mapToInt(i -> i).toArray(); } finally { closeQuietly(connection); } }
@Override public TrackingToken fetchToken(String processorName, int segment) throws UnableToClaimTokenException { Connection connection = getConnection(); try { return executeQuery(connection, c -> selectForUpdate(c, processorName, segment), resultSet -> { TrackingToken result = loadOrInsertToken(resultSet, processorName, segment); if (!connection.getAutoCommit()) { connection.commit(); } return result; }, e -> new JdbcException( format("Could not load token for processor [%s] and segment [%d]", processorName, segment), e)); } finally { closeQuietly(connection); } }
@Override public void releaseClaim(String processorName, int segment) { Connection connection = getConnection(); try { int[] result = executeUpdates(connection, e -> { throw new JdbcException( format("Could not load token for processor [%s] and segment " + "[%d]", processorName, segment), e); }, c -> releaseClaim(c, processorName, segment)); try { if (!connection.isClosed() && !connection.getAutoCommit()) { connection.commit(); } } catch (SQLException e) { // ignore } if (result[0] < 1) { logger.warn("Releasing claim of token {}/{} failed. It was owned by another node.", processorName, segment); } } finally { closeQuietly(connection); } }
@Override public TrackingToken fetchToken(String processorName, int segment) throws UnableToClaimTokenException { Connection connection = getConnection(); try { return executeQuery(connection, c -> selectForUpdate(c, processorName, segment), resultSet -> { TrackingToken result = loadOrInsertToken(resultSet, processorName, segment); if (!connection.getAutoCommit()) { connection.commit(); } return result; }, e -> new JdbcException( format("Could not load token for processor [%s] and segment [%d]", processorName, segment), e)); } finally { closeQuietly(connection); } }
@Override public void storeToken(TrackingToken token, String processorName, int segment) throws UnableToClaimTokenException { Connection connection = getConnection(); try { executeQuery(connection, c -> selectForUpdate(c, processorName, segment), resultSet -> { insertOrUpdateToken(resultSet, token, processorName, segment); if (!connection.getAutoCommit()) { connection.commit(); } return null; }, e -> new JdbcException(format("Could not store token [%s] for processor [%s] and segment [%d]", token, processorName, segment), e)); } finally { closeQuietly(connection); } }
@Override public void storeToken(TrackingToken token, String processorName, int segment) throws UnableToClaimTokenException { Connection connection = getConnection(); try { executeQuery(connection, c -> selectForUpdate(c, processorName, segment), resultSet -> { insertOrUpdateToken(resultSet, token, processorName, segment); if (!connection.getAutoCommit()) { connection.commit(); } return null; }, e -> new JdbcException(format("Could not store token [%s] for processor [%s] and segment [%d]", token, processorName, segment), e)); } finally { closeQuietly(connection); } }
@Override public void initializeTokenSegments(String processorName, int segmentCount, TrackingToken initialToken) throws UnableToClaimTokenException { Connection connection = getConnection(); try { executeQuery(connection, c -> selectForUpdate(c, processorName, 0), resultSet -> { for (int segment = 0; segment < segmentCount; segment++) { insertTokenEntry(resultSet, initialToken, processorName, segment); } if (!connection.getAutoCommit()) { connection.commit(); } return null; }, e -> new UnableToClaimTokenException("Could not initialize segments. Some segments were already present.", e)); } finally { closeQuietly(connection); } }
@Override public void initializeTokenSegments(String processorName, int segmentCount, TrackingToken initialToken) throws UnableToClaimTokenException { Connection connection = getConnection(); try { executeQuery(connection, c -> selectForUpdate(c, processorName, 0), resultSet -> { for (int segment = 0; segment < segmentCount; segment++) { insertTokenEntry(resultSet, initialToken, processorName, segment); } if (!connection.getAutoCommit()) { connection.commit(); } return null; }, e -> new UnableToClaimTokenException( "Could not initialize segments. Some segments were already present.", e )); } finally { closeQuietly(connection); } }
/** * Performs the DDL queries to create the schema necessary for this token store implementation. * * @param schemaFactory factory of the token entry schema * @throws EventStoreException when an error occurs executing SQL statements */ public void createSchema(TokenTableFactory schemaFactory) { Connection c = getConnection(); try { executeUpdates(c, e -> { throw new JdbcException("Failed to create token tables", e); }, connection -> schemaFactory.createTable(connection, schema)); } finally { closeQuietly(c); } }
/** * Performs the DDL queries to create the schema necessary for this token store implementation. * * @param schemaFactory factory of the token entry schema */ public void createSchema(TokenTableFactory schemaFactory) { Connection c = getConnection(); try { executeUpdates(c, e -> { throw new JdbcException("Failed to create token tables", e); }, connection -> schemaFactory.createTable(connection, schema)); } finally { closeQuietly(c); } }