diff --git a/org.springframework.jdbc/src/main/java/org/springframework/jdbc/core/JdbcTemplate.java b/org.springframework.jdbc/src/main/java/org/springframework/jdbc/core/JdbcTemplate.java index f3beb039e96..0cba943b506 100644 --- a/org.springframework.jdbc/src/main/java/org/springframework/jdbc/core/JdbcTemplate.java +++ b/org.springframework.jdbc/src/main/java/org/springframework/jdbc/core/JdbcTemplate.java @@ -1295,7 +1295,7 @@ public class JdbcTemplate extends JdbcAccessor implements JdbcOperations { /** - * Invocation handler that suppresses close calls on JDBC COnnections. + * Invocation handler that suppresses close calls on JDBC Connections. * Also prepares returned Statement (Prepared/CallbackStatement) objects. * @see java.sql.Connection#close() */ @@ -1310,11 +1310,7 @@ public class JdbcTemplate extends JdbcAccessor implements JdbcOperations { public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { // Invocation on ConnectionProxy interface coming in... - if (method.getName().equals("getTargetConnection")) { - // Handle getTargetConnection method: return underlying Connection. - return this.target; - } - else if (method.getName().equals("equals")) { + if (method.getName().equals("equals")) { // Only consider equal when proxies are identical. return (proxy == args[0]); } @@ -1322,10 +1318,27 @@ public class JdbcTemplate extends JdbcAccessor implements JdbcOperations { // Use hashCode of PersistenceManager proxy. return System.identityHashCode(proxy); } + else if (method.getName().equals("unwrap")) { + if (((Class) args[0]).isInstance(proxy)) { + return proxy; + } + } + else if (method.getName().equals("isWrapperFor")) { + if (((Class) args[0]).isInstance(proxy)) { + return true; + } + } else if (method.getName().equals("close")) { // Handle close method: suppress, not valid. return null; } + else if (method.getName().equals("isClosed")) { + return false; + } + else if (method.getName().equals("getTargetConnection")) { + // Handle getTargetConnection method: return underlying Connection. + return this.target; + } // Invoke method on target Connection. try { diff --git a/org.springframework.jdbc/src/main/java/org/springframework/jdbc/datasource/LazyConnectionDataSourceProxy.java b/org.springframework.jdbc/src/main/java/org/springframework/jdbc/datasource/LazyConnectionDataSourceProxy.java index 34c7401d853..f6ddc15b349 100644 --- a/org.springframework.jdbc/src/main/java/org/springframework/jdbc/datasource/LazyConnectionDataSourceProxy.java +++ b/org.springframework.jdbc/src/main/java/org/springframework/jdbc/datasource/LazyConnectionDataSourceProxy.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2009 the original author or authors. + * Copyright 2002-2010 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -288,6 +288,16 @@ public class LazyConnectionDataSourceProxy extends DelegatingDataSource { // Connection has been fetched: use hashCode of Connection proxy. return System.identityHashCode(proxy); } + else if (method.getName().equals("unwrap")) { + if (((Class) args[0]).isInstance(proxy)) { + return proxy; + } + } + else if (method.getName().equals("isWrapperFor")) { + if (((Class) args[0]).isInstance(proxy)) { + return true; + } + } else if (method.getName().equals("getTargetConnection")) { // Handle getTargetConnection method: return underlying connection. return getTargetConnection(method); @@ -344,14 +354,14 @@ public class LazyConnectionDataSourceProxy extends DelegatingDataSource { else if (method.getName().equals("clearWarnings")) { return null; } - else if (method.getName().equals("isClosed")) { - return this.closed; - } else if (method.getName().equals("close")) { // Ignore: no target connection yet. this.closed = true; return null; } + else if (method.getName().equals("isClosed")) { + return this.closed; + } else if (this.closed) { // Connection proxy closed, without ever having fetched a // physical JDBC Connection: throw corresponding SQLException. diff --git a/org.springframework.jdbc/src/main/java/org/springframework/jdbc/datasource/SingleConnectionDataSource.java b/org.springframework.jdbc/src/main/java/org/springframework/jdbc/datasource/SingleConnectionDataSource.java index 4ad4fddbeba..05edcab08bb 100644 --- a/org.springframework.jdbc/src/main/java/org/springframework/jdbc/datasource/SingleConnectionDataSource.java +++ b/org.springframework.jdbc/src/main/java/org/springframework/jdbc/datasource/SingleConnectionDataSource.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2009 the original author or authors. + * Copyright 2002-2010 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -325,10 +325,23 @@ public class SingleConnectionDataSource extends DriverManagerDataSource // Use hashCode of Connection proxy. return System.identityHashCode(proxy); } + else if (method.getName().equals("unwrap")) { + if (((Class) args[0]).isInstance(proxy)) { + return proxy; + } + } + else if (method.getName().equals("isWrapperFor")) { + if (((Class) args[0]).isInstance(proxy)) { + return true; + } + } else if (method.getName().equals("close")) { // Handle close method: don't pass the call on. return null; } + else if (method.getName().equals("isClosed")) { + return false; + } else if (method.getName().equals("getTargetConnection")) { // Handle getTargetConnection method: return underlying Connection. return this.target; diff --git a/org.springframework.jdbc/src/main/java/org/springframework/jdbc/datasource/TransactionAwareDataSourceProxy.java b/org.springframework.jdbc/src/main/java/org/springframework/jdbc/datasource/TransactionAwareDataSourceProxy.java index 5ff625fe40e..535ef53d10e 100644 --- a/org.springframework.jdbc/src/main/java/org/springframework/jdbc/datasource/TransactionAwareDataSourceProxy.java +++ b/org.springframework.jdbc/src/main/java/org/springframework/jdbc/datasource/TransactionAwareDataSourceProxy.java @@ -196,8 +196,15 @@ public class TransactionAwareDataSourceProxy extends DelegatingDataSource { } return sb.toString(); } - else if (method.getName().equals("isClosed")) { - return this.closed; + else if (method.getName().equals("unwrap")) { + if (((Class) args[0]).isInstance(proxy)) { + return proxy; + } + } + else if (method.getName().equals("isWrapperFor")) { + if (((Class) args[0]).isInstance(proxy)) { + return true; + } } else if (method.getName().equals("close")) { // Handle close method: only close if not within a transaction. @@ -205,6 +212,9 @@ public class TransactionAwareDataSourceProxy extends DelegatingDataSource { this.closed = true; return null; } + else if (method.getName().equals("isClosed")) { + return this.closed; + } if (this.target == null) { if (this.closed) {