001: /**
002: * Sequoia: Database clustering technology.
003: * Copyright (C) 2005 Emic Networks
004: * Science And Control (INRIA).
005: * Contact: sequoia@continuent.org
006: *
007: * Licensed under the Apache License, Version 2.0 (the "License");
008: * you may not use this file except in compliance with the License.
009: * You may obtain a copy of the License at
010: *
011: * http://www.apache.org/licenses/LICENSE-2.0
012: *
013: * Unless required by applicable law or agreed to in writing, software
014: * distributed under the License is distributed on an "AS IS" BASIS,
015: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
016: * See the License for the specific language governing permissions and
017: * limitations under the License.
018: *
019: * Initial developer(s): Emmanuel Cecchet.
020: * Contributor(s): ______________________.
021: */package org.continuent.sequoia.controller.virtualdatabase.protocol;
022:
023: import java.io.Serializable;
024: import java.sql.SQLException;
025:
026: import org.continuent.hedera.common.Member;
027: import org.continuent.sequoia.common.exceptions.VirtualDatabaseStartingException;
028: import org.continuent.sequoia.common.log.Trace;
029: import org.continuent.sequoia.controller.requestmanager.distributed.DistributedRequestManager;
030: import org.continuent.sequoia.controller.virtualdatabase.DistributedVirtualDatabase;
031:
032: /**
033: * This class defines a DistributedTransactionMarker which is used to transport
034: * commit/rollback/savepoint type of commands.
035: *
036: * @author <a href="mailto:emmanuel.cecchet@emicnetworks.com">Emmanuel Cecchet
037: * </a>
038: * @version 1.0
039: */
040: public abstract class DistributedTransactionMarker extends
041: DistributedVirtualDatabaseMessage {
042:
043: protected long transactionId;
044:
045: /**
046: * Creates a new <code>DistributedTransactionMarker</code> object
047: *
048: * @param transactionId the transaction identifier
049: */
050: public DistributedTransactionMarker(long transactionId) {
051: this .transactionId = transactionId;
052: }
053:
054: /**
055: * Schedule the command (i.e. commit or rollback). This method blocks until
056: * the command is scheduled.
057: *
058: * @param drm a distributed request manager
059: * @return the object inserted in the total order queue
060: * @throws SQLException if an error occurs.
061: */
062: public abstract Object scheduleCommand(DistributedRequestManager drm)
063: throws SQLException;
064:
065: /**
066: * Code to be executed by the distributed request manager receiving the
067: * command.
068: *
069: * @param drm a distributed request manager
070: * @return a Serializable object to be sent back to the caller
071: * @throws SQLException if an error occurs.
072: */
073: public abstract Serializable executeCommand(
074: DistributedRequestManager drm) throws SQLException;
075:
076: /**
077: * Returns the transactionId value.
078: *
079: * @return Returns the transactionId.
080: */
081: public long getTransactionId() {
082: return transactionId;
083: }
084:
085: /**
086: * @see org.continuent.sequoia.controller.virtualdatabase.protocol.DistributedVirtualDatabaseMessage#handleMessageSingleThreaded(org.continuent.sequoia.controller.virtualdatabase.DistributedVirtualDatabase,
087: * org.continuent.hedera.common.Member)
088: */
089: public Object handleMessageSingleThreaded(
090: DistributedVirtualDatabase dvdb, Member sender) {
091: if (!dvdb.isVirtualDatabaseStarted())
092: return new VirtualDatabaseStartingException();
093:
094: Trace distributedRequestLogger = dvdb
095: .getDistributedRequestLogger();
096: if (distributedRequestLogger.isInfoEnabled())
097: distributedRequestLogger.info(toString());
098:
099: try {
100: return scheduleCommand((DistributedRequestManager) dvdb
101: .getRequestManager());
102: } catch (SQLException e) {
103: return e;
104: }
105: }
106:
107: /**
108: * @see org.continuent.sequoia.controller.virtualdatabase.protocol.DistributedVirtualDatabaseMessage#handleMessageMultiThreaded(org.continuent.sequoia.controller.virtualdatabase.DistributedVirtualDatabase,
109: * org.continuent.hedera.common.Member, java.lang.Object)
110: */
111: public Serializable handleMessageMultiThreaded(
112: DistributedVirtualDatabase dvdb, Member sender,
113: Object handleMessageSingleThreadedResult) {
114: if (handleMessageSingleThreadedResult != null) {
115: if (handleMessageSingleThreadedResult instanceof Exception)
116: return (Serializable) handleMessageSingleThreadedResult;
117: }
118:
119: try {
120: return executeCommand((DistributedRequestManager) dvdb
121: .getRequestManager());
122: } catch (SQLException e) {
123: return e;
124: }
125: }
126:
127: /**
128: * @see java.lang.Object#hashCode()
129: */
130: public int hashCode() {
131: return (int) transactionId;
132: }
133:
134: /**
135: * @see java.lang.Object#equals(java.lang.Object)
136: */
137: public boolean equals(Object obj) {
138: if (obj == null)
139: return false;
140: if (obj.getClass().equals(this .getClass()))
141: return transactionId == ((DistributedTransactionMarker) obj)
142: .getTransactionId();
143: else
144: return false;
145: }
146: }
|