KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > continuent > sequoia > controller > scheduler > singledb > SingleDBPessimisticTransactionLevelScheduler


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.singledb;
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 a SingleDB. Each
41  * write takes a lock on the whole database. All following writes are blocked
42  * until the transaction of the first write completes.
43  *
44  * @author <a HREF="mailto:Emmanuel.Cecchet@inria.fr">Emmanuel Cecchet </a>
45  * @version 1.0
46  */

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

61   TransactionExclusiveLock lock = new TransactionExclusiveLock();
62
63   //
64
// Constructor
65
//
66

67   /**
68    * Creates a new Pessimistic Transaction Level Scheduler
69    */

70   public SingleDBPessimisticTransactionLevelScheduler()
71   {
72     super(RAIDbLevels.SingleDB, ParsingGranularities.NO_PARSING);
73   }
74
75   //
76
// Request Handling
77
//
78

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

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

93   public final void readCompletedNotify(SelectRequest request)
94   {
95   }
96
97   /**
98    * Additionally to scheduling the request, this method replaces the SQL Date
99    * macros such as now() with the current date.
100    *
101    * @see org.continuent.sequoia.controller.scheduler.AbstractScheduler#scheduleWriteRequest(AbstractWriteRequest)
102    */

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

125   public final void notifyWriteCompleted(AbstractWriteRequest request)
126   {
127     // Requests outside transaction delimiters must release the lock
128
// as soon as they have executed
129
if (request.isAutoCommit())
130       releaseLock(request.getTransactionId());
131   }
132
133   /**
134    * @see org.continuent.sequoia.controller.scheduler.AbstractScheduler#scheduleNonSuspendedStoredProcedure(org.continuent.sequoia.controller.requests.StoredProcedure)
135    */

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

160   public final void notifyStoredProcedureCompleted(StoredProcedure proc)
161   {
162     // Requests outside transaction delimiters must release the lock
163
// as soon as they have executed
164
if (proc.isAutoCommit() && (!proc.isCreate()))
165       releaseLock(proc.getTransactionId());
166   }
167
168   //
169
// Transaction Management
170
//
171

172   /**
173    * @see org.continuent.sequoia.controller.scheduler.AbstractScheduler#commitTransaction(long)
174    */

175   protected final void commitTransaction(long transactionId)
176   {
177     releaseLock(transactionId);
178   }
179
180   /**
181    * @see org.continuent.sequoia.controller.scheduler.AbstractScheduler#rollbackTransaction(long)
182    */

183   protected final void rollbackTransaction(long transactionId)
184   {
185     releaseLock(transactionId);
186   }
187
188   /**
189    * @see org.continuent.sequoia.controller.scheduler.AbstractScheduler#rollbackTransaction(long,
190    * String)
191    */

192   protected final void rollbackTransaction(long transactionId,
193       String JavaDoc savepointName)
194   {
195   }
196
197   /**
198    * @see org.continuent.sequoia.controller.scheduler.AbstractScheduler#setSavepointTransaction(long,
199    * String)
200    */

201   protected final void setSavepointTransaction(long transactionId, String JavaDoc name)
202   {
203   }
204
205   /**
206    * @see org.continuent.sequoia.controller.scheduler.AbstractScheduler#releaseSavepointTransaction(long,
207    * String)
208    */

209   protected final void releaseSavepointTransaction(long transactionId,
210       String JavaDoc name)
211   {
212   }
213
214   /**
215    * Release the locks we may own on the schema.
216    *
217    * @param transactionId id of the transaction that releases the lock
218    */

219   private void releaseLock(long transactionId)
220   {
221     // Are we the lock owner ?
222
if (lock.isLocked())
223     {
224       if (lock.getLocker() == transactionId)
225         lock.release();
226
227       // Note that the following warnings could be safely ignored if the
228
// transaction
229
// commiting/rolllbacking (releasing the lock) has not done any
230
// conflicting write
231
else if (logger.isDebugEnabled())
232         logger.debug("Transaction " + transactionId
233             + " wants to release the lock held by transaction "
234             + lock.getLocker());
235     }
236     else if (logger.isDebugEnabled())
237       logger.warn("Transaction " + transactionId
238           + " tries to release a lock that has not been acquired.");
239   }
240
241   //
242
// Debug/Monitoring
243
//
244

245   /**
246    * @see org.continuent.sequoia.controller.scheduler.AbstractScheduler#getXmlImpl()
247    */

248   public String JavaDoc getXmlImpl()
249   {
250     return "<" + DatabasesXmlTags.ELT_SingleDBScheduler + " "
251         + DatabasesXmlTags.ATT_level + "=\""
252         + DatabasesXmlTags.VAL_pessimisticTransaction + "\"/>";
253   }
254 }
255
Popular Tags