KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > continuent > sequoia > controller > requestmanager > TransactionMetaData


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): Peter Royal.
22  */

23
24 package org.continuent.sequoia.controller.requestmanager;
25
26 import java.io.Serializable JavaDoc;
27 import java.util.HashMap JavaDoc;
28 import java.util.LinkedList JavaDoc;
29 import java.util.List JavaDoc;
30 import java.util.Map JavaDoc;
31
32 import org.continuent.sequoia.controller.backend.DatabaseBackend;
33 import org.continuent.sequoia.controller.locks.TransactionLogicalLock;
34
35 /**
36  * This class carry transaction metadata including the transaction state.
37  * <p>
38  * Metadata include a transaction id, a login and a timeout. The state is used
39  * to track if a transaction is read-only or read-write and what kind of write
40  * accesses have been made in the transaction in case invalidations are needed
41  * at commit or rollback time.
42  * <p>
43  * The type is made serializable only for unlog messages that have to be sent
44  * between controllers.
45  *
46  * @author <a HREF="mailto:emmanuel.cecchet@emicnetworks.com">Emmanuel Cecchet</a>
47  * @version 2.2
48  */

49 public class TransactionMetaData implements Serializable JavaDoc
50 {
51   private static final long serialVersionUID = -5251053474824677394L;
52
53   private final long transactionId;
54   private long timeout;
55   private String JavaDoc login;
56   private final boolean isPersistentConnection;
57   private final long persistentConnectionId;
58   /** Recovery log id of the last commit/rollback operation on this transaction */
59   private transient long logId;
60   /** Map of Lists of locksMap taken by this task, indexed by backend */
61   private transient Map JavaDoc locksMap = new HashMap JavaDoc();
62   private transient boolean altersAggregateList = false;
63   private transient boolean altersDatabaseCatalog = false;
64   private transient boolean altersDatabaseSchema = false;
65   private transient boolean altersMetadataCache = false;
66   private transient boolean altersQueryResultCache = false;
67   private transient boolean altersSomething = false;
68   private transient boolean altersStoredProcedureList = false;
69   private transient boolean altersUserDefinedTypes = false;
70   private transient boolean altersUsers = false;
71   private transient boolean isReadOnly = true;
72
73   /**
74    * Creates a new <code>TransactionMetaData</code>.
75    *
76    * @param transactionId the transaction identifier.
77    * @param timeout the transaction timeout in seconds.
78    * @param login the user login.
79    * @param isPersistentConnection true if the transaction is started on a
80    * persistent connection
81    * @param persistentConnectionId persistent connection id if the transaction
82    * must be started on a persistent connection
83    */

84   public TransactionMetaData(long transactionId, long timeout, String JavaDoc login,
85       boolean isPersistentConnection, long persistentConnectionId)
86   {
87     this.transactionId = transactionId;
88     this.timeout = timeout;
89     this.login = login;
90     this.isPersistentConnection = isPersistentConnection;
91     this.persistentConnectionId = persistentConnectionId;
92   }
93
94   /**
95    * Add a lock to the list of locks acquired by the given backend on this
96    * transaction
97    *
98    * @param backend backend acquiring the lock
99    * @param lock the lock that has been acquired
100    */

101   public synchronized void addAcquiredLock(DatabaseBackend backend,
102       TransactionLogicalLock lock)
103   {
104     LinkedList JavaDoc acquiredLocks = (LinkedList JavaDoc) locksMap.get(backend);
105     if (acquiredLocks == null)
106     {
107       acquiredLocks = new LinkedList JavaDoc();
108       locksMap.put(backend, acquiredLocks);
109     }
110     acquiredLocks.add(lock);
111   }
112
113   /**
114    * Add locks to the list of locks acquired by the given backend on this
115    * transaction
116    *
117    * @param backend backend acquiring the locks
118    * @param locks the locks that have been acquired
119    */

120   public synchronized void addAcquiredLocks(DatabaseBackend backend, List JavaDoc locks)
121   {
122     LinkedList JavaDoc acquiredLocks = (LinkedList JavaDoc) locksMap.get(backend);
123     if (acquiredLocks == null)
124     {
125       acquiredLocks = new LinkedList JavaDoc();
126       locksMap.put(backend, acquiredLocks);
127     }
128     acquiredLocks.addAll(locks);
129   }
130
131   /**
132    * Returns the altersAggregateList value.
133    *
134    * @return Returns the altersAggregateList.
135    */

136   public final boolean altersAggregateList()
137   {
138     return altersAggregateList;
139   }
140
141   /**
142    * Returns the altersDatabaseCatalog value.
143    *
144    * @return Returns the altersDatabaseCatalog.
145    */

146   public final boolean altersDatabaseCatalog()
147   {
148     return altersDatabaseCatalog;
149   }
150
151   /**
152    * Returns the altersDatabaseSchema value.
153    *
154    * @return Returns the altersDatabaseSchema.
155    */

156   public final boolean altersDatabaseSchema()
157   {
158     return altersDatabaseSchema;
159   }
160
161   /**
162    * Returns the altersMetadataCache value.
163    *
164    * @return Returns the altersMetadataCache.
165    */

166   public final boolean altersMetadataCache()
167   {
168     return altersMetadataCache;
169   }
170
171   /**
172    * Returns the altersQueryResultCache value.
173    *
174    * @return Returns the altersQueryResultCache.
175    */

176   public final boolean altersQueryResultCache()
177   {
178     return altersQueryResultCache;
179   }
180
181   /**
182    * Returns the altersSomething value.
183    *
184    * @return Returns the altersSomething.
185    */

186   public final boolean altersSomething()
187   {
188     return altersSomething;
189   }
190
191   /**
192    * Returns the altersStoredProcedureList value.
193    *
194    * @return Returns the altersStoredProcedureList.
195    */

196   public final boolean altersStoredProcedureList()
197   {
198     return altersStoredProcedureList;
199   }
200
201   /**
202    * Returns the altersUserDefinedTypes value.
203    *
204    * @return Returns the altersUserDefinedTypes.
205    */

206   public final boolean altersUserDefinedTypes()
207   {
208     return altersUserDefinedTypes;
209   }
210
211   /**
212    * Returns the altersUsers value.
213    *
214    * @return Returns the altersUsers.
215    */

216   public final boolean altersUsers()
217   {
218     return altersUsers;
219   }
220
221   /**
222    * Return the list of locks acquired by the given backend for this transaction
223    * or null if no lock has been acquired so far.
224    *
225    * @param backend backend for which we want to retrieve to set of locks
226    * @return the list of acquired locks for the given backend
227    */

228   public List JavaDoc getAcquiredLocks(DatabaseBackend backend)
229   {
230     return (List JavaDoc) locksMap.get(backend);
231   }
232
233   /**
234    * Returns the logId value.
235    *
236    * @return Returns the logId.
237    */

238   public final long getLogId()
239   {
240     return logId;
241   }
242
243   /**
244    * Sets the logId value.
245    *
246    * @param logId The logId to set.
247    */

248   public final void setLogId(long logId)
249   {
250     this.logId = logId;
251   }
252
253   /**
254    * Returns the login.
255    *
256    * @return String
257    */

258   public String JavaDoc getLogin()
259   {
260     return login;
261   }
262
263   /**
264    * Returns the persistent connection id (only meaningful is
265    * isPersistentConnection is true).
266    *
267    * @return Returns the persistent connection id.
268    * @see #isPersistentConnection()
269    */

270   public final long getPersistentConnectionId()
271   {
272     return persistentConnectionId;
273   }
274
275   /**
276    * Returns the timeout.
277    *
278    * @return long
279    */

280   public long getTimeout()
281   {
282     return timeout;
283   }
284
285   /**
286    * Returns the transactionId.
287    *
288    * @return int
289    */

290   public long getTransactionId()
291   {
292     return transactionId;
293   }
294
295   /**
296    * Returns true if at least one backend is holding locks for this
297    * transactions.
298    *
299    * @return true if a backend has locks for the transactions
300    */

301   public boolean isLockedByBackends()
302   {
303     return !locksMap.isEmpty();
304   }
305
306   /**
307    * Returns true if the transaction executes on a persistent connection
308    * (retrieve the value using getPersistentConnectionId).
309    *
310    * @return Returns true if transaction uses a persistent connection.
311    * @see #getPersistentConnectionId()
312    */

313   public final boolean isPersistentConnection()
314   {
315     return isPersistentConnection;
316   }
317
318   /**
319    * Returns true if the transaction is read-only (true by default).
320    *
321    * @return Returns the isReadOnly.
322    */

323   public final boolean isReadOnly()
324   {
325     return isReadOnly;
326   }
327
328   /**
329    * Remove and return the lock list belonging to a backend.
330    *
331    * @param backend backend which locks should be removed
332    * @return the lock list or null if no lock were found for this backend
333    */

334   public List JavaDoc removeBackendLocks(DatabaseBackend backend)
335   {
336     return (List JavaDoc) locksMap.remove(backend);
337   }
338
339   /**
340    * Sets the altersAggregateList value.
341    *
342    * @param altersAggregateList The altersAggregateList to set.
343    */

344   public final void setAltersAggregateList(boolean altersAggregateList)
345   {
346     this.altersAggregateList = altersAggregateList;
347   }
348
349   /**
350    * Sets the altersDatabaseCatalog value.
351    *
352    * @param altersDatabaseCatalog The altersDatabaseCatalog to set.
353    */

354   public final void setAltersDatabaseCatalog(boolean altersDatabaseCatalog)
355   {
356     this.altersDatabaseCatalog = altersDatabaseCatalog;
357   }
358
359   /**
360    * Sets the altersDatabaseSchema value.
361    *
362    * @param altersDatabaseSchema The altersDatabaseSchema to set.
363    */

364   public final void setAltersDatabaseSchema(boolean altersDatabaseSchema)
365   {
366     this.altersDatabaseSchema = altersDatabaseSchema;
367   }
368
369   /**
370    * Sets the altersMetadataCache value.
371    *
372    * @param altersMetadataCache The altersMetadataCache to set.
373    */

374   public final void setAltersMetadataCache(boolean altersMetadataCache)
375   {
376     this.altersMetadataCache = altersMetadataCache;
377   }
378
379   /**
380    * Sets the altersQueryResultCache value.
381    *
382    * @param altersQueryResultCache The altersQueryResultCache to set.
383    */

384   public final void setAltersQueryResultCache(boolean altersQueryResultCache)
385   {
386     this.altersQueryResultCache = altersQueryResultCache;
387   }
388
389   /**
390    * Sets the altersSomething value.
391    *
392    * @param altersSomething The altersSomething to set.
393    */

394   public final void setAltersSomething(boolean altersSomething)
395   {
396     this.altersSomething = altersSomething;
397   }
398
399   /**
400    * Sets the altersStoredProcedureList value.
401    *
402    * @param altersStoredProcedureList The altersStoredProcedureList to set.
403    */

404   public final void setAltersStoredProcedureList(
405       boolean altersStoredProcedureList)
406   {
407     this.altersStoredProcedureList = altersStoredProcedureList;
408   }
409
410   /**
411    * Sets the altersUserDefinedTypes value.
412    *
413    * @param altersUserDefinedTypes The altersUserDefinedTypes to set.
414    */

415   public final void setAltersUserDefinedTypes(boolean altersUserDefinedTypes)
416   {
417     this.altersUserDefinedTypes = altersUserDefinedTypes;
418   }
419
420   /**
421    * Sets the altersUsers value.
422    *
423    * @param altersUsers The altersUsers to set.
424    */

425   public final void setAltersUsers(boolean altersUsers)
426   {
427     this.altersUsers = altersUsers;
428   }
429
430   /**
431    * Sets the isReadOnly value.
432    *
433    * @param isReadOnly The isReadOnly to set.
434    */

435   public final void setReadOnly(boolean isReadOnly)
436   {
437     this.isReadOnly = isReadOnly;
438   }
439
440   /**
441    * Sets the login.
442    *
443    * @param login the login to set.
444    */

445   public void setLogin(String JavaDoc login)
446   {
447     this.login = login;
448   }
449
450   /**
451    * Sets the timeout.
452    *
453    * @param timeout the timeout to set.
454    */

455   public void setTimeout(long timeout)
456   {
457     this.timeout = timeout;
458   }
459
460   /**
461    * @see java.lang.Object#equals(java.lang.Object)
462    */

463   public boolean equals(Object JavaDoc obj)
464   {
465     if (obj instanceof TransactionMetaData)
466     {
467       TransactionMetaData tm = (TransactionMetaData) obj;
468       return tm.getTransactionId() == transactionId;
469     }
470     return false;
471   }
472
473   /**
474    * @see java.lang.Object#hashCode()
475    */

476   public int hashCode()
477   {
478     return (int) transactionId;
479   }
480 }
481
Popular Tags