Skip to content

Commit 39eef1f

Browse files
committed
#4 - ENH: Add support for unwrapping java.sql.Connection via unwrap(java.sql.Connection)
1 parent 3e30386 commit 39eef1f

2 files changed

Lines changed: 31 additions & 0 deletions

File tree

src/main/java/org/avaje/datasource/delegate/ConnectionDelegator.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,13 @@ public ConnectionDelegator(Connection delegate) {
1313
this.delegate = delegate;
1414
}
1515

16+
/**
17+
* Return the underlying connection.
18+
*/
19+
public Connection getDelegate() {
20+
return delegate;
21+
}
22+
1623
@Override
1724
public void setSchema(String schema) throws SQLException {
1825
delegate.setSchema(schema);
@@ -231,6 +238,9 @@ public Struct createStruct(String typeName, Object[] attributes) throws SQLExcep
231238
}
232239

233240
public <T> T unwrap(Class<T> iface) throws SQLException {
241+
if (iface.equals(java.sql.Connection.class)) {
242+
return (T)delegate;
243+
}
234244
return delegate.unwrap(iface);
235245
}
236246

src/test/java/org/avaje/datasource/pool/ConnectionPoolTest.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,4 +95,25 @@ public void getConnection_explicitUserPassword() throws SQLException {
9595
Connection another = pool.getConnection("testing", "123");
9696
another.close();
9797
}
98+
99+
@Test
100+
public void unwrapConnection() throws SQLException {
101+
102+
Connection connection = pool.getConnection();
103+
Connection underlying = connection.unwrap(Connection.class);
104+
105+
assertThat(underlying).isInstanceOf(org.h2.jdbc.JdbcConnection.class);
106+
connection.close();
107+
}
108+
109+
@Test
110+
public void getDelegate() throws SQLException {
111+
112+
Connection connection = pool.getConnection();
113+
PooledConnection pc = (PooledConnection)connection;
114+
Connection underlying = pc.getDelegate();
115+
116+
assertThat(underlying).isInstanceOf(org.h2.jdbc.JdbcConnection.class);
117+
connection.close();
118+
}
98119
}

0 commit comments

Comments
 (0)