Skip to content

Commit a90f256

Browse files
jhoellerunknown
authored andcommitted
Spring-backed DataSources consistently implement JDBC 4.0's Wrapper interface
Issue: SPR-9770
1 parent f32e407 commit a90f256

File tree

2 files changed

+13
-13
lines changed

2 files changed

+13
-13
lines changed

spring-jdbc/src/main/java/org/springframework/jdbc/datasource/AbstractDataSource.java

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2011 the original author or authors.
2+
* Copyright 2002-2012 the original author or authors.
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.
@@ -24,8 +24,6 @@
2424
import org.apache.commons.logging.Log;
2525
import org.apache.commons.logging.LogFactory;
2626

27-
import org.springframework.util.Assert;
28-
2927
/**
3028
* Abstract base class for Spring's {@link javax.sql.DataSource}
3129
* implementations, taking care of the padding.
@@ -78,17 +76,16 @@ public void setLogWriter(PrintWriter pw) throws SQLException {
7876
//---------------------------------------------------------------------
7977

8078
@SuppressWarnings("unchecked")
81-
public <T> T unwrap(Class<T> iface) throws SQLException {
82-
Assert.notNull(iface, "Interface argument must not be null");
83-
if (!DataSource.class.equals(iface)) {
84-
throw new SQLException("DataSource of type [" + getClass().getName() +
85-
"] can only be unwrapped as [javax.sql.DataSource], not as [" + iface.getName());
79+
public <T> T unwrap(Class<T> iface) throws SQLException {
80+
if (iface.isInstance(this)) {
81+
return (T) this;
8682
}
87-
return (T) this;
83+
throw new SQLException("DataSource of type [" + getClass().getName() +
84+
"] cannot be unwrapped as [" + iface.getName() + "]");
8885
}
8986

9087
public boolean isWrapperFor(Class<?> iface) throws SQLException {
91-
return DataSource.class.equals(iface);
88+
return iface.isInstance(this);
9289
}
9390

9491

spring-jdbc/src/main/java/org/springframework/jdbc/datasource/DelegatingDataSource.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2011 the original author or authors.
2+
* Copyright 2002-2012 the original author or authors.
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.
@@ -110,12 +110,15 @@ public void setLoginTimeout(int seconds) throws SQLException {
110110
//---------------------------------------------------------------------
111111

112112
@SuppressWarnings("unchecked")
113-
public <T> T unwrap(Class<T> iface) throws SQLException {
113+
public <T> T unwrap(Class<T> iface) throws SQLException {
114+
if (iface.isInstance(this)) {
115+
return (T) this;
116+
}
114117
return getTargetDataSource().unwrap(iface);
115118
}
116119

117120
public boolean isWrapperFor(Class<?> iface) throws SQLException {
118-
return getTargetDataSource().isWrapperFor(iface);
121+
return (iface.isInstance(this) || getTargetDataSource().isWrapperFor(iface));
119122
}
120123

121124

0 commit comments

Comments
 (0)