Skip to content

Commit da8e2d6

Browse files
committed
AbstractRoutingDataSource consistently implements JDBC 4.0's Wrapper interface as well
Issue: SPR-9856
1 parent 6ed589d commit da8e2d6

File tree

1 file changed

+29
-15
lines changed

1 file changed

+29
-15
lines changed

org.springframework.jdbc/src/main/java/org/springframework/jdbc/datasource/lookup/AbstractRoutingDataSource.java

Lines changed: 29 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2010 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.
@@ -51,6 +51,7 @@ public abstract class AbstractRoutingDataSource extends AbstractDataSource imple
5151

5252
private DataSource resolvedDefaultDataSource;
5353

54+
5455
/**
5556
* Specify the map of target DataSources, with the lookup key as key.
5657
* The mapped value can either be a corresponding {@link javax.sql.DataSource}
@@ -121,6 +122,19 @@ public void afterPropertiesSet() {
121122
}
122123
}
123124

125+
/**
126+
* Resolve the given lookup key object, as specified in the
127+
* {@link #setTargetDataSources targetDataSources} map, into
128+
* the actual lookup key to be used for matching with the
129+
* {@link #determineCurrentLookupKey() current lookup key}.
130+
* <p>The default implementation simply returns the given key as-is.
131+
* @param lookupKey the lookup key object as specified by the user
132+
* @return the lookup key as needed for matching
133+
*/
134+
protected Object resolveSpecifiedLookupKey(Object lookupKey) {
135+
return lookupKey;
136+
}
137+
124138
/**
125139
* Resolve the specified data source object into a DataSource instance.
126140
* <p>The default implementation handles DataSource instances and data source
@@ -152,6 +166,20 @@ public Connection getConnection(String username, String password) throws SQLExce
152166
return determineTargetDataSource().getConnection(username, password);
153167
}
154168

169+
@Override
170+
@SuppressWarnings("unchecked")
171+
public <T> T unwrap(Class<T> iface) throws SQLException {
172+
if (iface.isInstance(this)) {
173+
return (T) this;
174+
}
175+
return determineTargetDataSource().unwrap(iface);
176+
}
177+
178+
@Override
179+
public boolean isWrapperFor(Class<?> iface) throws SQLException {
180+
return (iface.isInstance(this) || determineTargetDataSource().isWrapperFor(iface));
181+
}
182+
155183
/**
156184
* Retrieve the current target DataSource. Determines the
157185
* {@link #determineCurrentLookupKey() current lookup key}, performs
@@ -173,20 +201,6 @@ protected DataSource determineTargetDataSource() {
173201
return dataSource;
174202
}
175203

176-
177-
/**
178-
* Resolve the given lookup key object, as specified in the
179-
* {@link #setTargetDataSources targetDataSources} map, into
180-
* the actual lookup key to be used for matching with the
181-
* {@link #determineCurrentLookupKey() current lookup key}.
182-
* <p>The default implementation simply returns the given key as-is.
183-
* @param lookupKey the lookup key object as specified by the user
184-
* @return the lookup key as needed for matching
185-
*/
186-
protected Object resolveSpecifiedLookupKey(Object lookupKey) {
187-
return lookupKey;
188-
}
189-
190204
/**
191205
* Determine the current lookup key. This will typically be
192206
* implemented to check a thread-bound transaction context.

0 commit comments

Comments
 (0)