KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2
3    Derby - Class org.apache.derby.diag.TransactionTable
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 import org.apache.derby.iapi.services.monitor.Monitor;
25 import org.apache.derby.iapi.services.sanity.SanityManager;
26
27 import org.apache.derby.iapi.sql.conn.LanguageConnectionContext;
28 import org.apache.derby.iapi.sql.conn.ConnectionUtil;
29 import org.apache.derby.iapi.store.access.AccessFactory;
30 import org.apache.derby.iapi.store.access.TransactionInfo;
31
32 import org.apache.derby.vti.VTITemplate;
33 import org.apache.derby.vti.VTICosting;
34 import org.apache.derby.vti.VTIEnvironment;
35
36 import org.apache.derby.iapi.sql.ResultColumnDescriptor;
37 import org.apache.derby.impl.jdbc.EmbedResultSetMetaData;
38
39 import org.apache.derby.iapi.reference.Limits;
40 import org.apache.derby.iapi.util.StringUtil;
41
42 import java.sql.ResultSetMetaData JavaDoc;
43 import java.sql.SQLException JavaDoc;
44 import java.sql.Types JavaDoc;
45
46 /**
47     
48
49     TransactionTable is a virtual table that shows all transactions
50     currently in the database.
51     
52     This virtual table can be invoked by calling it
53     directly
54     <PRE> select * from SYSCS_DIAG.TRANSACTION_TABLE </PRE>
55
56     <P>The TransactionTable virtual table takes a snap shot of the
57     transaction table while the system is in flux, so it is possible that some
58     transactions may be in transition state while the snap shot is taken.
59     We choose to do this rather then impose extraneous timing restrictions so
60     that the use of this tool will not alter the normal timing and flow of
61     execution in the application.
62
63     <P>The TransactionTable virtual table has the following columns:
64     <UL>
65     <LI>XID varchar(15) - not nullable. The transaction id, this can be joined
66     with the LockTable virtual table's XID.</LI>
67     <LI>GLOBAL_XID varchar(140) - nullable. The global transaction id, only
68     set if this transaction is a participant in a distributed transaction.</LI>
69     <LI>USERNAME varchar(128) - nullable. The user name, or APP by default.
70     May appear null if the transaction is started by Cloudscape.</LI>
71     <LI>TYPE varchar(30) - not nullable. UserTransaction or an internal
72     transaction spawned by Cloudscape.</LI>
73     <LI>STATUS varchar(8) - not nullable. IDLE or ACTIVE. A transaction is
74     IDLE only when it is first created or right after it commits. Any
75     transaction that holds or has held any resource in the database is ACTIVE.
76     Accessing the TransactionTable virtual table without using the class alias
77     will not activate the transaction.</LI>
78     <LI>FIRST_INSTANT varchar(20) - nullable. If null, this is a read only
79     transaction. If not null, this is the first log record instant written by
80     the transaction.</LI>
81     <LI>SQL_TEXT VARCHAR(32672) - nullable. if null, this transaction is
82     currently not being executed in the database. If not null, this is the SQL
83     statement currently being executed in the database.</LI>
84     </UL>
85 */

86 public class TransactionTable extends VTITemplate implements VTICosting {
87
88     private TransactionInfo[] transactionTable;
89     boolean initialized;
90     int currentRow;
91     private boolean wasNull;
92
93     /**
94         @see java.sql.ResultSet#getMetaData
95      */

96     public ResultSetMetaData getMetaData()
97     {
98         return metadata;
99     }
100
101     /**
102         @see java.sql.ResultSet#next
103         @exception SQLException if no transaction context can be found
104      */

105     public boolean next() throws SQLException JavaDoc
106     {
107         if (!initialized)
108         {
109             LanguageConnectionContext lcc = ConnectionUtil.getCurrentLCC();
110
111             AccessFactory af = lcc.getLanguageConnectionFactory().getAccessFactory();
112             transactionTable = af.getTransactionInfo();
113
114             initialized = true;
115             currentRow = -1;
116         }
117
118         if (transactionTable == null)
119             return false;
120
121         for (currentRow++;
122              currentRow < transactionTable.length;
123              currentRow++)
124         {
125             TransactionInfo info = transactionTable[currentRow];
126             if (info == null)
127                 continue; // transaction object in flux while the
128
// snap shot was taken, get another row
129
return true;
130         }
131
132         // currentRow >= transactionTable.length
133
transactionTable = null;
134         return false;
135     }
136
137     /**
138         @see java.sql.ResultSet#close
139      */

140     public void close()
141     {
142         transactionTable = null;
143     }
144
145     /**
146         All columns in TransactionTable VTI is of String type.
147         @see java.sql.ResultSet#getString
148      */

149     public String JavaDoc getString(int columnNumber)
150     {
151         TransactionInfo info = transactionTable[currentRow];
152         String JavaDoc str = null;
153
154         switch(columnNumber)
155         {
156         case 1:
157             str = info.getTransactionIdString(); break;
158
159         case 2:
160             str = info.getGlobalTransactionIdString(); break;
161
162         case 3:
163             str = info.getUsernameString(); break;
164
165         case 4:
166             str = info.getTransactionTypeString(); break;
167
168         case 5:
169             str = info.getTransactionStatusString(); break;
170
171         case 6:
172             str = info.getFirstLogInstantString(); break;
173
174         case 7:
175
176             str = info.getStatementTextString();
177             str = StringUtil.truncate(str, Limits.DB2_VARCHAR_MAXWIDTH);
178             break;
179
180         default:
181             str = null;
182         }
183
184         wasNull = (str == null);
185         return str;
186
187     }
188
189     /**
190         @see java.sql.ResultSet#wasNull
191      */

192     public boolean wasNull()
193     {
194         return wasNull;
195     }
196
197
198     /** VTI costing interface */
199     
200     /**
201         @see VTICosting#getEstimatedRowCount
202      */

203     public double getEstimatedRowCount(VTIEnvironment vtiEnvironment)
204     {
205         return VTICosting.defaultEstimatedRowCount;
206     }
207     
208     /**
209         @see VTICosting#getEstimatedCostPerInstantiation
210      */

211     public double getEstimatedCostPerInstantiation(VTIEnvironment vtiEnvironment)
212     {
213         return VTICosting.defaultEstimatedCost;
214     }
215
216     /**
217         @return false
218         @see VTICosting#supportsMultipleInstantiations
219      */

220     public boolean supportsMultipleInstantiations(VTIEnvironment vtiEnvironment)
221     {
222         return false;
223     }
224
225
226     /*
227     ** Metadata
228     */

229     private static final ResultColumnDescriptor[] columnInfo = {
230
231         EmbedResultSetMetaData.getResultColumnDescriptor("XID", Types.VARCHAR, false, 15),
232         EmbedResultSetMetaData.getResultColumnDescriptor("GLOBAL_XID", Types.VARCHAR, true, 140),
233         EmbedResultSetMetaData.getResultColumnDescriptor("USERNAME", Types.VARCHAR, true, 128),
234         EmbedResultSetMetaData.getResultColumnDescriptor("TYPE", Types.VARCHAR, false, 30),
235         EmbedResultSetMetaData.getResultColumnDescriptor("STATUS", Types.VARCHAR, false, 8),
236         EmbedResultSetMetaData.getResultColumnDescriptor("FIRST_INSTANT", Types.VARCHAR, true, 20),
237         EmbedResultSetMetaData.getResultColumnDescriptor("SQL_TEXT", Types.VARCHAR, true, Limits.DB2_VARCHAR_MAXWIDTH),
238     };
239     
240     private static final ResultSetMetaData metadata = new EmbedResultSetMetaData(columnInfo);
241 }
242
243
Popular Tags