KickJava   Java API By Example, From Geeks To Geeks.

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


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

23
24 package org.continuent.sequoia.controller.loadbalancer.tasks;
25
26 import java.sql.Connection JavaDoc;
27 import java.sql.SQLException JavaDoc;
28
29 import org.continuent.sequoia.common.i18n.Translate;
30 import org.continuent.sequoia.common.log.Trace;
31 import org.continuent.sequoia.controller.backend.DatabaseBackend;
32 import org.continuent.sequoia.controller.connection.AbstractConnectionManager;
33 import org.continuent.sequoia.controller.connection.PooledConnection;
34 import org.continuent.sequoia.controller.loadbalancer.BackendWorkerThread;
35 import org.continuent.sequoia.controller.requestmanager.TransactionMetaData;
36 import org.continuent.sequoia.controller.requests.AbstractRequest;
37
38 /**
39  * Task to commit a transaction.
40  *
41  * @author <a HREF="mailto:Emmanuel.Cecchet@inria.fr">Emmanuel Cecchet </a>
42  * @author <a HREF="mailto:Julie.Marguerite@inria.fr">Julie Marguerite </a>
43  * @version 1.0
44  */

45 public class CommitTask extends AbstractTask
46 {
47   // Transaction metadata (login, transaction id, timeout)
48
private TransactionMetaData tm;
49
50   static Trace endUserLogger = Trace
51                                                 .getLogger("org.continuent.sequoia.enduser");
52
53   /**
54    * Commits a transaction given a login and a transaction id.
55    *
56    * @param nbToComplete number of threads that must succeed before returning
57    * @param totalNb total number of threads
58    * @param tm transaction metadata
59    * @throws NullPointerException if tm is null
60    */

61   public CommitTask(int nbToComplete, int totalNb, TransactionMetaData tm)
62       throws NullPointerException JavaDoc
63   {
64     super(nbToComplete, totalNb, tm.isPersistentConnection(), tm
65         .getPersistentConnectionId());
66     if (tm == null)
67       throw new NullPointerException JavaDoc("Unexpected null metadata in BeginTask");
68     this.tm = tm;
69   }
70
71   /**
72    * Commits a transaction with the given backend thread.
73    *
74    * @param backendThread the backend thread that will execute the task
75    * @throws SQLException if an error occurs
76    */

77   public void executeTask(BackendWorkerThread backendThread)
78       throws SQLException JavaDoc
79   {
80     DatabaseBackend backend = backendThread.getBackend();
81     Long JavaDoc lTid = new Long JavaDoc(tm.getTransactionId());
82
83     AbstractConnectionManager cm = backend.getConnectionManager(tm.getLogin());
84     if (cm == null)
85     {
86       SQLException JavaDoc se = new SQLException JavaDoc(
87           "No Connection Manager for Virtual Login:" + tm.getLogin());
88       try
89       {
90         notifyFailure(backendThread, -1, se);
91       }
92       catch (SQLException JavaDoc ignore)
93       {
94
95       }
96       throw se;
97     }
98     PooledConnection pc = cm.retrieveConnectionForTransaction(tm
99         .getTransactionId());
100
101     // Sanity check
102
if (pc == null)
103     { // Bad connection
104
backend.stopTransaction(lTid);
105       SQLException JavaDoc se = new SQLException JavaDoc(
106           "Unable to retrieve connection for transaction "
107               + tm.getTransactionId());
108
109       try
110       { // All backends failed, just ignore
111
if (!notifyFailure(backendThread, tm.getTimeout(), se))
112           return;
113       }
114       catch (SQLException JavaDoc ignore)
115       {
116       }
117       // Disable this backend (it is no more in sync) by killing the backend
118
// thread
119
backendThread.getLoadBalancer().disableBackend(backend, true);
120       String JavaDoc msg = "Failed to commit transaction " + tm.getTransactionId()
121           + " on backend " + backend.getName() + " but " + getSuccess()
122           + " succeeded (" + se + ")";
123       backendThread.getLogger().error(msg);
124       endUserLogger.error(Translate.get("loadbalancer.backend.disabling",
125           backend.getName()));
126       throw new SQLException JavaDoc(msg);
127     }
128
129     // Execute Query
130
try
131     {
132       Connection JavaDoc c = pc.getConnection();
133       c.commit();
134       c.setAutoCommit(true);
135     }
136     catch (Exception JavaDoc e)
137     {
138       try
139       {
140         if (!notifyFailure(backendThread, tm.getTimeout(), new SQLException JavaDoc(e
141             .getMessage())))
142           return;
143       }
144       catch (SQLException JavaDoc ignore)
145       {
146       }
147       // Disable this backend (it is no more in sync) by killing the backend
148
// thread
149
backendThread.getLoadBalancer().disableBackend(backend, true);
150       String JavaDoc msg = "Failed to commit transaction " + tm.getTransactionId()
151           + " on backend " + backend.getName() + " but " + getSuccess()
152           + " succeeded (" + e + ")";
153       backendThread.getLogger().error(msg);
154       endUserLogger.error(Translate.get("loadbalancer.backend.disabling",
155           backend.getName()));
156       throw new SQLException JavaDoc(msg);
157     }
158     finally
159     {
160       cm.releaseConnectionForTransaction(tm.getTransactionId());
161       backend.stopTransaction(lTid);
162       backend.getTaskQueues().releaseLocksAndCheckForPriorityInversion(tm);
163     }
164     notifySuccess(backendThread);
165   }
166
167   /**
168    * @see org.continuent.sequoia.controller.loadbalancer.tasks.AbstractTask#getRequest()
169    */

170   public AbstractRequest getRequest()
171   {
172     return null;
173   }
174
175   /**
176    * @see org.continuent.sequoia.controller.loadbalancer.tasks.AbstractTask#getTransactionId()
177    */

178   public long getTransactionId()
179   {
180     return tm.getTransactionId();
181   }
182
183   /**
184    * @see org.continuent.sequoia.controller.loadbalancer.tasks.AbstractTask#isAutoCommit()
185    */

186   public boolean isAutoCommit()
187   {
188     return false;
189   }
190
191   /**
192    * @see java.lang.Object#equals(java.lang.Object)
193    */

194   public boolean equals(Object JavaDoc other)
195   {
196     if ((other == null) || !(other instanceof CommitTask))
197       return false;
198
199     CommitTask commit = (CommitTask) other;
200     return this.getTransactionId() == commit.getTransactionId();
201   }
202
203   /**
204    * @see java.lang.Object#hashCode()
205    */

206   public int hashCode()
207   {
208     return (int) this.getTransactionId();
209   }
210
211   /**
212    * @see java.lang.Object#toString()
213    */

214   public String JavaDoc toString()
215   {
216     return "CommitTask (" + tm.getTransactionId() + ")";
217   }
218 }
Popular Tags