public boolean set(int sequence, String message) throws IOException { Connection connection = null; PreparedStatement insert = null; ResultSet rs = null; try { connection = dataSource.getConnection(); insert = connection.prepareStatement(SQL_INSERT_MESSAGE); int offset = setSessionIdParameters(insert, 1); insert.setInt(offset++, sequence); insert.setString(offset, message); insert.execute(); } catch (SQLException ex) { if (connection != null) { PreparedStatement update = null; try { update = connection.prepareStatement(SQL_UPDATE_MESSAGE); update.setString(1, message); int offset = setSessionIdParameters(update, 2); update.setInt(offset, sequence); boolean status = update.execute(); return !status ? update.getUpdateCount() > 0 : false; } catch (SQLException e) { throw (IOException) new IOException(e.getMessage()).initCause(e); } finally { JdbcUtil.close(sessionID, update); } } } finally { JdbcUtil.close(sessionID, rs); JdbcUtil.close(sessionID, insert); JdbcUtil.close(sessionID, connection); } return true; }
public void reset() throws IOException { cache.reset(); Connection connection = null; PreparedStatement deleteMessages = null; PreparedStatement updateTime = null; try { connection = dataSource.getConnection(); deleteMessages = connection.prepareStatement(SQL_DELETE_MESSAGES); setSessionIdParameters(deleteMessages, 1); deleteMessages.execute(); updateTime = connection.prepareStatement(SQL_UPDATE_SESSION); updateTime.setTimestamp( 1, new Timestamp(Calendar.getInstance(TimeZone.getTimeZone("UTC")).getTimeInMillis())); updateTime.setInt(2, getNextTargetMsgSeqNum()); updateTime.setInt(3, getNextSenderMsgSeqNum()); setSessionIdParameters(updateTime, 4); updateTime.execute(); } catch (SQLException e) { throw (IOException) new IOException(e.getMessage()).initCause(e); } catch (IOException e) { throw e; } finally { JdbcUtil.close(sessionID, deleteMessages); JdbcUtil.close(sessionID, updateTime); JdbcUtil.close(sessionID, connection); } }
private void loadCache() throws SQLException, IOException { Connection connection = null; PreparedStatement query = null; PreparedStatement insert = null; ResultSet rs = null; try { connection = dataSource.getConnection(); query = connection.prepareStatement(SQL_GET_SEQNUMS); setSessionIdParameters(query, 1); rs = query.executeQuery(); if (rs.next()) { cache.setCreationTime(SystemTime.getUtcCalendar(rs.getTimestamp(1))); cache.setNextTargetMsgSeqNum(rs.getInt(2)); cache.setNextSenderMsgSeqNum(rs.getInt(3)); } else { insert = connection.prepareStatement(SQL_INSERT_SESSION); int offset = setSessionIdParameters(insert, 1); insert.setTimestamp(offset++, new Timestamp(cache.getCreationTime().getTime())); insert.setInt(offset++, cache.getNextTargetMsgSeqNum()); insert.setInt(offset, cache.getNextSenderMsgSeqNum()); insert.execute(); } } finally { JdbcUtil.close(sessionID, rs); JdbcUtil.close(sessionID, query); JdbcUtil.close(sessionID, insert); JdbcUtil.close(sessionID, connection); } }
private void storeSequenceNumbers() throws IOException { Connection connection = null; PreparedStatement update = null; try { connection = dataSource.getConnection(); update = connection.prepareStatement(SQL_UPDATE_SEQNUMS); update.setInt(1, cache.getNextTargetMsgSeqNum()); update.setInt(2, cache.getNextSenderMsgSeqNum()); setSessionIdParameters(update, 3); update.execute(); } catch (SQLException e) { throw (IOException) new IOException(e.getMessage()).initCause(e); } finally { JdbcUtil.close(sessionID, update); JdbcUtil.close(sessionID, connection); } }
public void get(int startSequence, int endSequence, Collection<String> messages) throws IOException { Connection connection = null; PreparedStatement query = null; ResultSet rs = null; try { connection = dataSource.getConnection(); query = connection.prepareStatement(SQL_GET_MESSAGES); int offset = setSessionIdParameters(query, 1); query.setInt(offset++, startSequence); query.setInt(offset, endSequence); rs = query.executeQuery(); while (rs.next()) { String message = rs.getString(1); messages.add(message); } } catch (SQLException e) { throw (IOException) new IOException(e.getMessage()).initCause(e); } finally { JdbcUtil.close(sessionID, rs); JdbcUtil.close(sessionID, query); JdbcUtil.close(sessionID, connection); } }