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): Damian Arregui.
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.LoggerThread;
029: import org.continuent.sequoia.controller.recoverylog.RecoveryLog;
030:
031: /**
032: * This class defines a FindRollbackEvent.
033: * <p>
034: * This event is used to retrieve a rollback associated to a transaction ID.
035: * <br/>Once this event has been executed, the fact that the rollback was found
036: * or not can be checked through the <code>wasFound()</code> method.
037: *
038: * @author <a href="mailto:damian.arregui@continuent.com">Damian Arregui</a>
039: * @version 1.0
040: */
041: public class FindRollbackEvent implements LogEvent {
042: private Connection connection;
043: private RecoveryLog recoveryLog;
044: private long transactionId;
045: private boolean found = false;
046: private String status = LogEntry.MISSING;
047: private SQLException catchedException = null;
048:
049: /**
050: * Creates a new <code>FindRollbackEvent</code> object
051: *
052: * @param connection the connection used to access the recovery log table
053: * @param recoveryLog the recovery log object
054: * @param requestId the ID of the request to be found
055: */
056: public FindRollbackEvent(Connection connection,
057: RecoveryLog recoveryLog, long requestId) {
058: this .connection = connection;
059: this .recoveryLog = recoveryLog;
060: this .transactionId = requestId;
061: }
062:
063: /**
064: * @see org.continuent.sequoia.controller.recoverylog.events.LogEvent#belongToTransaction(long)
065: */
066: public boolean belongToTransaction(long tid) {
067: return false;
068: }
069:
070: /**
071: * @see org.continuent.sequoia.controller.recoverylog.events.LogEvent#execute(org.continuent.sequoia.controller.recoverylog.LoggerThread)
072: */
073: public void execute(LoggerThread loggerThread) {
074: PreparedStatement stmt = null;
075: ResultSet rs = null;
076: try {
077: stmt = connection
078: .prepareStatement("SELECT log_id, exec_status FROM "
079: + recoveryLog.getLogTableName()
080: + " WHERE ((transaction_id=?) AND ("
081: + recoveryLog.getLogTableSqlColumnName()
082: + "='rollback'))");
083: stmt.setLong(1, transactionId);
084: rs = stmt.executeQuery();
085:
086: // Found?
087: found = rs.next();
088: if (found)
089: status = rs.getString("exec_status");
090: } catch (SQLException e) {
091: catchedException = new SQLException(Translate.get(
092: "recovery.jdbc.rollback.not.found.error",
093: new Object[] { new Long(transactionId),
094: e.getMessage() }));
095: } finally {
096: try {
097: if (rs != null)
098: rs.close();
099: } catch (Exception ignore) {
100: }
101: try {
102: if (stmt != null)
103: stmt.close();
104: } catch (Exception ignore) {
105: }
106: synchronized (this ) {
107: notify();
108: }
109: }
110: }
111:
112: /**
113: * Returns the catched exception if an error occured during the execution.
114: * Returns null if no error occured or the query did not execute yet.
115: *
116: * @return Returns the catchedException.
117: */
118: public final SQLException getCatchedException() {
119: return catchedException;
120: }
121:
122: /**
123: * Returns the status of the rollback statement if found in the recovery log
124: *
125: * @return the status of the rollback statement found in the recovery log, or
126: * LogEntry.MISSING if not found
127: */
128: public String getStatus() {
129: return status;
130: }
131:
132: /**
133: * Checks if the rollback was found.
134: *
135: * @return true if the rollback has been found, false otherwise
136: */
137: public boolean wasFound() {
138: return found;
139: }
140:
141: /**
142: * @see java.lang.Object#toString()
143: */
144: public String toString() {
145: return "FindRollbackEvent for request ID " + transactionId;
146: }
147: }
|