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: import org.continuent.sequoia.controller.requestmanager.distributed.DistributedRequestManager;
031:
032: /**
033: * This class defines a FindLastIdEvent.
034: * <p>
035: * This event is used to retrieve the last ID for requests, transactions or
036: * persistent connections in the log which was allocated for (by) a given
037: * controller ID.
038: * <p>
039: * <br/>Once this event has been executed, the returned value can be accessed
040: * through the <code>getValue()</code> method. If no previous ID was used in
041: * this space, returned value will be controller ID.
042: *
043: * @author <a href="mailto:damian.arregui@continuent.com">Damian Arregui</a>
044: * @version 1.0
045: */
046: public class FindLastIdEvent implements LogEvent {
047: private Connection connection;
048: private RecoveryLog recoveryLog;
049: private String request;
050: private long controllerId;
051: private String sql;
052: private long value;
053: private SQLException catchedException = null;
054:
055: /**
056: * Creates a new <code>FindLastIdEvent</code> object
057: *
058: * @param connection the connection used to access the recovery log table
059: * @param recoveryLog the recovery log object
060: * @param requestId the ID of the request to be found
061: */
062: public FindLastIdEvent(Connection connection,
063: RecoveryLog recoveryLog, String request, long controllerId,
064: String sql) {
065: this .connection = connection;
066: this .recoveryLog = recoveryLog;
067: this .request = request;
068: this .controllerId = controllerId;
069: this .sql = sql;
070: }
071:
072: /**
073: * @see org.continuent.sequoia.controller.recoverylog.events.LogEvent#belongToTransaction(long)
074: */
075: public boolean belongToTransaction(long tid) {
076: return false;
077: }
078:
079: /**
080: * @see org.continuent.sequoia.controller.recoverylog.events.LogEvent#execute(org.continuent.sequoia.controller.recoverylog.LoggerThread)
081: */
082: public void execute(LoggerThread loggerThread) {
083: PreparedStatement stmt = null;
084: ResultSet rs = null;
085: try {
086: stmt = connection.prepareStatement(request);
087: long minIdForThisController = controllerId;
088: long maxIdForThisController = minIdForThisController
089: | ~DistributedRequestManager.CONTROLLER_ID_BIT_MASK;
090: stmt.setLong(1, minIdForThisController);
091: stmt.setLong(2, maxIdForThisController);
092: if (sql != null)
093: stmt.setString(3, sql);
094: rs = stmt.executeQuery();
095: value = 0;
096: if (rs.next())
097: value = rs.getLong(1);
098: if (value == 0)
099: // Table is empty: return controllerId
100: value = controllerId;
101: } catch (SQLException e) {
102: catchedException = e;
103: } finally {
104: try {
105: if (rs != null)
106: rs.close();
107: } catch (Exception ignore) {
108: }
109: try {
110: if (stmt != null)
111: stmt.close();
112: } catch (Exception ignore) {
113: }
114: synchronized (this ) {
115: notify();
116: }
117: }
118: }
119:
120: /**
121: * @return last ID delivered by controller or controller ID if none was
122: * delivered.
123: */
124: public long getValue() {
125: return value;
126: }
127:
128: /**
129: * Returns the catched exception if an error occured during the execution.
130: * Returns null if no error occured or the query did not execute yet.
131: *
132: * @return Returns the catchedException.
133: */
134: public final SQLException getCatchedException() {
135: return catchedException;
136: }
137:
138: /**
139: * @see java.lang.Object#toString()
140: */
141: public String toString() {
142: return "FindLastIdEvent for controller ID " + controllerId;
143: }
144: }
|