KickJava   Java API By Example, From Geeks To Geeks.

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


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.loadbalancer.BackendWorkerThread;
29 import org.continuent.sequoia.controller.requests.AbstractRequest;
30
31 /**
32  * This class defines a ClosePersistentConnectionTask that closes a persistent
33  * connection.
34  *
35  * @author <a HREF="mailto:emmanuel.cecchet@continuent.com">Emmanuel Cecchet</a>
36  * @version 1.0
37  */

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

51   public ClosePersistentConnectionTask(int nbToComplete, int totalNb,
52       String JavaDoc login, long persistentConnectionId)
53   {
54     super(nbToComplete, totalNb, true, persistentConnectionId);
55     this.login = login;
56     this.persistentConnectionId = persistentConnectionId;
57   }
58
59   /**
60    * @see org.continuent.sequoia.controller.loadbalancer.tasks.AbstractTask#executeTask(org.continuent.sequoia.controller.loadbalancer.BackendWorkerThread)
61    */

62   public void executeTask(BackendWorkerThread backendThread)
63       throws SQLException JavaDoc
64   {
65     DatabaseBackend backend = backendThread.getBackend();
66     AbstractConnectionManager cm = backend.getConnectionManager(login);
67     if (cm == null)
68     {
69       // Nothing to do, no such connection
70
notifyCompletion(backendThread);
71       return;
72     }
73
74     try
75     {
76       if (backend.canAcceptTasks(persistentConnectionId))
77       {
78         // Release the connection
79
cm.releasePersistentConnectionInAutoCommit(persistentConnectionId);
80         backend.removePersistentConnection(persistentConnectionId);
81         notifySuccess(backendThread);
82       }
83       else
84         notifyCompletion(backendThread);
85     }
86     catch (Exception JavaDoc e)
87     {
88       notifyFailure(backendThread, -1, e);
89     }
90   }
91
92   /**
93    * @see org.continuent.sequoia.controller.loadbalancer.tasks.AbstractTask#getRequest()
94    */

95   public AbstractRequest getRequest()
96   {
97     return null;
98   }
99
100   /**
101    * @see org.continuent.sequoia.controller.loadbalancer.tasks.AbstractTask#getTransactionId()
102    */

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

111   public boolean isAutoCommit()
112   {
113     return true;
114   }
115
116   /**
117    * @see java.lang.Object#toString()
118    */

119   public String JavaDoc toString()
120   {
121     return "Close persistent connection " + persistentConnectionId;
122   }
123
124 }
125
Popular Tags