KickJava   Java API By Example, From Geeks To Geeks.

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


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  * Task to set a savepoint to a transaction.
37  *
38  * @author <a HREF="mailto:jbvanzuylen@transwide.com">Jean-Bernard van Zuylen
39  * </a>
40  * @version 1.0
41  */

42 public class SavepointTask extends AbstractTask
43 {
44
45   /** Login used by the connection. */
46   private String JavaDoc login;
47
48   /** Unique transaction identifier. */
49   private long transactionId;
50
51   /** Request timeout in milliseconds. */
52   private long timeout;
53   
54   /** Name of the savepoint. */
55   private String JavaDoc savepointName;
56   
57   /** Savepoint that was created. */
58   private Savepoint JavaDoc result;
59
60
61   /**
62    * Sets a savepoint given a login, a transaction id and a name.
63    *
64    * @param nbToComplete number of threads that must succeed before returning
65    * @param totalNb total number of threads
66    * @param timeout request timeout in ms
67    * @param login the login used by the connection
68    * @param transactionId a unique transaction identifier
69    * @param savepointName a name for the savepoint
70    */

71   public SavepointTask(int nbToComplete, int totalNb, long timeout, String JavaDoc login,
72       long transactionId, String JavaDoc savepointName)
73   {
74     super(nbToComplete, totalNb);
75     this.login = login;
76     this.transactionId = transactionId;
77     this.timeout = timeout;
78     this.savepointName = savepointName;
79   }
80
81   /**
82    * @see org.objectweb.cjdbc.controller.loadbalancer.tasks.AbstractTask#executeTask(org.objectweb.cjdbc.controller.loadbalancer.BackendWorkerThread)
83    */

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

173   public Savepoint JavaDoc getResult()
174   {
175     return result;
176   }
177
178   /**
179    * @see java.lang.Object#toString()
180    */

181   public String JavaDoc toString()
182   {
183     return "SavepointTask for transaction " + transactionId + " ("
184         + savepointName + ")";
185   }
186 }
187
Popular Tags