KickJava   Java API By Example, From Geeks To Geeks.

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


1 /**
2  * C-JDBC: Clustered JDBC.
3  * Copyright (C) 2002-2005 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): Jean-Bernard van Zuylen.
22  * Contributor(s): ______________________.
23  */

24
25 package org.objectweb.cjdbc.controller.loadbalancer.tasks;
26
27 import java.sql.Connection JavaDoc;
28 import java.sql.SQLException JavaDoc;
29 import java.sql.Savepoint JavaDoc;
30
31 import org.objectweb.cjdbc.controller.backend.DatabaseBackend;
32 import org.objectweb.cjdbc.controller.connection.AbstractConnectionManager;
33 import org.objectweb.cjdbc.controller.loadbalancer.BackendWorkerThread;
34
35 /**
36  * This class defines a RollbackToSavepointTask
37  *
38  * @author <a HREF="mailto:jbvanzuylen@transwide.com">Jean-Bernard van Zuylen
39  * </a>
40  * @version 1.0
41  */

42 public class RollbackToSavepointTask extends AbstractTask
43 {
44   /** Login used by the connection. */
45   private String JavaDoc login;
46
47   /** Unique transaction identifier. */
48   private long transactionId;
49
50   /** Request timeout in milliseconds. */
51   private long timeout;
52
53   /** Name of the savepoint. */
54   private String JavaDoc savepointName;
55   
56   /**
57    * Creates a new <code>RollbackToSavepointTask</code> object
58    *
59    * @param nbToComplete number of threads that must succeed before returning
60    * @param totalNb total number of threads
61    * @param timeout request timeout in ms
62    * @param login the login used by the connection
63    * @param transactionId a unique transaction identifier
64    * @param savepointName the name of the savepoint
65    */

66   public RollbackToSavepointTask(int nbToComplete, int totalNb, long timeout,
67       String JavaDoc login, long transactionId, String JavaDoc savepointName)
68   {
69     super(nbToComplete, totalNb);
70     this.login = login;
71     this.transactionId = transactionId;
72     this.timeout = timeout;
73     this.savepointName = savepointName;
74   }
75   
76   /**
77    * @see org.objectweb.cjdbc.controller.loadbalancer.tasks.AbstractTask#executeTask(org.objectweb.cjdbc.controller.loadbalancer.BackendWorkerThread)
78    */

79   public void executeTask(BackendWorkerThread backendThread)
80       throws SQLException JavaDoc
81   {
82     DatabaseBackend db = backendThread.getBackend();
83     Long JavaDoc lTid = new Long JavaDoc(transactionId);
84
85     AbstractConnectionManager cm = db.getConnectionManager(login);
86     if (cm == null)
87     {
88       SQLException JavaDoc se = new SQLException JavaDoc(
89           "No Connection Manager for Virtual Login:" + login);
90       try
91       {
92         notifyFailure(backendThread, 1, se);
93       }
94       catch (SQLException JavaDoc ignore)
95       {
96
97       }
98       throw se;
99     }
100
101     Connection JavaDoc c = cm.retrieveConnection(transactionId);
102
103     // Sanity check
104
if (c == null)
105     { // Bad connection
106
db.stopTransaction(lTid);
107       SQLException JavaDoc se = new SQLException JavaDoc(
108           "Unable to retrieve connection for transaction " + transactionId);
109       try
110       { // All backends failed, just ignore
111
if (!notifyFailure(backendThread, timeout, 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.kill();
120       String JavaDoc msg = "Failed to rollback transaction " + transactionId
121           + " to savepoint " + savepointName + " on backend " + db.getName()
122           + " but " + getSuccess() + " succeeded (" + se + ")";
123       backendThread.getLogger().error(msg);
124       throw new SQLException JavaDoc(msg);
125     }
126
127     // Execute Query
128
boolean hasSavepoint = false;
129     try
130     {
131       hasSavepoint = db.hasSavepointForTransaction(lTid, savepointName);
132       if (hasSavepoint)
133       {
134         Savepoint JavaDoc savepoint = db.getSavepoint(lTid, savepointName);
135         c.rollback(savepoint);
136       }
137       else
138       {
139         c.rollback();
140         c.setAutoCommit(true);
141       }
142       // Flag the schema as dirty in case the transaction contained a DDL
143
backendThread.getBackend().setSchemaIsDirty(true);
144     }
145     catch (Exception JavaDoc e)
146     {
147       try
148       {
149         if (!notifyFailure(backendThread, timeout, new SQLException JavaDoc(e
150             .getMessage())))
151           return;
152       }
153       catch (SQLException JavaDoc ignore)
154       {
155       }
156       // Disable this backend (it is no more in sync) by killing the backend
157
// thread
158
backendThread.kill();
159       String JavaDoc msg = "Failed to rollback transaction " + transactionId
160           + " to savepoint " + savepointName + " on backend " + db.getName()
161           + " but " + getSuccess() + " succeeded (" + e + ")";
162       backendThread.getLogger().error(msg);
163       throw new SQLException JavaDoc(msg);
164     }
165     finally
166     {
167       if (! hasSavepoint)
168       {
169         cm.releaseConnection(transactionId);
170         db.stopTransaction(lTid);
171       }
172     }
173     notifySuccess();
174   }
175
176   /**
177    * @see java.lang.Object#toString()
178    */

179   public String JavaDoc toString()
180   {
181     return "RollbackToSavepointTask for transaction " + transactionId + " ("
182         + savepointName + ")";
183   }
184 }
185
Popular Tags