HibernateTransactionManager lazily acquires JDBC Connection

Aligned with HibernateJpaDialect, using ConnectionHandle as functional interface now. Also, LazyConnectionDataSourceProxy supports Connection holdability as applied by HibernateTransactionManager, for use with prepareConnection=true.

Issue: SPR-17216
This commit is contained in:
Juergen Hoeller
2018-08-31 12:40:04 +02:00
parent a689daadf9
commit 78cad0fdd3
8 changed files with 76 additions and 69 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2016 the original author or authors.
* Copyright 2002-2018 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.
@@ -27,6 +27,7 @@ import java.sql.Connection;
* @see SimpleConnectionHandle
* @see ConnectionHolder
*/
@FunctionalInterface
public interface ConnectionHandle {
/**
@@ -36,8 +37,11 @@ public interface ConnectionHandle {
/**
* Release the JDBC Connection that this handle refers to.
* <p>The default implementation is empty, assuming that the lifecycle
* of the connection is managed externally.
* @param con the JDBC Connection to release
*/
void releaseConnection(Connection con);
default void releaseConnection(Connection con) {
}
}

View File

@@ -21,6 +21,7 @@ import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.sql.DataSource;
@@ -256,14 +257,16 @@ public class LazyConnectionDataSourceProxy extends DelegatingDataSource {
@Nullable
private String password;
private Boolean readOnly = Boolean.FALSE;
@Nullable
private Boolean autoCommit;
@Nullable
private Integer transactionIsolation;
private boolean readOnly = false;
private int holdability = ResultSet.CLOSE_CURSORS_AT_COMMIT;
private boolean closed = false;
@Nullable
@@ -319,11 +322,15 @@ public class LazyConnectionDataSourceProxy extends DelegatingDataSource {
if (method.getName().equals("toString")) {
return "Lazy Connection proxy for target DataSource [" + getTargetDataSource() + "]";
}
else if (method.getName().equals("isReadOnly")) {
return this.readOnly;
else if (method.getName().equals("getAutoCommit")) {
if (this.autoCommit != null) {
return this.autoCommit;
}
// Else fetch actual Connection and check there,
// because we didn't have a default specified.
}
else if (method.getName().equals("setReadOnly")) {
this.readOnly = (Boolean) args[0];
else if (method.getName().equals("setAutoCommit")) {
this.autoCommit = (Boolean) args[0];
return null;
}
else if (method.getName().equals("getTransactionIsolation")) {
@@ -337,15 +344,18 @@ public class LazyConnectionDataSourceProxy extends DelegatingDataSource {
this.transactionIsolation = (Integer) args[0];
return null;
}
else if (method.getName().equals("getAutoCommit")) {
if (this.autoCommit != null) {
return this.autoCommit;
}
// Else fetch actual Connection and check there,
// because we didn't have a default specified.
else if (method.getName().equals("isReadOnly")) {
return this.readOnly;
}
else if (method.getName().equals("setAutoCommit")) {
this.autoCommit = (Boolean) args[0];
else if (method.getName().equals("setReadOnly")) {
this.readOnly = (Boolean) args[0];
return null;
}
else if (method.getName().equals("getHoldability")) {
return this.holdability;
}
else if (method.getName().equals("setHoldability")) {
this.holdability = (Integer) args[0];
return null;
}
else if (method.getName().equals("commit")) {

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2012 the original author or authors.
* Copyright 2002-2018 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.
@@ -49,14 +49,6 @@ public class SimpleConnectionHandle implements ConnectionHandle {
return this.connection;
}
/**
* This implementation is empty, as we're using a standard
* Connection handle that does not have to be released.
*/
@Override
public void releaseConnection(Connection con) {
}
@Override
public String toString() {