001: /**
002: * Sequoia: Database clustering technology.
003: * Copyright (C) 2006 Continuent, Inc.
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): Emmanuel Cecchet.
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: import java.util.ArrayList;
027: import java.util.List;
028:
029: import org.continuent.sequoia.controller.recoverylog.LoggerThread;
030:
031: /**
032: * This class defines a GetAllBeginLoggedAfterIdEvent.
033: * <p>
034: * This event is used to retrieve a log ID associated to a checkpoint. <br>
035: * Once this event has been execute, the log ID is available through the
036: * <code>getCheckpointRequestId()</code> method
037: */
038: public class GetAllBeginLoggedAfterIdEvent implements LogEvent {
039:
040: private Connection connection;
041: private String logTableName;
042: private long checkpointId;
043: private List beginList = null;
044: private SQLException catchedException = null;
045: private String logTableSqlColumnName;
046:
047: /**
048: * Creates a new <code>GetAllBeginLoggedAfterIdEvent</code> object
049: *
050: * @param connection the connection used to access the checkpoint table
051: * @param logTableName the name of the checkpoint table
052: * @param logTableSqlColumnName name of the recovery log table sql column
053: * @param checkpointId the name of the checkpoint
054: */
055: public GetAllBeginLoggedAfterIdEvent(Connection connection,
056: String logTableName, String logTableSqlColumnName,
057: long checkpointId) {
058: this .connection = connection;
059: this .logTableName = logTableName;
060: this .checkpointId = checkpointId;
061: this .logTableSqlColumnName = logTableSqlColumnName;
062: }
063:
064: /**
065: * @see org.continuent.sequoia.controller.recoverylog.events.LogEvent#belongToTransaction(long)
066: */
067: public boolean belongToTransaction(long tid) {
068: return false;
069: }
070:
071: /**
072: * @see org.continuent.sequoia.controller.recoverylog.events.LogEvent#execute(org.continuent.sequoia.controller.recoverylog.LoggerThread)
073: */
074: public void execute(LoggerThread loggerThread) {
075: PreparedStatement stmt = null;
076: ResultSet rs = null;
077: try {
078: stmt = connection
079: .prepareStatement("SELECT transaction_id FROM "
080: + logTableName + " WHERE log_id>? AND "
081: + logTableSqlColumnName + " LIKE ?");
082: stmt.setLong(1, checkpointId);
083: stmt.setString(2, "begin");
084: rs = stmt.executeQuery();
085: beginList = new ArrayList();
086: while (rs.next())
087: beginList.add(new Long(rs.getLong(1)));
088: } catch (SQLException e) {
089: catchedException = e;
090: } finally {
091: try {
092: if (rs != null)
093: rs.close();
094: } catch (Exception ignore) {
095: }
096: try {
097: if (stmt != null)
098: stmt.close();
099: } catch (Exception ignore) {
100: }
101: synchronized (this ) {
102: notify();
103: }
104: }
105: }
106:
107: /**
108: * Returns the catched exception if an error occured during the execution.
109: * Returns null if no error occured or the query did not execute yet.
110: *
111: * @return Returns the catchedException.
112: */
113: public final SQLException getCatchedException() {
114: return catchedException;
115: }
116:
117: /**
118: * Return the list of transactions started after the given id
119: *
120: * @return begin list
121: * @throws SQLException if an error occured
122: */
123: public List getBeginList() throws SQLException {
124: if (beginList == null)
125: throw catchedException;
126: return beginList;
127: }
128:
129: /**
130: * @see java.lang.Object#toString()
131: */
132: public String toString() {
133: return "GetCheckpointLogIdEvent for checkpoint " + checkpointId;
134: }
135:
136: }
|