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.exceptions.NoResultAvailableException;
028: import org.continuent.sequoia.common.i18n.Translate;
029: import org.continuent.sequoia.controller.recoverylog.LoggerThread;
030: import org.continuent.sequoia.controller.recoverylog.RecoveryLog;
031:
032: /**
033: * This class defines a GetUpdateCountEvent.
034: * <p>
035: * This event is used to retrieve an update count associated to a request ID.
036: * <br/>Once this event has been executed, the update count is available through
037: * the <code>getUpdateCount)</code> method.
038: *
039: * @author <a href="mailto:damian.arregui@continuent.com">Damian Arregui</a>
040: * @version 1.0
041: */
042: public class GetUpdateCountEvent implements LogEvent {
043: private Connection connection;
044: private RecoveryLog recoveryLog;
045: private long requestId;
046: private int updateCount = -1;
047: private Exception catchedException = null;
048:
049: /**
050: * Creates a new <code>GetUpdateCountEvent</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 GetUpdateCountEvent(Connection connection,
057: RecoveryLog recoveryLog, long requestId) {
058: this .connection = connection;
059: this .recoveryLog = recoveryLog;
060: this .requestId = 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 update_count, exec_status FROM "
079: + recoveryLog.getLogTableName()
080: + " WHERE request_id=?");
081: stmt.setLong(1, requestId);
082: rs = stmt.executeQuery();
083: if (rs.next()) {
084: // Found!
085: updateCount = rs.getInt(1);
086: if (!LogEntry.SUCCESS.equals(rs.getString(2)))
087: throw new SQLException(Translate
088: .get("recovery.jdbc.update.failed"));
089: } else {
090: // Not found
091: String msg = Translate.get(
092: "recovery.jdbc.updatecount.not.found",
093: requestId);
094: catchedException = new NoResultAvailableException(msg);
095: }
096: } catch (SQLException e) {
097: catchedException = new SQLException(Translate
098: .get("recovery.jdbc.updatecount.not.found.error",
099: new Object[] { new Long(requestId),
100: e.getMessage() }));
101: } finally {
102: try {
103: if (rs != null)
104: rs.close();
105: } catch (Exception ignore) {
106: }
107: try {
108: if (stmt != null)
109: stmt.close();
110: } catch (Exception ignore) {
111: }
112: synchronized (this ) {
113: notify();
114: }
115: }
116: }
117:
118: /**
119: * Returns the catched exception if an error occured during the execution (can
120: * be SQLException or NoResultAvailableException). Returns null if no error
121: * occured or the query did not execute yet.
122: *
123: * @return Returns the catchedException.
124: */
125: public final Exception getCatchedException() {
126: return catchedException;
127: }
128:
129: /**
130: * Gets the update count corresponding to the request ID
131: *
132: * @return the update count that has been found
133: */
134: public int getUpdateCount() {
135: return updateCount;
136: }
137:
138: /**
139: * @see java.lang.Object#toString()
140: */
141: public String toString() {
142: return "GetUpdateCountEvent for request ID " + requestId;
143: }
144: }
|