KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > derby > diag > LockTable


1 /*
2
3    Derby - Class org.apache.derby.diag.LockTable
4
5    Licensed to the Apache Software Foundation (ASF) under one or more
6    contributor license agreements. See the NOTICE file distributed with
7    this work for additional information regarding copyright ownership.
8    The ASF licenses this file to You under the Apache License, Version 2.0
9    (the "License"); you may not use this file except in compliance with
10    the License. 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  */

21
22 package org.apache.derby.diag;
23
24 // temp
25
import org.apache.derby.impl.services.locks.TableNameInfo;
26
27 import org.apache.derby.iapi.services.locks.LockFactory;
28 import org.apache.derby.iapi.services.locks.Latch;
29 import org.apache.derby.iapi.services.locks.Lockable;
30 import org.apache.derby.iapi.services.locks.VirtualLockTable;
31 import org.apache.derby.iapi.services.sanity.SanityManager;
32
33 import org.apache.derby.iapi.error.StandardException;
34 import org.apache.derby.iapi.sql.conn.LanguageConnectionContext;
35 import org.apache.derby.iapi.sql.conn.ConnectionUtil;
36 import org.apache.derby.iapi.sql.conn.LanguageConnectionFactory;
37 import org.apache.derby.iapi.store.access.TransactionController;
38 import org.apache.derby.iapi.error.PublicAPI;
39
40 import org.apache.derby.iapi.sql.ResultColumnDescriptor;
41 import org.apache.derby.impl.jdbc.EmbedResultSetMetaData;
42
43 import java.util.Hashtable JavaDoc;
44 import java.util.Enumeration JavaDoc;
45 import java.sql.ResultSetMetaData JavaDoc;
46 import java.sql.SQLException JavaDoc;
47 import java.sql.Types JavaDoc;
48 import org.apache.derby.vti.VTITemplate;
49 import org.apache.derby.vti.VTICosting;
50 import org.apache.derby.vti.VTIEnvironment;
51
52 /**
53     LockTable is a virtual table that shows all locks currently held in
54     the database.
55     
56     This virtual table can be invoked by calling it directly
57     <PRE> select * from SYSCS_DIAG.LOCK_TABLE </PRE>
58     
59     <P>The LockTable virtual table takes a snap shot of the lock table while
60     the system is in flux, so it is possible that some locks may be in
61     transition state while the snap shot is taken. We choose to do this rather
62     then impose extranous timing restrictions so that the use of this tool will
63     not alter the normal timing and flow of execution in the application.
64
65     <P>The LockTable virtual table has the following columns:
66     <UL><LI>XID varchar(15) - not nullable. The transaction id, this can be joined with the
67     TransactionTable virtual table's XID.</LI>
68     <LI>TYPE varchar(5) - nullable. The type of lock, ROW, TABLE, or LATCH</LI>
69     <LI>MODE varchar(4) - not nullable. The mode of the lock, "S", "U", "X", "IS", "IX".</LI>
70         <UL><LI>S is shared lock (N/A to Latch) </LI>
71             <LI>U is update lock (N/A to Latch) </LI>
72             <LI>X is exclusive lock </LI>
73             <LI>IS is intent shared lock (N/A to Latch or Row lock) </LI>
74             <LI>IX is intent exclusive lock (N/A to Latch or Row lock) </LI>
75         </UL>
76     <LI>TABLENAME varchar(128) - not nullable. The name of the base table the lock is for </LI>
77     <LI>LOCKNAME varchar(20) - not nullable. The name of the lock </LI>
78     <LI>STATE varchar(5) - nullable. GRANT or WAIT </LI>
79     <LI>TABLETYPE varchar(9) - not nullable. 'T' for user table, 'S' for system table </LI>
80     <LI>LOCKCOUNT varchar(5) - not nullable. Internal lock count.</LI>
81     <LI>INDEXNAME varchar(128) - normally null. If non-null, a lock is held on
82     the index, this can only happen if this is not a user transaction.</LI>
83     </UL>
84
85  */

86 public class LockTable extends VTITemplate implements VTICosting {
87
88     /** return only latches */
89     public static final int LATCH = VirtualLockTable.LATCH;
90
91     /** return only table and row locks */
92     public static final int TABLE_AND_ROWLOCK = VirtualLockTable.TABLE_AND_ROWLOCK;
93
94     /** return all locks and latches */
95     public static final int ALL = VirtualLockTable.ALL;
96
97     /*
98     ** private
99     */

100     private TransactionController tc;
101     private LanguageConnectionFactory lcf;
102     private Hashtable JavaDoc currentRow; // an entry in the lock table
103
private Enumeration JavaDoc lockTable;
104     private boolean wasNull;
105     private boolean initialized;
106     private final int flag;
107     private TableNameInfo tabInfo;
108
109     /**
110         The normal way of instantiating a LockTable, equivalent to
111         LockTable(org.apache.derby.diag.LockTable->TABLE_AND_ROWLOCK).
112         Only shows row and table lock and not latches. Latches are generally
113         held for very short duration and are not of interest to Cloudscape
114         users. Only under abnormal circumstances will one be interested in
115         looking at latches.
116      */

117     public LockTable()
118     {
119         flag = TABLE_AND_ROWLOCK;
120     }
121
122     /**
123         This call is intrusive and should only be used under the supervision of
124         technical support. Create an instance of the lock table which
125         has transient latches as well as locks.
126      */

127     public LockTable(int flag)
128     {
129         this.flag = flag;
130     }
131
132     /**
133         @see java.sql.ResultSet#getMetaData
134      */

135     public ResultSetMetaData getMetaData()
136     {
137         return metadata;
138     }
139
140     /**
141         @see java.sql.ResultSet#next
142         @exception SQLException if no transaction context can be found, or other
143         Cloudscape internal errors are encountered.
144      */

145     public boolean next() throws SQLException JavaDoc
146     {
147         try
148         {
149             if (!initialized)
150             {
151                 LanguageConnectionContext lcc = ConnectionUtil.getCurrentLCC();
152
153                 tc = lcc.getTransactionExecute();
154                 LanguageConnectionFactory lcf = lcc.getLanguageConnectionFactory();
155                 LockFactory lf = lcf.getAccessFactory().getLockFactory();
156                 lockTable = lf.makeVirtualLockTable();
157                 initialized = true;
158                 tabInfo = new TableNameInfo(lcc, true);
159             }
160
161             currentRow = null;
162             if (lockTable != null) {
163                 while (lockTable.hasMoreElements() && (currentRow == null)) {
164                     currentRow = dumpLock((Latch) lockTable.nextElement());
165                 }
166             }
167         }
168         catch (StandardException se)
169         {
170             throw PublicAPI.wrapStandardException(se);
171         }
172
173         return (currentRow != null);
174     }
175
176     /**
177         @see java.sql.ResultSet#close
178      */

179     public void close()
180     {
181         lockTable = null;
182     }
183
184     /**
185         All columns in TransactionTable VTI are of String type.
186         @see java.sql.ResultSet#getString
187      */

188     public String JavaDoc getString(int columnNumber)
189     {
190         String JavaDoc val = (String JavaDoc)currentRow.get(columnInfo[columnNumber-1].getName());
191         wasNull = (val == null);
192
193         return val;
194     }
195
196
197     /**
198         @see java.sql.ResultSet#wasNull
199      */

200     public boolean wasNull()
201     {
202         return wasNull;
203     }
204
205     /** VTI costing interface */
206     
207     /**
208         @see VTICosting#getEstimatedRowCount
209      */

210     public double getEstimatedRowCount(VTIEnvironment vtiEnvironment)
211     {
212         return VTICosting.defaultEstimatedRowCount;
213     }
214     
215     /**
216         @see VTICosting#getEstimatedCostPerInstantiation
217      */

218     public double getEstimatedCostPerInstantiation(VTIEnvironment vtiEnvironment)
219     {
220         return VTICosting.defaultEstimatedCost;
221     }
222     /**
223         @return false
224         @see VTICosting#supportsMultipleInstantiations
225      */

226     public boolean supportsMultipleInstantiations(VTIEnvironment vtiEnvironment)
227     {
228         return false;
229     }
230
231     /*
232     ** Private methods
233     */

234
235     /**
236         Convert the lock information into a hashtable.
237     */

238     private Hashtable JavaDoc dumpLock(
239     Latch lock)
240         throws StandardException
241     {
242         Hashtable JavaDoc attributes = new Hashtable JavaDoc(17);
243         Object JavaDoc lock_type = lock.getQualifier();
244
245
246         // 4 things we are interested in from the lockable:
247
// containerId, segmentId, pageNum, recId
248

249         Lockable lockable = lock.getLockable();
250
251         // see if this lockable object wants to participate
252
if (!lockable.lockAttributes(flag, attributes))
253             return null;
254
255         // if it does, the lockable object must have filled in the following
256
// fields
257
if (SanityManager.DEBUG)
258         {
259             SanityManager.ASSERT(attributes.get(VirtualLockTable.LOCKNAME) != null,
260              "lock table can only represent locks that have a LOCKNAME");
261
262             SanityManager.ASSERT(attributes.get(VirtualLockTable.LOCKTYPE) != null,
263              "lock table can only represent locks that have a LOCKTYPE");
264
265             if (attributes.get(VirtualLockTable.CONTAINERID) == null &&
266                 attributes.get(VirtualLockTable.CONGLOMID) == null)
267                 SanityManager.THROWASSERT(
268              "lock table can only represent locks that are associated with a container or conglomerate");
269         }
270
271         if (attributes.get(VirtualLockTable.LOCKNAME) == null ||
272             attributes.get(VirtualLockTable.LOCKTYPE) == null)
273             return null; // can't deal with this for now
274

275         // if the lock has zero count and is an instance of Lock then it
276
// is a lock that has just been released. Therefore do put it into
277
// the lock table. This occurs because the Lock object is the real
278
// live object in the LockTable. Thus when we copied the lock table
279
// it had a non-zero count, but since then it has been released
280
// (after we dropped the sync). Note if it is of type ActiveLock
281
// with zero count there is stil the chance it has been released.
282
// Less likely, but we still need to fix that at some time.
283
int lockCount = lock.getCount();
284         String JavaDoc state;
285         if (lockCount != 0)
286             state = "GRANT";
287         else if (!(lock instanceof org.apache.derby.impl.services.locks.ActiveLock))
288             return null;
289         else
290             state = "WAIT";
291
292         Long JavaDoc conglomId = (Long JavaDoc) attributes.get(VirtualLockTable.CONGLOMID);
293
294         if (conglomId == null)
295         {
296             // we need to figure this out
297
if (attributes.get(VirtualLockTable.CONTAINERID) == null)
298                 return null; // can't deal with this for now
299

300             Long JavaDoc value = (Long JavaDoc)attributes.get(VirtualLockTable.CONTAINERID);
301             conglomId = new Long JavaDoc(tc.findConglomid(value.longValue()));
302             attributes.put(VirtualLockTable.CONGLOMID, conglomId);
303         }
304
305         attributes.put(VirtualLockTable.LOCKOBJ, lock);
306         attributes.put(VirtualLockTable.XACTID, lock.getCompatabilitySpace().toString());
307         attributes.put(VirtualLockTable.LOCKMODE, lock_type.toString());
308
309         attributes.put(VirtualLockTable.LOCKCOUNT, Integer.toString(lockCount));
310
311         attributes.put(VirtualLockTable.STATE, state);
312
313         String JavaDoc tableName = tabInfo.getTableName(conglomId);
314
315         attributes.put(VirtualLockTable.TABLENAME, tableName);
316
317         String JavaDoc indexName = tabInfo.getIndexName(conglomId);
318
319         if (indexName != null)
320             attributes.put(VirtualLockTable.INDEXNAME, indexName);
321
322         String JavaDoc tableType = tabInfo.getTableType(conglomId);
323         attributes.put(VirtualLockTable.TABLETYPE, tableType);
324         return attributes;
325
326     }
327
328     /*
329     ** Metadata
330     */

331     private static final ResultColumnDescriptor[] columnInfo = {
332
333         EmbedResultSetMetaData.getResultColumnDescriptor(VirtualLockTable.XACTID, Types.VARCHAR, false, 15),
334         EmbedResultSetMetaData.getResultColumnDescriptor(VirtualLockTable.LOCKTYPE, Types.VARCHAR, true, 5),
335         EmbedResultSetMetaData.getResultColumnDescriptor(VirtualLockTable.LOCKMODE, Types.VARCHAR, false, 4),
336         EmbedResultSetMetaData.getResultColumnDescriptor(VirtualLockTable.TABLENAME, Types.VARCHAR, false, 128),
337         EmbedResultSetMetaData.getResultColumnDescriptor(VirtualLockTable.LOCKNAME, Types.VARCHAR, false, 20),
338         EmbedResultSetMetaData.getResultColumnDescriptor(VirtualLockTable.STATE, Types.VARCHAR, true, 5),
339         EmbedResultSetMetaData.getResultColumnDescriptor(VirtualLockTable.TABLETYPE, Types.VARCHAR, false, 9),
340         EmbedResultSetMetaData.getResultColumnDescriptor(VirtualLockTable.LOCKCOUNT, Types.VARCHAR, false, 5),
341         EmbedResultSetMetaData.getResultColumnDescriptor(VirtualLockTable.INDEXNAME, Types.VARCHAR, true, 128)
342     };
343     
344     private static final ResultSetMetaData metadata = new EmbedResultSetMetaData(columnInfo);
345 }
346
347
Popular Tags