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.loadbalancer.tasks;
021:
022: import java.sql.SQLException;
023:
024: import org.continuent.sequoia.controller.backend.DatabaseBackend;
025: import org.continuent.sequoia.controller.connection.AbstractConnectionManager;
026: import org.continuent.sequoia.controller.loadbalancer.BackendWorkerThread;
027: import org.continuent.sequoia.controller.requests.AbstractRequest;
028:
029: /**
030: * This class defines a ClosePersistentConnectionTask that closes a persistent
031: * connection.
032: *
033: * @author <a href="mailto:emmanuel.cecchet@continuent.com">Emmanuel Cecchet</a>
034: * @version 1.0
035: */
036: public class ClosePersistentConnectionTask extends AbstractTask {
037: private String login;
038: private long persistentConnectionId;
039:
040: /**
041: * Creates a new <code>ClosePersistentConnectionTask</code> object
042: *
043: * @param nbToComplete number of threads that must succeed before returning
044: * @param totalNb total number of threads
045: * @param login login requesting the connection closing
046: * @param persistentConnectionId id of the persistent connection to close
047: */
048: public ClosePersistentConnectionTask(int nbToComplete, int totalNb,
049: String login, long persistentConnectionId) {
050: super (nbToComplete, totalNb, true, persistentConnectionId);
051: this .login = login;
052: this .persistentConnectionId = persistentConnectionId;
053: }
054:
055: /**
056: * @see org.continuent.sequoia.controller.loadbalancer.tasks.AbstractTask#executeTask(org.continuent.sequoia.controller.loadbalancer.BackendWorkerThread)
057: */
058: public void executeTask(BackendWorkerThread backendThread)
059: throws SQLException {
060: DatabaseBackend backend = backendThread.getBackend();
061: AbstractConnectionManager cm = backend
062: .getConnectionManager(login);
063: if (cm == null) {
064: // Nothing to do, no such connection
065: notifyCompletion(backendThread);
066: return;
067: }
068:
069: try {
070: if (backend.canAcceptTasks(persistentConnectionId)) {
071: // Release the connection
072: cm
073: .releasePersistentConnectionInAutoCommit(persistentConnectionId);
074: backend
075: .removePersistentConnection(persistentConnectionId);
076: notifySuccess(backendThread);
077: } else
078: notifyCompletion(backendThread);
079: } catch (Exception e) {
080: notifyFailure(backendThread, -1, e);
081: }
082: }
083:
084: /**
085: * @see org.continuent.sequoia.controller.loadbalancer.tasks.AbstractTask#getRequest()
086: */
087: public AbstractRequest getRequest() {
088: return null;
089: }
090:
091: /**
092: * @see org.continuent.sequoia.controller.loadbalancer.tasks.AbstractTask#getTransactionId()
093: */
094: public long getTransactionId() {
095: return persistentConnectionId;
096: }
097:
098: /**
099: * @see org.continuent.sequoia.controller.loadbalancer.tasks.AbstractTask#isAutoCommit()
100: */
101: public boolean isAutoCommit() {
102: return true;
103: }
104:
105: /**
106: * @see java.lang.Object#toString()
107: */
108: public String toString() {
109: return "Close persistent connection " + persistentConnectionId;
110: }
111:
112: }
|