001: /**
002: * Sequoia: Database clustering technology.
003: * Copyright (C) 2005 Emic Networks.
004: * Contact: sequoia@continuent.org
005: *
006: * Licensed under the Apache License, Version 2.0 (the "License");
007: * you may not use this file except in compliance with the License.
008: * You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing, software
013: * distributed under the License is distributed on an "AS IS" BASIS,
014: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015: * See the License for the specific language governing permissions and
016: * limitations under the License.
017: *
018: * Initial developer(s): Jeff Mesnil.
019: * Contributor(s): ______________________.
020: */package org.continuent.sequoia.controller.recoverylog.events;
021:
022: import java.sql.Connection;
023: import java.sql.PreparedStatement;
024: import java.sql.ResultSet;
025: import java.sql.SQLException;
026:
027: import org.continuent.sequoia.common.i18n.Translate;
028: import org.continuent.sequoia.controller.recoverylog.CheckpointLogEntry;
029: import org.continuent.sequoia.controller.recoverylog.LoggerThread;
030: import org.continuent.sequoia.controller.recoverylog.RecoveryLog;
031:
032: /**
033: * This class defines a GetCheckpointLogEntryEvent.
034: * <p>
035: * This event is used to retrieve a log entry associated to a checkpoint.
036: * <br/>Once this event has been execute, the log entry is available through the
037: * <code>getCheckpointLogEntry()</code> method
038: */
039: public class GetCheckpointLogEntryEvent implements LogEvent {
040:
041: private Connection connection;
042: private RecoveryLog recoveryLog;
043: private String checkpointName;
044: private CheckpointLogEntry logEntry = null;
045: private SQLException catchedException = null;
046:
047: /**
048: * Creates a new <code>GetCheckpointLogEntryEvent</code> object
049: *
050: * @param connection the connection used to access the checkpoint table
051: * @param recoveryLog the recovery log we are attached to
052: * @param checkpointName name of the checkpoint to look for
053: */
054: public GetCheckpointLogEntryEvent(Connection connection,
055: RecoveryLog recoveryLog, String checkpointName) {
056: this .connection = connection;
057: this .recoveryLog = recoveryLog;
058: this .checkpointName = checkpointName;
059: }
060:
061: /**
062: * @see org.continuent.sequoia.controller.recoverylog.events.LogEvent#belongToTransaction(long)
063: */
064: public boolean belongToTransaction(long tid) {
065: return false;
066: }
067:
068: /**
069: * @see org.continuent.sequoia.controller.recoverylog.events.LogEvent#execute(org.continuent.sequoia.controller.recoverylog.LoggerThread)
070: */
071: public void execute(LoggerThread loggerThread) {
072: PreparedStatement stmt = null;
073: ResultSet rs = null;
074: try {
075: stmt = connection
076: .prepareStatement("SELECT "
077: + recoveryLog.getLogTableSqlColumnName()
078: + ",vlogin,auto_conn_tran,transaction_id,request_id FROM "
079: + recoveryLog.getLogTableName() + ","
080: + recoveryLog.getCheckpointTableName()
081: + " WHERE name LIKE ? and "
082: + recoveryLog.getLogTableName()
083: + ".log_id="
084: + recoveryLog.getCheckpointTableName()
085: + ".log_id");
086: stmt.setString(1, checkpointName);
087: rs = stmt.executeQuery();
088:
089: if (rs.next()) {
090: logEntry = new CheckpointLogEntry(rs.getString(1), rs
091: .getString(2), rs.getString(3), rs.getLong(4),
092: rs.getLong(5));
093: return;
094: }
095:
096: String msg = Translate.get(
097: "recovery.jdbc.checkpoint.not.found",
098: checkpointName);
099: throw new SQLException(msg);
100: } catch (SQLException e) {
101: catchedException = new SQLException(Translate.get(
102: "recovery.jdbc.checkpoint.not.found.error",
103: new String[] { checkpointName, e.getMessage() }));
104: } finally {
105: try {
106: if (rs != null)
107: rs.close();
108: } catch (Exception ignore) {
109: }
110: try {
111: if (stmt != null)
112: stmt.close();
113: } catch (Exception ignore) {
114: }
115: synchronized (this ) {
116: notify();
117: }
118: }
119: }
120:
121: /**
122: * Returns the catched exception if an error occured during the execution.
123: * Returns null if no error occured or the query did not execute yet.
124: *
125: * @return Returns the catchedException.
126: */
127: public final SQLException getCatchedException() {
128: return catchedException;
129: }
130:
131: /**
132: * Gets the CheckpointLogEntry corresponding to the checkpoint
133: *
134: * @return the CheckpointLogEntry that has been found
135: * @throws SQLException if an error occurs
136: */
137: public CheckpointLogEntry getCheckpointLogEntry()
138: throws SQLException {
139: if (logEntry == null) {
140: throw catchedException;
141: }
142: return logEntry;
143: }
144:
145: /**
146: * @see java.lang.Object#toString()
147: */
148: public String toString() {
149: return "GetCheckpointLogEntryEvent for checkpoint "
150: + checkpointName;
151: }
152: }
|