|
1 | 1 | /*
|
2 |
| - * Copyright 2009-2011 The MyBatis Team |
| 2 | + * Copyright 2009-2012 The MyBatis Team |
3 | 3 | *
|
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
5 | 5 | * you may not use this file except in compliance with the License.
|
|
15 | 15 | */
|
16 | 16 | package org.apache.ibatis.datasource.pooled;
|
17 | 17 |
|
18 |
| -import org.apache.ibatis.datasource.DataSourceException; |
19 |
| -import org.apache.ibatis.reflection.ExceptionUtil; |
20 |
| - |
21 | 18 | import java.lang.reflect.InvocationHandler;
|
22 | 19 | import java.lang.reflect.Method;
|
23 | 20 | import java.lang.reflect.Proxy;
|
24 | 21 | import java.sql.Connection;
|
| 22 | +import java.sql.SQLException; |
| 23 | + |
| 24 | +import org.apache.ibatis.reflection.ExceptionUtil; |
25 | 25 |
|
26 | 26 | class PooledConnection implements InvocationHandler {
|
27 | 27 |
|
28 | 28 | private static final String CLOSE = "close";
|
29 |
| - private static final Class<?>[] IFACES = new Class<?>[]{Connection.class}; |
| 29 | + private static final Class<?>[] IFACES = new Class<?>[] { Connection.class }; |
30 | 30 |
|
31 | 31 | private int hashCode = 0;
|
32 | 32 | private PooledDataSource dataSource;
|
@@ -237,18 +237,23 @@ public Object invoke(Object proxy, Method method, Object[] args)
|
237 | 237 | return null;
|
238 | 238 | } else {
|
239 | 239 | try {
|
240 |
| - return method.invoke(getValidConnection(), args); |
| 240 | + if (method.getDeclaringClass() != Object.class) { |
| 241 | + // issue #578. |
| 242 | + // toString() should never fail |
| 243 | + // throw an SQLException instead of a Runtime |
| 244 | + checkConnection(); |
| 245 | + } |
| 246 | + return method.invoke(realConnection, args); |
241 | 247 | } catch (Throwable t) {
|
242 | 248 | throw ExceptionUtil.unwrapThrowable(t);
|
243 | 249 | }
|
244 | 250 | }
|
245 | 251 | }
|
246 | 252 |
|
247 |
| - private Connection getValidConnection() { |
| 253 | + private void checkConnection() throws SQLException { |
248 | 254 | if (!valid) {
|
249 |
| - throw new DataSourceException("Error accessing PooledConnection. Connection is invalid."); |
| 255 | + throw new SQLException("Error accessing PooledConnection. Connection is invalid."); |
250 | 256 | }
|
251 |
| - return realConnection; |
252 | 257 | }
|
253 | 258 |
|
254 | 259 | }
|
0 commit comments