KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > cjdbc > controller > loadbalancer > tasks > CommitTask


1 /**
2  * C-JDBC: Clustered JDBC.
3  * Copyright (C) 2002-2004 French National Institute For Research In Computer
4  * Science And Control (INRIA).
5  * Contact: c-jdbc@objectweb.org
6  *
7  * This library is free software; you can redistribute it and/or modify it
8  * under the terms of the GNU Lesser General Public License as published by the
9  * Free Software Foundation; either version 2.1 of the License, or any later
10  * version.
11  *
12  * This library is distributed in the hope that it will be useful, but WITHOUT
13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
15  * for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public License
18  * along with this library; if not, write to the Free Software Foundation,
19  * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
20  *
21  * Initial developer(s): Emmanuel Cecchet.
22  * Contributor(s): Julie Marguerite.
23  */

24
25 package org.objectweb.cjdbc.controller.loadbalancer.tasks;
26
27 import java.sql.Connection JavaDoc;
28 import java.sql.SQLException JavaDoc;
29
30 import org.objectweb.cjdbc.controller.backend.DatabaseBackend;
31 import org.objectweb.cjdbc.controller.connection.AbstractConnectionManager;
32 import org.objectweb.cjdbc.controller.loadbalancer.BackendWorkerThread;
33
34 /**
35  * Task to commit a transaction.
36  *
37  * @author <a HREF="mailto:Emmanuel.Cecchet@inria.fr">Emmanuel Cecchet </a>
38  * @author <a HREF="mailto:Julie.Marguerite@inria.fr">Julie Marguerite </a>
39  * @version 1.0
40  */

41 public class CommitTask extends AbstractTask
42 {
43   /** Login used by the connection. */
44   private String JavaDoc login;
45
46   /** Unique transaction identifier. */
47   private long transactionId;
48
49   /** Request timeout in milliseconds. */
50   private long timeout;
51
52   /**
53    * Commits a transaction given a login and a transaction id.
54    *
55    * @param nbToComplete number of threads that must succeed before returning
56    * @param totalNb total number of threads
57    * @param timeout request timeout in ms
58    * @param login the login used by the connection
59    * @param transactionId a unique transaction identifier
60    */

61   public CommitTask(int nbToComplete, int totalNb, long timeout, String JavaDoc login,
62       long transactionId)
63   {
64     super(nbToComplete, totalNb);
65     this.login = login;
66     this.transactionId = transactionId;
67     this.timeout = timeout;
68   }
69
70   /**
71    * Commits a transaction with the given backend thread.
72    *
73    * @param backendThread the backend thread that will execute the task
74    * @throws SQLException if an error occurs
75    */

76   public void executeTask(BackendWorkerThread backendThread) throws SQLException JavaDoc
77   {
78     DatabaseBackend db = backendThread.getBackend();
79     Long JavaDoc lTid = new Long JavaDoc(transactionId);
80
81     AbstractConnectionManager cm = db.getConnectionManager(login);
82     if (cm == null)
83     {
84       SQLException JavaDoc se = new SQLException JavaDoc(
85           "No Connection Manager for Virtual Login:" + login);
86       try
87       {
88         notifyFailure(backendThread, 1, se);
89       }
90       catch (SQLException JavaDoc ignore)
91       {
92
93       }
94       throw se;
95     }
96     Connection JavaDoc c = cm.retrieveConnection(transactionId);
97
98     // Sanity check
99
if (c == null)
100     { // Bad connection
101
db.stopTransaction(lTid);
102       SQLException JavaDoc se = new SQLException JavaDoc(
103           "Unable to retrieve connection for transaction " + transactionId);
104
105       try
106       { // All backends failed, just ignore
107
if (!notifyFailure(backendThread, timeout, se))
108           return;
109       }
110       catch (SQLException JavaDoc ignore)
111       {
112       }
113       // Disable this backend (it is no more in sync) by killing the backend
114
// thread
115
backendThread.kill();
116       String JavaDoc msg = "Failed to commit transaction " + transactionId
117           + " on backend " + db.getName() + " but " + getSuccess()
118           + " succeeded (" + se + ")";
119       backendThread.getLogger().error(msg);
120       throw new SQLException JavaDoc(msg);
121     }
122
123     // Execute Query
124
try
125     {
126       c.commit();
127       c.setAutoCommit(true);
128     }
129     catch (Exception JavaDoc e)
130     {
131       try
132       {
133         if (!notifyFailure(backendThread, timeout, new SQLException JavaDoc(e
134             .getMessage())))
135           return;
136       }
137       catch (SQLException JavaDoc ignore)
138       {
139       }
140       // Disable this backend (it is no more in sync) by killing the backend
141
// thread
142
backendThread.kill();
143       String JavaDoc msg = "Failed to commit transaction " + transactionId
144           + " on backend " + db.getName() + " but " + getSuccess()
145           + " succeeded (" + e + ")";
146       backendThread.getLogger().error(msg);
147       throw new SQLException JavaDoc(msg);
148     }
149     finally
150     {
151       cm.releaseConnection(transactionId);
152       db.stopTransaction(lTid);
153     }
154     notifySuccess();
155   }
156
157   /**
158    * @see java.lang.Object#toString()
159    */

160   public String JavaDoc toString()
161   {
162     return "CommitTask (" + transactionId + ")";
163   }
164 }
Popular Tags