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.connection.PooledConnection;
027: import org.continuent.sequoia.controller.loadbalancer.BackendWorkerThread;
028: import org.continuent.sequoia.controller.requests.AbstractRequest;
029: import org.continuent.sequoia.controller.requests.UnknownReadRequest;
030:
031: /**
032: * This class defines a OpenPersistentConnectionTask that opens a persistent
033: * connection.
034: *
035: * @author <a href="mailto:emmanuel.cecchet@continuent.com">Emmanuel Cecchet</a>
036: * @version 1.0
037: */
038: public class OpenPersistentConnectionTask extends AbstractTask {
039: private AbstractRequest request;
040:
041: /**
042: * Creates a new <code>OpenPersistentConnectionTask</code> object
043: *
044: * @param nbToComplete number of threads that must succeed before returning
045: * @param totalNb total number of threads
046: * @param login login requesting the connection closing
047: * @param persistentConnectionId id of the persistent connection to open
048: */
049: public OpenPersistentConnectionTask(int nbToComplete, int totalNb,
050: String login, long persistentConnectionId) {
051: super (nbToComplete, totalNb, true, persistentConnectionId);
052: request = new UnknownReadRequest("", false, 0, "");
053: request.setLogin(login);
054: request.setPersistentConnection(true);
055: request.setPersistentConnectionId(persistentConnectionId);
056: }
057:
058: /**
059: * @see org.continuent.sequoia.controller.loadbalancer.tasks.AbstractTask#executeTask(org.continuent.sequoia.controller.loadbalancer.BackendWorkerThread)
060: */
061: public void executeTask(BackendWorkerThread backendThread)
062: throws SQLException {
063: DatabaseBackend backend = backendThread.getBackend();
064:
065: if (!backend.canAcceptTasks(request)) {
066: // Backend is disabling we do not try to open new persistent connections
067: notifyCompletion(backendThread);
068: return;
069: }
070:
071: AbstractConnectionManager cm = backend
072: .getConnectionManager(request.getLogin());
073: if (cm == null) {
074: notifyFailure(backendThread, -1, new SQLException(
075: "No connection manager found for user "
076: + request.getLogin()));
077: return;
078: }
079:
080: try {
081: // Get a new connection
082: PooledConnection c = cm
083: .retrieveConnectionInAutoCommit(request);
084: backend.addPersistentConnection(request
085: .getPersistentConnectionId(), c);
086:
087: notifySuccess(backendThread);
088: } catch (Exception e) {
089: notifyFailure(backendThread, -1, e);
090: }
091: }
092:
093: /**
094: * @see org.continuent.sequoia.controller.loadbalancer.tasks.AbstractTask#getRequest()
095: */
096: public AbstractRequest getRequest() {
097: return null;
098: }
099:
100: /**
101: * @see org.continuent.sequoia.controller.loadbalancer.tasks.AbstractTask#getTransactionId()
102: */
103: public long getTransactionId() {
104: return request.getPersistentConnectionId();
105: }
106:
107: /**
108: * @see org.continuent.sequoia.controller.loadbalancer.tasks.AbstractTask#isAutoCommit()
109: */
110: public boolean isAutoCommit() {
111: return true;
112: }
113:
114: /**
115: * @see java.lang.Object#toString()
116: */
117: public String toString() {
118: return "Open persistent connection "
119: + request.getPersistentConnectionId();
120: }
121:
122: }
|