KickJava   Java API By Example, From Geeks To Geeks.

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


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

163   public String JavaDoc toString()
164   {
165     return "RollbackTask (" + transactionId + ")";
166   }
167 }
Popular Tags