@Override public String resetConnection(String name) { ConnectionConcierge concierge = _conns.get(name); if (concierge == null) { return "Not Found"; } Connection conn = TransactionLegacy.getStandaloneConnection(); if (conn == null) { return "Unable to get anotehr db connection"; } concierge.reset(conn); return "Done"; }
private Merovingian2(long msId) { super(MerovingianMBean.class, false); _msId = msId; Connection conn = null; try { conn = TransactionLegacy.getStandaloneConnectionWithException(); conn.setTransactionIsolation(Connection.TRANSACTION_READ_COMMITTED); conn.setAutoCommit(true); _concierge = new ConnectionConcierge("LockMaster", conn, true); } catch (SQLException e) { s_logger.error("Unable to get a new db connection", e); throw new CloudRuntimeException("Unable to initialize a connection to the database for locking purposes", e); } finally { if (_concierge == null && conn != null) { try { conn.close(); } catch (SQLException e) { s_logger.debug("closing connection failed after everything else.", e); } } } }
@Override public boolean releaseLockAsLastResortAndIReallyKnowWhatIAmDoing(String key) { s_logger.info("Releasing a lock from JMX lck-" + key); try (PreparedStatement pstmt = _concierge.conn().prepareStatement(RELEASE_LOCK_SQL);) { pstmt.setString(1, key); int rows = pstmt.executeUpdate(); return rows > 0; } catch (Exception e) { s_logger.error("releaseLockAsLastResortAndIReallyKnowWhatIAmDoing : Exception: " + e.getMessage()); return false; } }
@Override protected void runInContext() { s_logger.trace("connection concierge keep alive task"); for (Map.Entry<String, ConnectionConcierge> entry : _conns.entrySet()) { ConnectionConcierge concierge = entry.getValue(); if (concierge.keepAlive()) { testValidity(entry.getKey(), entry.getValue().conn()); } } } }, 0, seconds, TimeUnit.SECONDS);
private Connection getHeartbeatConnection() throws SQLException { if (_heartbeatConnection == null) { final Connection conn = TransactionLegacy.getStandaloneConnectionWithException(); _heartbeatConnection = new ConnectionConcierge("ClusterManagerHeartbeat", conn, false); } return _heartbeatConnection.conn(); }
@Override protected void finalize() throws Exception { if (_conn != null) { release(); } }
@Override protected void runInContext() { s_logger.trace("connection concierge keep alive task"); for (final Map.Entry<String, ConnectionConcierge> entry : _conns.entrySet()) { final ConnectionConcierge concierge = entry.getValue(); if (concierge.keepAlive()) { testValidity(entry.getKey(), entry.getValue().conn()); } } } }, 0, seconds, TimeUnit.SECONDS);
public void reset(Connection conn) { try { release(); } catch (Throwable th) { s_logger.error("Unable to release a connection", th); } _conn = conn; try { _conn.setAutoCommit(_autoCommit); _conn.setHoldability(_holdability); _conn.setTransactionIsolation(_isolationLevel); } catch (SQLException e) { s_logger.error("Unable to release a connection", e); } s_mgr.register(_name, this); s_logger.debug("Registering a database connection for " + _name); }
@Override public List<String> testValidityOfConnections() { ArrayList<String> results = new ArrayList<String>(_conns.size()); for (Map.Entry<String, ConnectionConcierge> entry : _conns.entrySet()) { String result = testValidity(entry.getKey(), entry.getValue().conn()); results.add(entry.getKey() + "=" + (result == null ? "OK" : result)); } return results; }
public ConnectionConcierge(String name, Connection conn, boolean keepAlive) { _name = name + s_mgr.getNextId(); _keepAlive = keepAlive; try { _autoCommit = conn.getAutoCommit(); _isolationLevel = conn.getTransactionIsolation(); _holdability = conn.getHoldability(); } catch (SQLException e) { throw new CloudRuntimeException("Unable to get information from the connection object", e); } reset(conn); }
private Merovingian2(final long msId) { super(MerovingianMBean.class, false); _msId = msId; Connection conn = null; try { conn = TransactionLegacy.getStandaloneConnectionWithException(); conn.setTransactionIsolation(Connection.TRANSACTION_READ_COMMITTED); conn.setAutoCommit(true); _concierge = new ConnectionConcierge("LockMaster", conn, true); } catch (final SQLException e) { s_logger.error("Unable to get a new db connection", e); throw new CloudRuntimeException("Unable to initialize a connection to the database for locking purposes", e); } finally { if (_concierge == null && conn != null) { try { conn.close(); } catch (final SQLException e) { s_logger.debug("closing connection failed after everything else.", e); } } } }
@Override protected void finalize() throws Exception { if (_conn != null) { release(); } }
@Override public void cleanupForServer(long msId) { s_logger.info("Cleaning up locks for " + msId); try { synchronized (_concierge.conn()) { try(PreparedStatement pstmt = _concierge.conn().prepareStatement(CLEANUP_MGMT_LOCKS_SQL);) { pstmt.setLong(1, msId); int rows = pstmt.executeUpdate(); s_logger.info("Released " + rows + " locks for " + msId); }catch (Exception e) { s_logger.error("cleanupForServer:Exception:"+e.getMessage()); throw new CloudRuntimeException("cleanupForServer:Exception:"+e.getMessage(), e); } } } catch (Exception e) { s_logger.error("cleanupForServer:Exception:"+e.getMessage()); throw new CloudRuntimeException("cleanupForServer:Exception:"+e.getMessage(), e); } }
private void invalidHeartbeatConnection() { if (_heartbeatConnection != null) { final Connection conn = TransactionLegacy.getStandaloneConnection(); if (conn != null) { _heartbeatConnection.reset(conn); } else { s_logger.error("DB communication problem detected, fence it"); queueNotification(new ClusterManagerMessage(ClusterManagerMessage.MessageType.nodeIsolated)); } // The stand-alone connection does not have to be closed here because there will be another reference to it. // As a matter of fact, it will be assigned to the connection instance variable in the ConnectionConcierge class. } }
public void reset(final Connection conn) { release(); _conn = conn; try { _conn.setAutoCommit(_autoCommit); _conn.setHoldability(_holdability); _conn.setTransactionIsolation(_isolationLevel); } catch (final SQLException e) { s_logger.error("Unable to release a connection", e); } s_mgr.register(_name, this); s_logger.debug("Registering a database connection for " + _name); }
public List<Map<String, String>> getLocksAcquiredBy(long msId, String threadName) { try (PreparedStatement pstmt = _concierge.conn().prepareStatement(SELECT_THREAD_LOCKS_SQL);){ pstmt.setLong(1, msId); pstmt.setString(2, threadName); try (ResultSet rs =pstmt.executeQuery();) { return toLocks(rs); } catch (Exception e) { s_logger.error("getLocksAcquiredBy:Exception:"+e.getMessage()); throw new CloudRuntimeException("Can't get locks " + pstmt, e); } } catch (Exception e) { s_logger.error("getLocksAcquiredBy:Exception:"+e.getMessage()); throw new CloudRuntimeException("getLocksAcquiredBy:Exception:"+e.getMessage(), e); } }
@Override public String resetConnection(final String name) { final ConnectionConcierge concierge = _conns.get(name); if (concierge == null) { return "Not Found"; } final Connection conn = TransactionLegacy.getStandaloneConnection(); if (conn == null) { return "Unable to get anotehr db connection"; } concierge.reset(conn); return "Done"; }
public void cleanupThread() { Count count = s_tls.get(); if (count == null || count.count == 0) { return; } int c = count.count; count.count = 0; Thread th = Thread.currentThread(); String threadName = th.getName(); int threadId = System.identityHashCode(th); try (PreparedStatement pstmt = _concierge.conn().prepareStatement(CLEANUP_THREAD_LOCKS_SQL);) { pstmt.setLong(1, _msId); pstmt.setString(2, threadName); pstmt.setInt(3, threadId); int rows = pstmt.executeUpdate(); assert (false) : "Abandon hope, all ye who enter here....There were still " + rows + ":" + c + " locks not released when the transaction ended, check for lock not released or @DB is not added to the code that using the locks!"; } catch (Exception e) { s_logger.error("cleanupThread:Exception:" + e.getMessage()); throw new CloudRuntimeException("cleanupThread:Exception:" + e.getMessage(), e); } }
public ConnectionConcierge(final String name, final Connection conn, final boolean keepAlive) { _name = name + s_mgr.getNextId(); _keepAlive = keepAlive; try { _autoCommit = conn.getAutoCommit(); _isolationLevel = conn.getTransactionIsolation(); _holdability = conn.getHoldability(); } catch (final SQLException e) { throw new CloudRuntimeException("Unable to get information from the connection object", e); } reset(conn); }
protected boolean increment(String key, String threadName, int threadId) { try (PreparedStatement pstmt = _concierge.conn().prepareStatement(INCREMENT_SQL);){ pstmt.setString(1, key); pstmt.setLong(2, _msId); pstmt.setString(3, threadName); pstmt.setInt(4, threadId); int rows = pstmt.executeUpdate(); assert (rows <= 1) : "hmm...non unique key? " + pstmt; if (s_logger.isTraceEnabled()) { s_logger.trace("lck-" + key + (rows == 1 ? " acquired again" : " failed to acquire again")); } if (rows == 1) { incrCount(); return true; } return false; } catch (Exception e) { s_logger.error("increment:Exception:"+e.getMessage()); throw new CloudRuntimeException("increment:Exception:"+e.getMessage(), e); } }