KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > continuent > sequoia > controller > loadbalancer > tasks > OpenPersistentConnectionTask


1 /**
2  * Sequoia: Database clustering technology.
3  * Copyright (C) 2006 Continuent, Inc.
4  * Contact: sequoia@continuent.org
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  * Initial developer(s): Emmanuel Cecchet.
19  * Contributor(s): ______________________.
20  */

21
22 package org.continuent.sequoia.controller.loadbalancer.tasks;
23
24 import java.sql.SQLException JavaDoc;
25
26 import org.continuent.sequoia.controller.backend.DatabaseBackend;
27 import org.continuent.sequoia.controller.connection.AbstractConnectionManager;
28 import org.continuent.sequoia.controller.connection.PooledConnection;
29 import org.continuent.sequoia.controller.loadbalancer.BackendWorkerThread;
30 import org.continuent.sequoia.controller.requests.AbstractRequest;
31 import org.continuent.sequoia.controller.requests.UnknownReadRequest;
32
33 /**
34  * This class defines a OpenPersistentConnectionTask that opens a persistent
35  * connection.
36  *
37  * @author <a HREF="mailto:emmanuel.cecchet@continuent.com">Emmanuel Cecchet</a>
38  * @version 1.0
39  */

40 public class OpenPersistentConnectionTask extends AbstractTask
41 {
42   private AbstractRequest request;
43
44   /**
45    * Creates a new <code>OpenPersistentConnectionTask</code> object
46    *
47    * @param nbToComplete number of threads that must succeed before returning
48    * @param totalNb total number of threads
49    * @param login login requesting the connection closing
50    * @param persistentConnectionId id of the persistent connection to open
51    */

52   public OpenPersistentConnectionTask(int nbToComplete, int totalNb,
53       String JavaDoc login, long persistentConnectionId)
54   {
55     super(nbToComplete, totalNb, true, persistentConnectionId);
56     request = new UnknownReadRequest("", false, 0, "");
57     request.setLogin(login);
58     request.setPersistentConnection(true);
59     request.setPersistentConnectionId(persistentConnectionId);
60   }
61
62   /**
63    * @see org.continuent.sequoia.controller.loadbalancer.tasks.AbstractTask#executeTask(org.continuent.sequoia.controller.loadbalancer.BackendWorkerThread)
64    */

65   public void executeTask(BackendWorkerThread backendThread)
66       throws SQLException JavaDoc
67   {
68     DatabaseBackend backend = backendThread.getBackend();
69
70     if (!backend.canAcceptTasks(request))
71     {
72       // Backend is disabling we do not try to open new persistent connections
73
notifyCompletion(backendThread);
74       return;
75     }
76
77     AbstractConnectionManager cm = backend.getConnectionManager(request
78         .getLogin());
79     if (cm == null)
80     {
81       notifyFailure(backendThread, -1, new SQLException JavaDoc(
82           "No connection manager found for user " + request.getLogin()));
83       return;
84     }
85
86     try
87     {
88       // Get a new connection
89
PooledConnection c = cm.retrieveConnectionInAutoCommit(request);
90       backend.addPersistentConnection(request.getPersistentConnectionId(), c);
91
92       notifySuccess(backendThread);
93     }
94     catch (Exception JavaDoc e)
95     {
96       notifyFailure(backendThread, -1, e);
97     }
98   }
99
100   /**
101    * @see org.continuent.sequoia.controller.loadbalancer.tasks.AbstractTask#getRequest()
102    */

103   public AbstractRequest getRequest()
104   {
105     return null;
106   }
107
108   /**
109    * @see org.continuent.sequoia.controller.loadbalancer.tasks.AbstractTask#getTransactionId()
110    */

111   public long getTransactionId()
112   {
113     return request.getPersistentConnectionId();
114   }
115
116   /**
117    * @see org.continuent.sequoia.controller.loadbalancer.tasks.AbstractTask#isAutoCommit()
118    */

119   public boolean isAutoCommit()
120   {
121     return true;
122   }
123
124   /**
125    * @see java.lang.Object#toString()
126    */

127   public String JavaDoc toString()
128   {
129     return "Open persistent connection " + request.getPersistentConnectionId();
130   }
131
132 }
133
Popular Tags