/
PoolingConnection.java
351 lines (308 loc) · 13.5 KB
/
PoolingConnection.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.datanucleus.store.rdbms.datasource.dbcp;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.NoSuchElementException;
import org.datanucleus.store.rdbms.datasource.dbcp.pool.KeyedObjectPool;
import org.datanucleus.store.rdbms.datasource.dbcp.pool.KeyedPoolableObjectFactory;
/**
* A {@link DelegatingConnection} that pools {@link PreparedStatement}s.
* <p>
* The {@link #prepareStatement} and {@link #prepareCall} methods, rather than creating a new PreparedStatement
* each time, may actually pull the statement from a pool of unused statements.
* The {@link PreparedStatement#close} method of the returned statement doesn't
* actually close the statement, but rather returns it to the pool.
* (See {@link PoolablePreparedStatement}, {@link PoolableCallableStatement}.)
*
*
* @see PoolablePreparedStatement
* @author Rodney Waldhoff
* @author Dirk Verbeeck
* @version $Revision: 885261 $ $Date: 2009-11-29 15:07:02 -0500 (Sun, 29 Nov 2009) $
*/
public class PoolingConnection extends DelegatingConnection implements Connection, KeyedPoolableObjectFactory {
/** Pool of {@link PreparedStatement}s. and {@link CallableStatement}s */
protected KeyedObjectPool _pstmtPool = null;
/** Prepared Statement type */
private static final byte STATEMENT_PREPAREDSTMT = 0;
/** Callable Statement type */
private static final byte STATEMENT_CALLABLESTMT = 1;
/**
* Constructor.
* @param c the underlying {@link Connection}.
*/
public PoolingConnection(Connection c) {
super(c);
}
/**
* Constructor.
* @param c the underlying {@link Connection}.
* @param pool {@link KeyedObjectPool} of {@link PreparedStatement}s and {@link CallableStatement}s.
*/
public PoolingConnection(Connection c, KeyedObjectPool pool) {
super(c);
_pstmtPool = pool;
}
/**
* Close and free all {@link PreparedStatement}s or {@link CallableStatement} from the pool, and
* close the underlying connection.
*/
public synchronized void close() throws SQLException {
if(null != _pstmtPool) {
KeyedObjectPool oldpool = _pstmtPool;
_pstmtPool = null;
try {
oldpool.close();
} catch(RuntimeException e) {
throw e;
} catch(SQLException e) {
throw e;
} catch(Exception e) {
throw (SQLException) new SQLException("Cannot close connection").initCause(e);
}
}
getInnermostDelegate().close();
}
/**
* Create or obtain a {@link PreparedStatement} from the pool.
* @param sql the sql string used to define the PreparedStatement
* @return a {@link PoolablePreparedStatement}
*/
public PreparedStatement prepareStatement(String sql) throws SQLException {
if (null == _pstmtPool) {
throw new SQLException(
"Statement pool is null - closed or invalid PoolingConnection.");
}
try {
return(PreparedStatement)(_pstmtPool.borrowObject(createKey(sql)));
} catch(NoSuchElementException e) {
throw (SQLException) new SQLException("MaxOpenPreparedStatements limit reached").initCause(e);
} catch(RuntimeException e) {
throw e;
} catch(Exception e) {
throw new SQLNestedException("Borrow prepareStatement from pool failed", e);
}
}
/**
* Create or obtain a {@link PreparedStatement} from the pool.
* @param sql the sql string used to define the PreparedStatement
* @param resultSetType result set type
* @param resultSetConcurrency result set concurrency
* @return a {@link PoolablePreparedStatement}
*/
public PreparedStatement prepareStatement(String sql, int resultSetType, int resultSetConcurrency) throws SQLException {
if (null == _pstmtPool) {
throw new SQLException(
"Statement pool is null - closed or invalid PoolingConnection.");
}
try {
return(PreparedStatement)(_pstmtPool.borrowObject(createKey(sql,resultSetType,resultSetConcurrency)));
} catch(NoSuchElementException e) {
throw (SQLException) new SQLException("MaxOpenPreparedStatements limit reached").initCause(e);
} catch(RuntimeException e) {
throw e;
} catch(Exception e) {
throw (SQLException) new SQLException("Borrow prepareStatement from pool failed").initCause(e);
}
}
public CallableStatement prepareCall(String sql) throws SQLException {
try {
return (CallableStatement) (_pstmtPool.borrowObject(createKey(sql, STATEMENT_CALLABLESTMT)));
} catch (NoSuchElementException e) {
throw new SQLNestedException("MaxOpenCallableStatements limit reached", e);
} catch (RuntimeException e) {
throw e;
} catch (Exception e) {
throw new SQLNestedException("Borrow callableStatement from pool failed", e);
}
}
public CallableStatement prepareCall(String sql, int resultSetType, int resultSetConcurrency) throws SQLException {
try {
return (CallableStatement) (_pstmtPool.borrowObject(createKey(sql, resultSetType,
resultSetConcurrency, STATEMENT_CALLABLESTMT)));
} catch (NoSuchElementException e) {
throw new SQLNestedException("MaxOpenCallableStatements limit reached", e);
} catch (RuntimeException e) {
throw e;
} catch (Exception e) {
throw new SQLNestedException("Borrow callableStatement from pool failed", e);
}
}
protected Object createKey(String sql, int resultSetType, int resultSetConcurrency) {
String catalog = null;
try {
catalog = getCatalog();
} catch (SQLException e) {}
return new PStmtKey(normalizeSQL(sql), catalog, resultSetType, resultSetConcurrency);
}
protected Object createKey(String sql, int resultSetType, int resultSetConcurrency, byte stmtType) {
String catalog = null;
try {
catalog = getCatalog();
} catch (SQLException e) {}
return new PStmtKey(normalizeSQL(sql), catalog, resultSetType, resultSetConcurrency, stmtType);
}
protected Object createKey(String sql) {
String catalog = null;
try {
catalog = getCatalog();
} catch (SQLException e) {}
return new PStmtKey(normalizeSQL(sql), catalog);
}
protected Object createKey(String sql, byte stmtType) {
String catalog = null;
try {
catalog = getCatalog();
} catch (SQLException e) {}
return new PStmtKey(normalizeSQL(sql), catalog, stmtType);
}
protected String normalizeSQL(String sql) {
return sql.trim();
}
public Object makeObject(Object obj) throws Exception {
if(null == obj || !(obj instanceof PStmtKey)) {
throw new IllegalArgumentException("Prepared statement key is null or invalid.");
} else {
PStmtKey key = (PStmtKey)obj;
if( null == key._resultSetType && null == key._resultSetConcurrency ) {
if (key._stmtType == STATEMENT_PREPAREDSTMT ) {
return new PoolablePreparedStatement(getDelegate().prepareStatement( key._sql), key, _pstmtPool, this);
} else {
return new PoolableCallableStatement(getDelegate().prepareCall( key._sql), key, _pstmtPool, this);
}
} else { // Both _resultSetType and _resultSetConcurrency are non-null here (both or neither are set by constructors)
if(key._stmtType == STATEMENT_PREPAREDSTMT) {
return new PoolablePreparedStatement(getDelegate().prepareStatement(
key._sql, key._resultSetType.intValue(),key._resultSetConcurrency.intValue()), key, _pstmtPool, this);
} else {
return new PoolableCallableStatement( getDelegate().prepareCall(
key._sql,key._resultSetType.intValue(), key._resultSetConcurrency.intValue()), key, _pstmtPool, this);
}
}
}
}
public void destroyObject(Object key, Object obj) throws Exception {
if(obj instanceof DelegatingPreparedStatement) {
((DelegatingPreparedStatement)obj).getInnermostDelegate().close();
} else {
((PreparedStatement)obj).close();
}
}
public boolean validateObject(Object key, Object obj) {
return true;
}
public void activateObject(Object key, Object obj) throws Exception {
((DelegatingPreparedStatement)obj).activate();
}
public void passivateObject(Object key, Object obj) throws Exception {
((PreparedStatement)obj).clearParameters();
((DelegatingPreparedStatement)obj).passivate();
}
public String toString() {
if (_pstmtPool != null ) {
return "PoolingConnection: " + _pstmtPool.toString();
} else {
return "PoolingConnection: null";
}
}
/**
* A key uniquely identifiying {@link PreparedStatement}s.
*/
static class PStmtKey {
/** SQL defining Prepared or Callable Statement */
protected String _sql = null;
/** Result set type */
protected Integer _resultSetType = null;
/** Result set concurrency */
protected Integer _resultSetConcurrency = null;
/** Database catalog */
protected String _catalog = null;
/**
* Statement type. Either STATEMENT_PREPAREDSTMT (PreparedStatement)
* or STATEMENT_CALLABLESTMT (CallableStatement)
*/
protected byte _stmtType = STATEMENT_PREPAREDSTMT;
PStmtKey(String sql) {
_sql = sql;
}
PStmtKey(String sql, String catalog) {
_sql = sql;
_catalog = catalog;
}
PStmtKey(String sql, String catalog, byte stmtType) {
_sql = sql;
_catalog = catalog;
_stmtType = stmtType;
}
PStmtKey(String sql, int resultSetType, int resultSetConcurrency) {
_sql = sql;
_resultSetType = new Integer(resultSetType);
_resultSetConcurrency = new Integer(resultSetConcurrency);
}
PStmtKey(String sql, String catalog, int resultSetType, int resultSetConcurrency) {
_sql = sql;
_catalog = catalog;
_resultSetType = new Integer(resultSetType);
_resultSetConcurrency = new Integer(resultSetConcurrency);
}
PStmtKey(String sql, String catalog, int resultSetType, int resultSetConcurrency, byte stmtType) {
_sql = sql;
_catalog = catalog;
_resultSetType = new Integer(resultSetType);
_resultSetConcurrency = new Integer(resultSetConcurrency);
_stmtType = stmtType;
}
public boolean equals(Object that) {
try {
PStmtKey key = (PStmtKey)that;
return( ((null == _sql && null == key._sql) || _sql.equals(key._sql)) &&
((null == _catalog && null == key._catalog) || _catalog.equals(key._catalog)) &&
((null == _resultSetType && null == key._resultSetType) || _resultSetType.equals(key._resultSetType)) &&
((null == _resultSetConcurrency && null == key._resultSetConcurrency) || _resultSetConcurrency.equals(key._resultSetConcurrency)) &&
(_stmtType == key._stmtType)
);
} catch(ClassCastException e) {
return false;
} catch(NullPointerException e) {
return false;
}
}
public int hashCode() {
if (_catalog==null)
return(null == _sql ? 0 : _sql.hashCode());
else
return(null == _sql ? _catalog.hashCode() : (_catalog + _sql).hashCode());
}
public String toString() {
StringBuffer buf = new StringBuffer();
buf.append("PStmtKey: sql=");
buf.append(_sql);
buf.append(", catalog=");
buf.append(_catalog);
buf.append(", resultSetType=");
buf.append(_resultSetType);
buf.append(", resultSetConcurrency=");
buf.append(_resultSetConcurrency);
buf.append(", statmentType=");
buf.append(_stmtType);
return buf.toString();
}
}
}