Skip to content

Commit 2188eb2

Browse files
committed
fix for http://code.google.com/p/mybatis/issues/detail?id=579 . toString() should never fail.
1 parent 61f777b commit 2188eb2

File tree

2 files changed

+21
-9
lines changed

2 files changed

+21
-9
lines changed

src/main/java/org/apache/ibatis/datasource/pooled/PooledConnection.java

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2009-2011 The MyBatis Team
2+
* Copyright 2009-2012 The MyBatis Team
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -15,18 +15,18 @@
1515
*/
1616
package org.apache.ibatis.datasource.pooled;
1717

18-
import org.apache.ibatis.datasource.DataSourceException;
19-
import org.apache.ibatis.reflection.ExceptionUtil;
20-
2118
import java.lang.reflect.InvocationHandler;
2219
import java.lang.reflect.Method;
2320
import java.lang.reflect.Proxy;
2421
import java.sql.Connection;
22+
import java.sql.SQLException;
23+
24+
import org.apache.ibatis.reflection.ExceptionUtil;
2525

2626
class PooledConnection implements InvocationHandler {
2727

2828
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 };
3030

3131
private int hashCode = 0;
3232
private PooledDataSource dataSource;
@@ -237,18 +237,23 @@ public Object invoke(Object proxy, Method method, Object[] args)
237237
return null;
238238
} else {
239239
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);
241247
} catch (Throwable t) {
242248
throw ExceptionUtil.unwrapThrowable(t);
243249
}
244250
}
245251
}
246252

247-
private Connection getValidConnection() {
253+
private void checkConnection() throws SQLException {
248254
if (!valid) {
249-
throw new DataSourceException("Error accessing PooledConnection. Connection is invalid.");
255+
throw new SQLException("Error accessing PooledConnection. Connection is invalid.");
250256
}
251-
return realConnection;
252257
}
253258

254259
}

src/test/java/org/apache/ibatis/jdbc/PooledDataSourceTest.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,4 +69,11 @@ public void shouldProperlyMaintainPoolOf3ActiveAnd2IdleConnections() throws Exce
6969
}
7070
}
7171

72+
@Test
73+
public void shouldNotFailCallingToStringOverAnInvalidConnection() throws Exception {
74+
PooledDataSource ds = createPooledDataSource(JPETSTORE_PROPERTIES);
75+
Connection c = ds.getConnection();
76+
c.close();
77+
c.toString();
78+
}
7279
}

0 commit comments

Comments
 (0)