KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > mchange > v2 > c3p0 > stmt > ValueIdentityStatementCacheKey


1 /*
2  * Distributed as part of c3p0 v.0.9.1
3  *
4  * Copyright (C) 2005 Machinery For Change, Inc.
5  *
6  * Author: Steve Waldman <swaldman@mchange.com>
7  *
8  * This library is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU Lesser General Public License version 2.1, as
10  * published by the Free Software Foundation.
11  *
12  * This software is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public License
18  * along with this software; see the file LICENSE. If not, write to the
19  * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
20  * Boston, MA 02111-1307, USA.
21  */

22
23
24 package com.mchange.v2.c3p0.stmt;
25
26 import java.sql.Connection JavaDoc;
27 import java.sql.ResultSet JavaDoc;
28 import java.lang.reflect.Method JavaDoc;
29 import com.mchange.v2.coalesce.*;
30
31 import java.sql.Connection JavaDoc;
32 import java.sql.ResultSet JavaDoc;
33 import java.lang.reflect.Method JavaDoc;
34 import com.mchange.v2.coalesce.*;
35
36 final class ValueIdentityStatementCacheKey extends StatementCacheKey
37 {
38     //MT: not thread-safe, but protected within the find() method
39
// by StatementCacheKey.class lock
40
final static Coalescer keyCoalescer;
41
42     //MT: modified only within StatementCacheKey.class-locked find() method
43
static ValueIdentityStatementCacheKey spare = new ValueIdentityStatementCacheKey();
44
45     static
46     {
47     CoalesceChecker cc = new CoalesceChecker()
48         {
49         public boolean checkCoalesce( Object JavaDoc a, Object JavaDoc b )
50         { return StatementCacheKey.equals( (StatementCacheKey) a, b ); }
51
52         public int coalesceHash( Object JavaDoc a )
53         { return ((ValueIdentityStatementCacheKey) a).cached_hash; }
54         };
55     
56     //make weak, unsync'ed coalescer
57
keyCoalescer = CoalescerFactory.createCoalescer( cc, true, false );
58     }
59
60     static StatementCacheKey _find( Connection JavaDoc pcon, Method JavaDoc stmtProducingMethod, Object JavaDoc[] args )
61     {
62     ///BEGIN FIND LOGIC///
63
String JavaDoc stmtText = (String JavaDoc) args[0];
64     boolean is_callable = stmtProducingMethod.getName().equals("prepareCall");
65     int result_set_type;
66     int result_set_concurrency;
67
68     int[] columnIndexes;
69     String JavaDoc[] columnNames;
70     Integer JavaDoc autogeneratedKeys;
71     Integer JavaDoc resultSetHoldability;
72
73     if (args.length == 1)
74         {
75         result_set_type = ResultSet.TYPE_FORWARD_ONLY;
76         result_set_concurrency = ResultSet.CONCUR_READ_ONLY;
77         columnIndexes = null;
78         columnNames = null;
79         autogeneratedKeys = null;
80         resultSetHoldability = null;
81         }
82     else if (args.length == 2)
83         {
84         Class JavaDoc[] argTypes = stmtProducingMethod.getParameterTypes();
85         if (argTypes[1].isArray())
86             {
87             Class JavaDoc baseType = argTypes[1].getComponentType();
88             if (baseType == int.class) //second arg is columnIndexes
89
{
90                 result_set_type = ResultSet.TYPE_FORWARD_ONLY;
91                 result_set_concurrency = ResultSet.CONCUR_READ_ONLY;
92                 columnIndexes = (int[]) args[1];
93                 columnNames = null;
94                 autogeneratedKeys = null;
95                 resultSetHoldability = null;
96                 }
97             else if (baseType == String JavaDoc.class)
98                 {
99                 result_set_type = ResultSet.TYPE_FORWARD_ONLY;
100                 result_set_concurrency = ResultSet.CONCUR_READ_ONLY;
101                 columnIndexes = null;
102                 columnNames = (String JavaDoc[]) args[1];
103                 autogeneratedKeys = null;
104                 resultSetHoldability = null;
105                 }
106             else
107                 throw new IllegalArgumentException JavaDoc("c3p0 probably needs to be updated for some new " +
108                                    "JDBC spec! As of JDBC3, we expect two arg statement " +
109                                    "producing methods where the second arg is either " +
110                                    "an int, int array, or String array.");
111             }
112         else //it should be a boxed int, autogeneratedKeys
113
{
114             result_set_type = ResultSet.TYPE_FORWARD_ONLY;
115             result_set_concurrency = ResultSet.CONCUR_READ_ONLY;
116             columnIndexes = null;
117             columnNames = null;
118             autogeneratedKeys = (Integer JavaDoc) args[1];
119             resultSetHoldability = null;
120             }
121         }
122     else if (args.length == 3)
123         {
124         result_set_type = ((Integer JavaDoc) args[1]).intValue();
125         result_set_concurrency = ((Integer JavaDoc) args[2]).intValue();
126         columnIndexes = null;
127         columnNames = null;
128         autogeneratedKeys = null;
129         resultSetHoldability = null;
130         }
131     else if (args.length == 4)
132         {
133         result_set_type = ((Integer JavaDoc) args[1]).intValue();
134         result_set_concurrency = ((Integer JavaDoc) args[2]).intValue();
135         columnIndexes = null;
136         columnNames = null;
137         autogeneratedKeys = null;
138         resultSetHoldability = (Integer JavaDoc) args[3];
139         }
140     else
141         throw new IllegalArgumentException JavaDoc("Unexpected number of args to " +
142                            stmtProducingMethod.getName() );
143     ///END FIND LOGIC///
144

145
146     // we keep around a "spare" and initialize it over and over again
147
// rather than allocating, because usually we'll find the statement we're
148
// looking for is already in the coalescer, and we can avoid the
149
// allocation altogether.
150
spare.init( pcon,
151             stmtText,
152             is_callable,
153             result_set_type,
154             result_set_concurrency,
155             columnIndexes,
156             columnNames,
157             autogeneratedKeys,
158             resultSetHoldability );
159
160     StatementCacheKey out = (StatementCacheKey) keyCoalescer.coalesce( spare );
161
162 // System.err.println( "StatementCacheKey -> " + out );
163
// System.err.println( "Key is coalesced already? " + (out != spare) );
164
// System.err.println( "Keys in coalescer: " + keyCoalescer.countCoalesced() );
165

166     if (out == spare)
167         spare = new ValueIdentityStatementCacheKey();
168     return out;
169     }
170
171     void init( Connection JavaDoc physicalConnection,
172            String JavaDoc stmtText,
173            boolean is_callable,
174            int result_set_type,
175            int result_set_concurrency,
176            int[] columnIndexes,
177            String JavaDoc[] columnNames,
178            Integer JavaDoc autogeneratedKeys,
179            Integer JavaDoc resultSetHoldability )
180     {
181     super.init( physicalConnection,
182             stmtText,
183             is_callable,
184             result_set_type,
185             result_set_concurrency,
186             columnIndexes,
187             columnNames,
188             autogeneratedKeys,
189             resultSetHoldability );
190     this.cached_hash = StatementCacheKey.hashCode( this );
191     }
192
193     // extra instance varieable
194
int cached_hash;
195
196     // Note that we DON'T override equals() or hashCode() here -- each instance let the coalescer guarantee a
197
// single instance exists that would equals() it (that is, itself), and we rely on Object's default equals() and
198
// hashCode methods do their thangs.
199
}
200
201
202
203
Popular Tags