Skip to content

Commit

Permalink
Extract static values. Fix CamelCase. Enhance loggig.
Browse files Browse the repository at this point in the history
  • Loading branch information
belugabehr committed Nov 27, 2020
1 parent 5d8eb88 commit 6a72369
Showing 1 changed file with 12 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,10 @@
public class DbNotificationListener extends TransactionalMetaStoreEventListener {

private static final Logger LOG = LoggerFactory.getLogger(DbNotificationListener.class.getName());

private static final String NL_SEL_SQL = "select \"NEXT_VAL\" from \"SEQUENCE_TABLE\" where \"SEQUENCE_NAME\" = ?";
private static final String NL_UPD_SQL = "update \"SEQUENCE_TABLE\" set \"NEXT_VAL\" = ? where \"SEQUENCE_NAME\" = ?";

private static CleanerThread cleaner = null;

private Configuration conf;
Expand Down Expand Up @@ -981,27 +985,24 @@ private static void close(ResultSet rs) {
*/
private long getNextNLId(Connection con, SQLGenerator sqlGenerator, String sequence)
throws SQLException, MetaException {
final String seq_sql = "select \"NEXT_VAL\" from \"SEQUENCE_TABLE\" where \"SEQUENCE_NAME\" = ?";
final String upd_sql = "update \"SEQUENCE_TABLE\" set \"NEXT_VAL\" = ? where \"SEQUENCE_NAME\" = ?";

final String sou_sql = sqlGenerator.addForUpdateClause(seq_sql);
final String sfuSql = sqlGenerator.addForUpdateClause(NL_SEL_SQL);
Optional<Long> nextSequenceValue = Optional.empty();

LOG.debug("Going to execute query <{}>", sou_sql);
try (PreparedStatement stmt = con.prepareStatement(sou_sql)) {
LOG.debug("Going to execute query [{}][1={}]", sfuSql, sequence);
try (PreparedStatement stmt = con.prepareStatement(sfuSql)) {
stmt.setString(1, sequence);
try (ResultSet rs = stmt.executeQuery()) {
if (rs.next()) {
nextSequenceValue = Optional.of(rs.getLong(1));
}
ResultSet rs = stmt.executeQuery();
if (rs.next()) {
nextSequenceValue = Optional.of(rs.getLong(1));
}
}

final long updatedNLId = 1L + nextSequenceValue.orElseThrow(
() -> new MetaException("Transaction database not properly configured, failed to determine next NL ID"));

LOG.debug("Going to execute query <{}>", upd_sql);
try (PreparedStatement stmt = con.prepareStatement(upd_sql)) {
LOG.debug("Going to execute query [{}][1={}][2={}]", NL_UPD_SQL, updatedNLId, sequence);
try (PreparedStatement stmt = con.prepareStatement(NL_UPD_SQL)) {
stmt.setLong(1, updatedNLId);
stmt.setString(2, sequence);
final int rowCount = stmt.executeUpdate();
Expand Down

0 comments on commit 6a72369

Please sign in to comment.