KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > continuent > sequoia > controller > scheduler > raidb1 > RAIDb1PessimisticTransactionLevelScheduler


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.raidb1;
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-1 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 RAIDb1PessimisticTransactionLevelScheduler
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   TransactionExclusiveLock lock = new TransactionExclusiveLock();
65
66   //
67
// Constructor
68
//
69

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

73   public RAIDb1PessimisticTransactionLevelScheduler()
74   {
75     super(RAIDbLevels.RAIDb1, 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    * Note that CREATE statements are not synchronized.
102    *
103    * @see org.continuent.sequoia.controller.scheduler.AbstractScheduler#scheduleNonSuspendedWriteRequest(AbstractWriteRequest)
104    */

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

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

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

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

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

184   protected final void commitTransaction(long transactionId)
185   {
186     if (lock.isLocked())
187       releaseLock(transactionId);
188     // else this was an empty or read-only transaction
189
}
190
191   /**
192    * @see org.continuent.sequoia.controller.scheduler.AbstractScheduler#rollbackTransaction(long)
193    */

194   protected final void rollbackTransaction(long transactionId)
195   {
196     if (lock.isLocked())
197       releaseLock(transactionId);
198     // else this was an empty or read-only transaction
199
}
200
201   /**
202    * @see org.continuent.sequoia.controller.scheduler.AbstractScheduler#rollbackTransaction(long,
203    * String)
204    */

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

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

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

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

259   public String JavaDoc getXmlImpl()
260   {
261     return "<" + DatabasesXmlTags.ELT_RAIDb1Scheduler + " "
262         + DatabasesXmlTags.ATT_level + "=\""
263         + DatabasesXmlTags.VAL_pessimisticTransaction + "\"/>";
264   }
265 }
266
Popular Tags