提交 be04aca0 编写于 作者: J Juergen Hoeller

consistent handling of unwrap/isWrapperFor/isClosed in JDBC proxies

上级 153680a5
......@@ -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 {
......
/*
* 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.
......
/*
* 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;
......
......@@ -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) {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册