KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > mckoi > database > LockHandle


1 /**
2  * com.mckoi.database.LockHandle 11 May 1998
3  *
4  * Mckoi SQL Database ( http://www.mckoi.com/database )
5  * Copyright (C) 2000, 2001, 2002 Diehl and Associates, Inc.
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * Version 2 as published by the Free Software Foundation.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License Version 2 for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * Version 2 along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19  *
20  * Change Log:
21  *
22  *
23  */

24
25 package com.mckoi.database;
26
27 import com.mckoi.debug.*;
28
29 /**
30  * This represents a handle for a series of locks that a query has over the
31  * tables in a database. It is returned by the 'LockingMechanism' object
32  * after the 'lockTables' method is used.
33  * <p>
34  * @author Tobias Downer
35  */

36
37 public final class LockHandle {
38
39   /**
40    * The array of Lock objects that are being used in this locking process.
41    */

42   private Lock[] lock_list;
43
44   /**
45    * A temporary index used during initialisation of object to add locks.
46    */

47   private int lock_index;
48
49   /**
50    * Set when the 'unlockAll' method is called for the first time.
51    */

52   private boolean unlocked;
53
54   /**
55    * The DebugLogger object that we log debug messages to.
56    */

57   private final DebugLogger debug;
58
59   /**
60    * The Constructor. Takes the number of locks that will be put into this
61    * handle.
62    */

63   LockHandle(int lock_count, DebugLogger logger) {
64     this.debug = logger;
65     lock_list = new Lock[lock_count];
66     lock_index = 0;
67     unlocked = false;
68   }
69
70   /**
71    * Adds a new lock to the locks for this handle.
72    * NOTE: This method does not need to be synchronized because synchronization
73    * is handled by the 'LockingMechanism.lockTables' method.
74    */

75   void addLock(Lock lock) {
76     lock_list[lock_index] = lock;
77     ++lock_index;
78   }
79
80   /**
81    * Unlocks all the locks in this handle. This removes the locks from its
82    * table queue.
83    * NOTE: This method does not need to be synchronized because synchronization
84    * is handled by the 'LockingMechanism.unlockTables' method.
85    */

86   void unlockAll() {
87     if (!unlocked) {
88       for (int i = lock_list.length - 1; i >= 0; --i) {
89         lock_list[i].release();
90       }
91       unlocked = true;
92     }
93   }
94
95   /**
96    * Blocks until access to the given DataTable object is safe. It blocks
97    * using either the read or read/write privs that it has been given.
98    * Note that this method is public and is a method that is intended to be
99    * used outside the locking mechanism.
100    * We also provide an 'access_type' field which is set to the type of access
101    * that is happening for this check. This is either Lock.READ or Lock.WRITE.
102    * NOTE: Any call to this method after the first call should be
103    * instantanious.
104    */

105   public void checkAccess(DataTable table, int access_type) {
106     for (int i = lock_list.length - 1; i >= 0; --i) {
107       Lock l = lock_list[i];
108       if (l.getTable() == table) {
109         l.checkAccess(access_type);
110         return;
111       }
112     }
113     throw new RuntimeException JavaDoc(
114         "The given DataTable was not found in the lock list for this handle");
115   }
116
117   /**
118    * On garbage collection, this will call 'unlockAll' just in case the
119    * program did not use the 'LockingMechanism.unlockTables' method in error.
120    * This should ensure the database does not deadlock. This method is a
121    * 'just in case' clause.
122    */

123   public void finalize() {
124     if (!unlocked) {
125       unlockAll();
126       debug.write(Lvl.ERROR, this, "Finalize released a table lock - " +
127         "This indicates that there is a serious error. Locks should " +
128         "only have a very short life span. The 'unlockAll' method should " +
129         "have been called before finalization. " + toString());
130     }
131   }
132
133   public String JavaDoc toString() {
134     StringBuffer JavaDoc str = new StringBuffer JavaDoc("LockHandle: ");
135     for (int i = 0; i < lock_list.length; ++i) {
136       str.append(lock_list[i].toString());
137     }
138     return new String JavaDoc(str);
139   }
140
141 }
142
Popular Tags