KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > continuent > sequoia > controller > scheduler > raidb0 > RAIDb0PessimisticTransactionLevelScheduler


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) 2005 AmicoSoft, Inc. dba Emic Networks
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): Jean-Bernard van Zuylen.
22  */

23
24 package org.continuent.sequoia.controller.scheduler.raidb0;
25
26 import java.sql.SQLException JavaDoc;
27
28 import org.continuent.sequoia.common.exceptions.RollbackException;
29 import org.continuent.sequoia.common.xml.DatabasesXmlTags;
30 import org.continuent.sequoia.controller.core.ControllerConstants;
31 import org.continuent.sequoia.controller.requestmanager.RAIDbLevels;
32 import org.continuent.sequoia.controller.requests.AbstractWriteRequest;
33 import org.continuent.sequoia.controller.requests.ParsingGranularities;
34 import org.continuent.sequoia.controller.requests.SelectRequest;
35 import org.continuent.sequoia.controller.requests.StoredProcedure;
36 import org.continuent.sequoia.controller.scheduler.AbstractScheduler;
37 import org.continuent.sequoia.controller.scheduler.schema.TransactionExclusiveLock;
38
39 /**
40  * This scheduler provides transaction level scheduling for RAIDb-0 controllers.
41  * Each write takes a lock on the whole database. All following writes are
42  * blocked until the transaction of the first write completes.
43  *
44  * @author <a HREF="mailto:Emmanuel.Cecchet@inria.fr">Emmanuel Cecchet</a>
45  * @author <a HREF="mailto:jbvanzuylen@transwide.com">Jean-Bernard van Zuylen
46  * </a>
47  * @version 1.0
48  * @deprecated since Sequoia 2.2
49  */

50 public class RAIDb0PessimisticTransactionLevelScheduler
51     extends AbstractScheduler
52 {
53
54   //
55
// How the code is organized ?
56
//
57
// 1. Member variables
58
// 2. Constructor
59
// 3. Request handling
60
// 4. Transaction management
61
// 5. Debug/Monitoring
62
//
63

64   private TransactionExclusiveLock lock = new TransactionExclusiveLock();
65
66   //
67
// Constructor
68
//
69

70   /**
71    * Creates a new Pessimistic Transaction Level Scheduler
72    */

73   public RAIDb0PessimisticTransactionLevelScheduler()
74   {
75     super(RAIDbLevels.RAIDb0, ParsingGranularities.NO_PARSING);
76   }
77
78   //
79
// Request Handling
80
//
81

82   /**
83    * Additionally to scheduling the request, this method replaces the SQL Date
84    * macros such as now() with the current date.
85    *
86    * @see org.continuent.sequoia.controller.scheduler.AbstractScheduler#scheduleNonSuspendedReadRequest(SelectRequest)
87    */

88   public final void scheduleNonSuspendedReadRequest(SelectRequest request)
89       throws SQLException JavaDoc
90   {
91   }
92
93   /**
94    * @see org.continuent.sequoia.controller.scheduler.AbstractScheduler#readCompletedNotify(SelectRequest)
95    */

96   public final void readCompletedNotify(SelectRequest request)
97   {
98   }
99
100   /**
101    * Additionally to scheduling the request, this method replaces the SQL Date
102    * macros such as now() with the current date. Note that CREATE statements are
103    * not synchronized.
104    *
105    * @see org.continuent.sequoia.controller.scheduler.AbstractScheduler#scheduleWriteRequest(AbstractWriteRequest)
106    */

107   public void scheduleNonSuspendedWriteRequest(AbstractWriteRequest request)
108       throws SQLException JavaDoc
109   {
110     if (request.isCreate())
111     {
112       return;
113     }
114
115     if (lock.acquire(request))
116     {
117       if (logger.isDebugEnabled())
118         logger.debug("Request " + request.getId() + " scheduled for write ("
119             + getPendingWrites() + " pending writes)");
120     }
121     else
122     {
123       if (logger.isWarnEnabled())
124         logger.warn("Request " + request.getId() + " timed out ("
125             + request.getTimeout() + " s)");
126       throw new SQLException JavaDoc("Timeout (" + request.getTimeout()
127           + ") for request: "
128           + request.getSqlShortForm(ControllerConstants.SQL_SHORT_FORM_LENGTH));
129     }
130   }
131
132   /**
133    * @see org.continuent.sequoia.controller.scheduler.AbstractScheduler#notifyWriteCompleted(AbstractWriteRequest)
134    */

135   public final synchronized void notifyWriteCompleted(
136       AbstractWriteRequest request)
137   {
138     // Requests outside transaction delimiters must release the lock
139
// as soon as they have executed
140
if (request.isAutoCommit() && (!request.isCreate()))
141       releaseLock(request.getTransactionId());
142   }
143
144   /**
145    * @see org.continuent.sequoia.controller.scheduler.AbstractScheduler#scheduleNonSuspendedStoredProcedure(org.continuent.sequoia.controller.requests.StoredProcedure)
146    */

147   public final void scheduleNonSuspendedStoredProcedure(StoredProcedure proc)
148       throws SQLException JavaDoc, RollbackException
149   {
150     if (lock.acquire(proc))
151     {
152       if (logger.isDebugEnabled())
153         logger.debug("Stored procedure " + proc.getId()
154             + " scheduled for write (" + getPendingWrites()
155             + " pending writes)");
156     }
157     else
158     {
159       if (logger.isWarnEnabled())
160         logger.warn("Stored procedure " + proc.getId() + " timed out ("
161             + proc.getTimeout() + " s)");
162       throw new SQLException JavaDoc("Timeout (" + proc.getTimeout()
163           + ") for request: "
164           + proc.getSqlShortForm(ControllerConstants.SQL_SHORT_FORM_LENGTH));
165     }
166   }
167
168   /**
169    * @see org.continuent.sequoia.controller.scheduler.AbstractScheduler#notifyStoredProcedureCompleted(org.continuent.sequoia.controller.requests.StoredProcedure)
170    */

171   public final void notifyStoredProcedureCompleted(StoredProcedure proc)
172   {
173     // Requests outside transaction delimiters must release the lock
174
// as soon as they have executed
175
if (proc.isAutoCommit() && (!proc.isCreate()))
176       releaseLock(proc.getTransactionId());
177   }
178
179   //
180
// Transaction Management
181
//
182

183   /**
184    * @see org.continuent.sequoia.controller.scheduler.AbstractScheduler#commitTransaction(long)
185    */

186   protected final void commitTransaction(long transactionId)
187   {
188     releaseLock(transactionId);
189   }
190
191   /**
192    * @see org.continuent.sequoia.controller.scheduler.AbstractScheduler#rollbackTransaction(long)
193    */

194   protected final void rollbackTransaction(long transactionId)
195   {
196     releaseLock(transactionId);
197   }
198
199   /**
200    * @see org.continuent.sequoia.controller.scheduler.AbstractScheduler#rollbackTransaction(long,
201    * String)
202    */

203   protected final void rollbackTransaction(long transactionId,
204       String JavaDoc savepointName)
205   {
206   }
207
208   /**
209    * @see org.continuent.sequoia.controller.scheduler.AbstractScheduler#setSavepointTransaction(long,
210    * String)
211    */

212   protected final void setSavepointTransaction(long transactionId, String JavaDoc name)
213   {
214   }
215
216   /**
217    * @see org.continuent.sequoia.controller.scheduler.AbstractScheduler#releaseSavepointTransaction(long,
218    * String)
219    */

220   protected final void releaseSavepointTransaction(long transactionId,
221       String JavaDoc name)
222   {
223   }
224
225   /**
226    * Release the locks we may own on the schema.
227    *
228    * @param transactionId id of the transaction that releases the lock
229    */

230   private void releaseLock(long transactionId)
231   {
232     // Are we the lock owner ?
233
if (lock.isLocked())
234     {
235       if (lock.getLocker() == transactionId)
236         lock.release();
237
238       // Note that the following warnings could be safely ignored if the
239
// transaction
240
// commiting/rolllbacking (releasing the lock) has not done any
241
// conflicting write
242
else if (logger.isDebugEnabled())
243         logger.debug("Transaction " + transactionId
244             + " wants to release the lock held by transaction "
245             + lock.getLocker());
246     }
247     else if (logger.isDebugEnabled())
248       logger.warn("Transaction " + transactionId
249           + " tries to release a lock that has not been acquired.");
250   }
251
252   //
253
// Debug/Monitoring
254
//
255

256   /**
257    * @see org.continuent.sequoia.controller.scheduler.AbstractScheduler#getXmlImpl()
258    */

259   public String JavaDoc getXmlImpl()
260   {
261     StringBuffer JavaDoc info = new StringBuffer JavaDoc();
262     info.append("<" + DatabasesXmlTags.ELT_RAIDb0Scheduler + " "
263         + DatabasesXmlTags.ATT_level + "=\""
264         + DatabasesXmlTags.VAL_pessimisticTransaction + "\"/>");
265     return info.toString();
266   }
267 }
268
Popular Tags