/** * Close all 3 JDBC artifacts in order: {@code rs stmt dbConn} */ static void close(ResultSet rs, Statement stmt, Connection dbConn) { close(rs); closeStmt(stmt); closeDbConn(dbConn); }
private LockInfo getTxnIdFromLockId(Connection dbConn, long extLockId) throws NoSuchLockException, MetaException, SQLException { Statement stmt = null; ResultSet rs = null; try { stmt = dbConn.createStatement(); String s = "select hl_lock_ext_id, hl_lock_int_id, hl_db, hl_table, " + "hl_partition, hl_lock_state, hl_lock_type, hl_txnid from HIVE_LOCKS where " + "hl_lock_ext_id = " + extLockId; LOG.debug("Going to execute query <" + s + ">"); rs = stmt.executeQuery(s); if (!rs.next()) { return null; } LockInfo info = new LockInfo(rs); LOG.debug("getTxnIdFromLockId(" + extLockId + ") Return " + JavaUtils.txnIdToString(info.txnId)); return info; } finally { close(rs); closeStmt(stmt); } }
/** * For testing only, do not use. */ @VisibleForTesting public int numLocksInLockTable() throws SQLException, MetaException { Connection dbConn = null; Statement stmt = null; ResultSet rs = null; try { dbConn = getDbConn(Connection.TRANSACTION_READ_COMMITTED); stmt = dbConn.createStatement(); String s = "select count(*) from HIVE_LOCKS"; LOG.debug("Going to execute query <" + s + ">"); rs = stmt.executeQuery(s); rs.next(); int rc = rs.getInt(1); // Necessary to clean up the transaction in the db. dbConn.rollback(); return rc; } finally { close(rs, stmt, dbConn); } }
} finally { closeStmt(pst); close(rs);
@Override public void releaseLocks() { rollbackDBConn(dbConn); close(rs, stmt, dbConn); if(derbySemaphore != null) { derbySemaphore.release(); } for(String key : keys) { LOG.debug(quoteString(key) + " unlocked by " + quoteString(TxnHandler.hostname)); } } }
LOG.error("Failed to purge timedout locks due to: " + ex.getMessage(), ex); } finally { close(rs); closeStmt(stmt);
/** * Note that by definition select for update is divorced from update, i.e. you executeQuery() to read * and then executeUpdate(). One other alternative would be to actually update the row in TXNS but * to the same value as before thus forcing db to acquire write lock for duration of the transaction. * * There is no real reason to return the ResultSet here other than to make sure the reference to it * is retained for duration of intended lock scope and is not GC'd thus (unlikely) causing lock * to be released. * @param txnState the state this txn is expected to be in. may be null * @return null if no row was found * @throws SQLException * @throws MetaException */ private ResultSet lockTransactionRecord(Statement stmt, long txnId, Character txnState) throws SQLException, MetaException { String query = "select TXN_STATE from TXNS where TXN_ID = " + txnId + (txnState != null ? " AND TXN_STATE=" + quoteChar(txnState) : ""); ResultSet rs = stmt.executeQuery(sqlGenerator.addForUpdateClause(query)); if(rs.next()) { return rs; } close(rs); return null; }
StringUtils.stringifyException(ex)); } finally { close(rs, pst, dbConn); if(handle != null) { handle.releaseLocks();
close(lockHandle, stmt, dbConn); unlockInternal();
close(rs); throw new NoSuchLockException(sb.toString());
checkRetryable(dbConn, e, "countOpenTxns()"); } finally { close(rs, stmt, dbConn);
private enum LockAction {ACQUIRE, WAIT, KEEP_LOOKING}
private ValidTxnList getValidTxnList(Connection dbConn, String fullTableName, Long writeId) throws MetaException, SQLException { PreparedStatement pst = null; ResultSet rs = null; try { String[] names = TxnUtils.getDbTableName(fullTableName); assert names.length == 2; List<String> params = Arrays.asList(names[0], names[1]); String s = "select t2w_txnid from TXN_TO_WRITE_ID where t2w_database = ? and t2w_table = ? and t2w_writeid = " + writeId; pst = sqlGenerator.prepareStmtWithParameters(dbConn, s, params); LOG.debug("Going to execute query <" + s.replaceAll("\\?", "{}") + ">", quoteString(names[0]), quoteString(names[1])); rs = pst.executeQuery(); if (rs.next()) { return TxnCommonUtils.createValidReadTxnList(getOpenTxns(), rs.getLong(1)); } throw new MetaException("invalid write id " + writeId + " for table " + fullTableName); } finally { close(rs, pst, null); } }
StringUtils.stringifyException(e)); } finally { close(rs, stmt, dbConn); unlockInternal();
+ StringUtils.stringifyException(e)); } finally { close(null, pst, dbConn);
StringUtils.stringifyException(e)); } finally { close(null, pst, dbConn); unlockInternal();
StringUtils.stringifyException(ex)); } finally { close(rs, pst, dbConn);
+ StringUtils.stringifyException(e)); } finally { close(null, pst, dbConn); if(handle != null) { handle.releaseLocks();
+ StringUtils.stringifyException(e)); } finally { close(null, stmt, dbConn); unlockInternal();