/** * */ public synchronized void stopReads() { U.close(readCh, log); }
/** {@inheritDoc} */ @Override protected void cleanup() { synchronized (this) { U.close(sock); sock = null; } } }
/** * This method accomplishes RDBMS-independent table exists check. * * @param conn Active database connection. * @return {@code true} if specified table exists, {@code false} otherwise. */ private boolean isCheckpointTableExists(Connection conn) { Statement st = null; ResultSet rs = null; try { st = conn.createStatement(); rs = st.executeQuery(chkTblExistsSql); return true; // if table does exist, no rows will ever be returned } catch (SQLException ignored) { return false; // if table does not exist, an exception will be thrown } finally { U.close(rs, log); U.close(st, log); } }
/** * Checks specified checkpoint existing. * * @param conn Active jdbc connection. * @param key Checkpoint key. * @return {@code true} if specified checkpoint exists in the checkpoint table. * @throws SQLException Thrown in case of any errors. */ private boolean isCheckpointExists(Connection conn, String key) throws SQLException { PreparedStatement st = null; ResultSet rs = null; try { st = conn.prepareStatement(chkExistsSql); st.setString(1, key); rs = st.executeQuery(); return rs.next(); } finally { U.close(rs, log); U.close(st, log); } }
/** * Handles SQL exception. */ private void onSqlException() { Connection conn = connCache.get().connection(); connCache.set(null); if (conn != null) { conns.remove(conn); // Reset connection to receive new one at next call. U.close(conn, log); } }
/** {@inheritDoc} */ @Override public void interrupt() { super.interrupt(); synchronized (this) { U.close(sock); sock = null; } }
/** {@inheritDoc} */ @Override public void interrupt() { super.interrupt(); U.close(srvrSock, log); } }
/** * Closes and releases output stream. * * @param out Object output stream. */ static void closeOut(GridOptimizedObjectOutputStream out) { U.close(out, null); StreamHolder holder = holders.get(); holder.releaseOut(); if (pool != null) { holders.remove(); boolean b = pool.offer(holder); assert b; } }
/** * Creates checkpoint. * * @param conn Active database connection. * @param key Checkpoint key. * @param state Checkpoint data. * @param expTime Checkpoint expire time. * @return Number of rows affected by query. * @throws SQLException Thrown in case of any errors. */ private int createCheckpoint(Connection conn, String key, byte[] state, Time expTime) throws SQLException { PreparedStatement st = null; try { st = conn.prepareStatement(insSql); st.setString(1, key); st.setBytes(2, state); st.setTime(3, expTime); return st.executeUpdate(); } finally { U.close(st, log); } }
/** * Updates checkpoint data. * * @param conn Active database connection. * @param key Checkpoint key. * @param state Checkpoint data. * @param expTime Checkpoint expire time. * @return Number of rows affected by query. * @throws SQLException Thrown in case of any errors. */ private int updateCheckpoint(Connection conn, String key, byte[] state, Time expTime) throws SQLException { PreparedStatement st = null; try { st = conn.prepareStatement(updateSql); st.setBytes(1, state); st.setTime(2, expTime); st.setString(3, key); return st.executeUpdate(); } finally { U.close(st, log); } }
/** * Closes and releases input stream. * * @param in Object input stream. */ @SuppressWarnings("TypeMayBeWeakened") static void closeIn(GridOptimizedObjectInputStream in) { U.close(in, null); StreamHolder holder = holders.get(); holder.releaseIn(); if (pool != null) { holders.remove(); boolean b = pool.offer(holder); assert b; } }
/** {@inheritDoc} */ @Override public byte[] loadCheckpoint(String key) throws GridSpiException { Connection conn = null; PreparedStatement st = null; ResultSet rs = null; try { conn = getConnection(); st = conn.prepareStatement(selSql); st.setString(1, key); st.setTime(2, new Time(U.currentTimeMillis())); rs = st.executeQuery(); return rs.next() ? rs.getBytes(1) : null; } catch (SQLException e) { throw new GridSpiException("Failed to load checkpoint [tblName=" + tblName + ", key=" + key + ']', e); } finally { U.close(rs, log); U.close(st, log); U.close(conn, log); } }
/** {@inheritDoc} */ @Override public <T> T unmarshal(byte[] arr, @Nullable ClassLoader clsLdr) throws GridException { GridByteArrayInputStream in = null; try { in = new GridByteArrayInputStream(arr, 0, arr.length); return unmarshal(in, clsLdr); } finally { U.close(in, null); } } }
/** * @throws GridException If failed. */ public synchronized void stopWrites() throws GridException { if (!isWritable()) return; try { writeCh.force(false); } catch (IOException e) { U.warn(log, "Failed to fsync channel: " + id, e); } U.close(writeCh, log); }
/** * Creates checkpoint table. * * @param conn Active database connection. * @throws SQLException Thrown in case of any errors. */ private void createCheckpointTable(Connection conn) throws SQLException { Statement st = null; try { st = conn.createStatement(); st.executeUpdate(crtTblSql); if (log.isDebugEnabled()) log.debug("Successfully created checkpoint table: " + tblName); } finally { U.close(st, log); } }
/** {@inheritDoc} */ @Override public byte[] marshal(@Nullable Object obj) throws GridException { GridByteArrayOutputStream out = null; try { out = new GridByteArrayOutputStream(DFLT_BUFFER_SIZE); marshal(obj, out); return out.toByteArray(); } finally { U.close(out, null); } }
/** * Reads all checkpoint data from given file. File is read as binary * data which is than deserialized. * * @param file File which contains checkpoint data. * @param m Grid marshaller. * @param log Messages logger. * @return Checkpoint data object read from given file. * @throws GridException Thrown if data could not be converted * to {@link GridSharedFsCheckpointData} object. * @throws IOException Thrown if file read error occurred. */ static GridSharedFsCheckpointData read(File file, GridMarshaller m, GridLogger log) throws IOException, GridException { assert file != null; assert m != null; assert log != null; InputStream in = new FileInputStream(file); try { return (GridSharedFsCheckpointData)m.unmarshal(in, U.gridClassLoader()); } finally { U.close(in, log); } }
/** * @param sql SQL statement. * @throws GridSpiException If failed. */ private void executeStatement(String sql) throws GridSpiException { Statement stmt = null; try { Connection c = connectionForThread(null); stmt = c.createStatement(); stmt.executeUpdate(sql); } catch (SQLException e) { onSqlException(); throw new GridSpiException("Failed to execute statement: " + sql, e); } finally { U.close(stmt, log); } }
/** {@inheritDoc} */ @Override protected void onContextDestroyed0() { if (dataSrc != null) { Connection conn = null; try { conn = getConnection(); removeExpiredCheckpoints(conn); conn.commit(); } catch (SQLException e) { U.rollbackConnection(conn, log); U.error(log, "Failed to remove expired checkpoints from: " + tblName, e); //throw new GridSpiException("Failed to remove expired checkpoints from: " + tblName, e); } finally { U.close(conn, log); } } }