Ejemplo n.º 1
0
 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;
 }
Ejemplo n.º 2
0
  public JdbcStore(SessionSettings settings, SessionID sessionID, DataSource ds) throws Exception {
    this.sessionID = sessionID;
    if (settings.isSetting(sessionID, SETTING_JDBC_STORE_SESSIONS_TABLE_NAME)) {
      sessionTableName = settings.getString(sessionID, SETTING_JDBC_STORE_SESSIONS_TABLE_NAME);
    } else {
      sessionTableName = DEFAULT_SESSION_TABLE_NAME;
    }

    if (settings.isSetting(sessionID, SETTING_JDBC_STORE_MESSAGES_TABLE_NAME)) {
      messageTableName = settings.getString(sessionID, SETTING_JDBC_STORE_MESSAGES_TABLE_NAME);
    } else {
      messageTableName = DEFAULT_MESSAGE_TABLE_NAME;
    }

    if (settings.isSetting(sessionID, SETTING_JDBC_SESSION_ID_DEFAULT_PROPERTY_VALUE)) {
      defaultSessionIdPropertyValue =
          settings.getString(sessionID, SETTING_JDBC_SESSION_ID_DEFAULT_PROPERTY_VALUE);
    } else {
      defaultSessionIdPropertyValue = SessionID.NOT_SET;
    }

    dataSource = ds == null ? JdbcUtil.getDataSource(settings, sessionID) : ds;

    // One table is sampled for the extended session ID columns. Be sure
    // that all tables are extended if you extend any of them.
    extendedSessionIdSupported = JdbcUtil.determineSessionIdSupport(dataSource, sessionTableName);

    setSqlStrings();

    loadCache();
  }
Ejemplo n.º 3
0
  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);
    }
  }
Ejemplo n.º 4
0
 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);
   }
 }
Ejemplo n.º 5
0
 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);
   }
 }
Ejemplo n.º 6
0
 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);
   }
 }
Ejemplo n.º 7
0
  private final void setSqlStrings() {
    String idWhereClause = JdbcUtil.getIDWhereClause(extendedSessionIdSupported);
    String idColumns = JdbcUtil.getIDColumns(extendedSessionIdSupported);
    String idPlaceholders = JdbcUtil.getIDPlaceholders(extendedSessionIdSupported);

    SQL_UPDATE_SEQNUMS =
        "UPDATE "
            + sessionTableName
            + " SET incoming_seqnum=?, "
            + "outgoing_seqnum=? WHERE "
            + idWhereClause;

    SQL_INSERT_SESSION =
        "INSERT INTO "
            + sessionTableName
            + " ("
            + idColumns
            + ", creation_time,incoming_seqnum, outgoing_seqnum) VALUES ("
            + idPlaceholders
            + ",?,?,?)";

    SQL_GET_SEQNUMS =
        "SELECT creation_time, incoming_seqnum, outgoing_seqnum FROM "
            + sessionTableName
            + " WHERE "
            + idWhereClause;

    SQL_UPDATE_MESSAGE =
        "UPDATE "
            + messageTableName
            + " SET message=? "
            + "WHERE "
            + idWhereClause
            + " and msgseqnum=?";

    SQL_INSERT_MESSAGE =
        "INSERT INTO "
            + messageTableName
            + " ("
            + idColumns
            + ", msgseqnum,message) VALUES ("
            + idPlaceholders
            + ",?,?)";

    SQL_GET_MESSAGES =
        "SELECT message FROM "
            + messageTableName
            + " WHERE  "
            + idWhereClause
            + " and msgseqnum>=? and msgseqnum<=? "
            + "ORDER BY msgseqnum";

    SQL_UPDATE_SESSION =
        "UPDATE "
            + sessionTableName
            + " SET creation_time=?, "
            + "incoming_seqnum=?, outgoing_seqnum=? "
            + "WHERE "
            + idWhereClause;

    SQL_DELETE_MESSAGES = "DELETE FROM " + messageTableName + " WHERE " + idWhereClause;
  }
Ejemplo n.º 8
0
 private int setSessionIdParameters(PreparedStatement query, int offset) throws SQLException {
   return JdbcUtil.setSessionIdParameters(
       sessionID, query, offset, extendedSessionIdSupported, defaultSessionIdPropertyValue);
 }