Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commita905eaa

Browse files
author
Dave Cramer
committed
Aaron's patch for Pooled Connections
1 parent0046d80 commita905eaa

File tree

2 files changed

+168
-6
lines changed

2 files changed

+168
-6
lines changed

‎src/interfaces/jdbc/org/postgresql/jdbc2/optional/PooledConnectionImpl.java

Lines changed: 110 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
packageorg.postgresql.jdbc2.optional;
22

33
importjavax.sql.*;
4-
importjava.sql.SQLException;
5-
importjava.sql.Connection;
4+
importjava.sql.*;
65
importjava.util.*;
76
importjava.lang.reflect.*;
87

@@ -13,7 +12,7 @@
1312
* @see ConnectionPool
1413
*
1514
* @author Aaron Mulder (ammulder@chariotsolutions.com)
16-
* @version $Revision: 1.3 $
15+
* @version $Revision: 1.4 $
1716
*/
1817
publicclassPooledConnectionImplimplementsPooledConnection
1918
{
@@ -115,7 +114,9 @@ public Connection getConnection() throws SQLException
115114
con.setAutoCommit(autoCommit);
116115
ConnectionHandlerhandler =newConnectionHandler(con);
117116
last =handler;
118-
return (Connection)Proxy.newProxyInstance(getClass().getClassLoader(),newClass[]{Connection.class},handler);
117+
Connectioncon = (Connection)Proxy.newProxyInstance(getClass().getClassLoader(),newClass[]{Connection.class},handler);
118+
last.setProxy(con);
119+
returncon;
119120
}
120121

121122
/**
@@ -166,6 +167,7 @@ void fireConnectionFatalError(SQLException e)
166167
privateclassConnectionHandlerimplementsInvocationHandler
167168
{
168169
privateConnectioncon;
170+
privateConnectionproxy;// the Connection the client is currently using, which is a proxy
169171
privatebooleanautomatic =false;
170172

171173
publicConnectionHandler(Connectioncon)
@@ -229,6 +231,7 @@ public Object invoke(Object proxy, Method method, Object[] args)
229231
}
230232
con.clearWarnings();
231233
con =null;
234+
proxy =null;
232235
last =null;
233236
fireConnectionClosed();
234237
if (ex !=null)
@@ -237,20 +240,123 @@ public Object invoke(Object proxy, Method method, Object[] args)
237240
}
238241
returnnull;
239242
}
243+
elseif(method.getName().equals("createStatement"))
244+
{
245+
Statementst = (Statement)method.invoke(con,args);
246+
returnProxy.newProxyInstance(getClass().getClassLoader(),newClass[]{Statement.class},newStatementHandler(this,st));
247+
}
248+
elseif(method.getName().equals("prepareCall"))
249+
{
250+
Statementst = (Statement)method.invoke(con,args);
251+
returnProxy.newProxyInstance(getClass().getClassLoader(),newClass[]{CallableStatement.class},newStatementHandler(this,st));
252+
}
253+
elseif(method.getName().equals("prepareStatement"))
254+
{
255+
Statementst = (Statement)method.invoke(con,args);
256+
returnProxy.newProxyInstance(getClass().getClassLoader(),newClass[]{PreparedStatement.class},newStatementHandler(this,st));
257+
}
240258
else
241259
{
242260
returnmethod.invoke(con,args);
243261
}
244262
}
245263

264+
ConnectiongetProxy() {
265+
returnproxy;
266+
}
267+
268+
voidsetProxy(Connectionproxy) {
269+
this.proxy =proxy;
270+
}
271+
246272
publicvoidclose()
247273
{
248274
if (con !=null)
249275
{
250276
automatic =true;
251277
}
252278
con =null;
279+
proxy =null;
253280
// No close event fired here: see JDBC 2.0 Optional Package spec section 6.3
254281
}
282+
283+
publicbooleanisClosed() {
284+
returncon ==null;
285+
}
255286
}
287+
288+
/**
289+
* Instead of declaring classes implementing Statement, PreparedStatement,
290+
* and CallableStatement, which would have to be updated for every JDK rev,
291+
* use a dynamic proxy to handle all calls through the Statement
292+
* interfaces.This is the part that requires JDK 1.3 or higher, though
293+
* JDK 1.2 could be supported with a 3rd-party proxy package.
294+
*
295+
* The StatementHandler is required in order to return the proper
296+
* Connection proxy for the getConnection method.
297+
*/
298+
privatestaticclassStatementHandlerimplementsInvocationHandler {
299+
privateConnectionHandlercon;
300+
privateStatementst;
301+
302+
publicStatementHandler(ConnectionHandlercon,Statementst) {
303+
this.con =con;
304+
this.st =st;
305+
}
306+
publicObjectinvoke(Objectproxy,Methodmethod,Object[]args)
307+
throwsThrowable
308+
{
309+
// From Object
310+
if (method.getDeclaringClass().getName().equals("java.lang.Object"))
311+
{
312+
if (method.getName().equals("toString"))
313+
{
314+
return"Pooled statement wrapping physical statement " +st;
315+
}
316+
if (method.getName().equals("hashCode"))
317+
{
318+
returnnewInteger(st.hashCode());
319+
}
320+
if (method.getName().equals("equals"))
321+
{
322+
if (args[0] ==null)
323+
{
324+
returnBoolean.FALSE;
325+
}
326+
try
327+
{
328+
returnProxy.isProxyClass(args[0].getClass()) && ((StatementHandler)Proxy.getInvocationHandler(args[0])).st ==st ?Boolean.TRUE :Boolean.FALSE;
329+
}
330+
catch (ClassCastExceptione)
331+
{
332+
returnBoolean.FALSE;
333+
}
334+
}
335+
returnmethod.invoke(st,args);
336+
}
337+
// All the rest is from the Statement interface
338+
if (st ==null ||con.isClosed())
339+
{
340+
thrownewSQLException("Statement has been closed");
341+
}
342+
if (method.getName().equals("close"))
343+
{
344+
try {
345+
st.close();
346+
}finally {
347+
con =null;
348+
st =null;
349+
returnnull;
350+
}
351+
}
352+
elseif (method.getName().equals("getConnection"))
353+
{
354+
returncon.getProxy();// the proxied connection, not a physical connection
355+
}
356+
else
357+
{
358+
returnmethod.invoke(st,args);
359+
}
360+
}
361+
}
256362
}

‎src/interfaces/jdbc/org/postgresql/test/jdbc2/optional/ConnectionPoolTest.java

Lines changed: 58 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
* interface to the PooledConnection is through the CPDS.
1212
*
1313
* @author Aaron Mulder (ammulder@chariotsolutions.com)
14-
* @version $Revision: 1.3 $
14+
* @version $Revision: 1.4 $
1515
*/
1616
publicclassConnectionPoolTestextendsBaseDataSourceTest
1717
{
@@ -341,7 +341,63 @@ public void testIsClosed()
341341
}
342342
}
343343

344-
/**
344+
/**
345+
* Ensures that a statement generated by a proxied connection returns the
346+
* proxied connection from getConnection() [not the physical connection].
347+
*/
348+
publicvoidtestStatementConnection() {
349+
try {
350+
PooledConnectionpc =getPooledConnection();
351+
Connectioncon =pc.getConnection();
352+
Statements =con.createStatement();
353+
ConnectionconRetrieved =s.getConnection();
354+
355+
assertTrue(con.getClass().equals(conRetrieved.getClass()));
356+
assertTrue(con.equals(conRetrieved));
357+
}catch (SQLExceptione) {
358+
fail(e.getMessage());
359+
}
360+
}
361+
362+
/**
363+
* Ensures that a prepared statement generated by a proxied connection
364+
* returns the proxied connection from getConnection() [not the physical
365+
* connection].
366+
*/
367+
publicvoidtestPreparedStatementConnection() {
368+
try {
369+
PooledConnectionpc =getPooledConnection();
370+
Connectioncon =pc.getConnection();
371+
PreparedStatements =con.prepareStatement("select 'x'");
372+
ConnectionconRetrieved =s.getConnection();
373+
374+
assertTrue(con.getClass().equals(conRetrieved.getClass()));
375+
assertTrue(con.equals(conRetrieved));
376+
}catch (SQLExceptione) {
377+
fail(e.getMessage());
378+
}
379+
}
380+
381+
/**
382+
* Ensures that a callable statement generated by a proxied connection
383+
* returns the proxied connection from getConnection() [not the physical
384+
* connection].
385+
*/
386+
publicvoidtestCallableStatementConnection() {
387+
try {
388+
PooledConnectionpc =getPooledConnection();
389+
Connectioncon =pc.getConnection();
390+
CallableStatements =con.prepareCall("select 'x'");
391+
ConnectionconRetrieved =s.getConnection();
392+
393+
assertTrue(con.getClass().equals(conRetrieved.getClass()));
394+
assertTrue(con.equals(conRetrieved));
395+
}catch (SQLExceptione) {
396+
fail(e.getMessage());
397+
}
398+
}
399+
400+
/**
345401
* Helper class to remove a listener during event dispatching.
346402
*/
347403
privateclassRemoveCloseimplementsConnectionEventListener

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp