mirror of
https://github.com/spring-projects/spring-framework.git
synced 2026-09-26 08:59:09 +00:00
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:
+40
-29
@@ -197,9 +197,16 @@ public class HibernateTransactionManager extends AbstractPlatformTransactionMana
|
||||
* manager needs to work on the underlying target DataSource. If there's
|
||||
* nevertheless a TransactionAwareDataSourceProxy passed in, it will be
|
||||
* unwrapped to extract its target DataSource.
|
||||
* <p><b>NOTE: For scenarios with many transactions that just read data from
|
||||
* Hibernate's cache (and do not actually access the database), consider using
|
||||
* a {@link org.springframework.jdbc.datasource.LazyConnectionDataSourceProxy}
|
||||
* for the actual target DataSource. Alternatively, consider switching
|
||||
* {@link #setPrepareConnection "prepareConnection"} to {@code false}.</b>
|
||||
* In both cases, this transaction manager will not eagerly acquire a
|
||||
* JDBC Connection for each Hibernate Session anymore (as of Spring 5.1).
|
||||
* @see #setAutodetectDataSource
|
||||
* @see TransactionAwareDataSourceProxy
|
||||
* @see DataSourceUtils
|
||||
* @see org.springframework.jdbc.datasource.LazyConnectionDataSourceProxy
|
||||
* @see org.springframework.jdbc.core.JdbcTemplate
|
||||
*/
|
||||
public void setDataSource(@Nullable DataSource dataSource) {
|
||||
@@ -462,33 +469,37 @@ public class HibernateTransactionManager extends AbstractPlatformTransactionMana
|
||||
|
||||
session = txObject.getSessionHolder().getSession();
|
||||
|
||||
if (this.prepareConnection && isSameConnectionForEntireSession(session)) {
|
||||
// We're allowed to change the transaction settings of the JDBC Connection.
|
||||
if (logger.isDebugEnabled()) {
|
||||
logger.debug("Preparing JDBC Connection of Hibernate Session [" + session + "]");
|
||||
}
|
||||
Connection con = ((SessionImplementor) session).connection();
|
||||
Integer previousIsolationLevel = DataSourceUtils.prepareConnectionForTransaction(con, definition);
|
||||
txObject.setPreviousIsolationLevel(previousIsolationLevel);
|
||||
if (this.allowResultAccessAfterCompletion && !txObject.isNewSession()) {
|
||||
int currentHoldability = con.getHoldability();
|
||||
if (currentHoldability != ResultSet.HOLD_CURSORS_OVER_COMMIT) {
|
||||
txObject.setPreviousHoldability(currentHoldability);
|
||||
con.setHoldability(ResultSet.HOLD_CURSORS_OVER_COMMIT);
|
||||
boolean holdabilityNeeded = this.allowResultAccessAfterCompletion && !txObject.isNewSession();
|
||||
boolean isolationLevelNeeded = (definition.getIsolationLevel() != TransactionDefinition.ISOLATION_DEFAULT);
|
||||
if (holdabilityNeeded || isolationLevelNeeded || definition.isReadOnly()) {
|
||||
if (this.prepareConnection && isSameConnectionForEntireSession(session)) {
|
||||
// We're allowed to change the transaction settings of the JDBC Connection.
|
||||
if (logger.isDebugEnabled()) {
|
||||
logger.debug("Preparing JDBC Connection of Hibernate Session [" + session + "]");
|
||||
}
|
||||
Connection con = ((SessionImplementor) session).connection();
|
||||
Integer previousIsolationLevel = DataSourceUtils.prepareConnectionForTransaction(con, definition);
|
||||
txObject.setPreviousIsolationLevel(previousIsolationLevel);
|
||||
if (this.allowResultAccessAfterCompletion && !txObject.isNewSession()) {
|
||||
int currentHoldability = con.getHoldability();
|
||||
if (currentHoldability != ResultSet.HOLD_CURSORS_OVER_COMMIT) {
|
||||
txObject.setPreviousHoldability(currentHoldability);
|
||||
con.setHoldability(ResultSet.HOLD_CURSORS_OVER_COMMIT);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
else {
|
||||
// Not allowed to change the transaction settings of the JDBC Connection.
|
||||
if (definition.getIsolationLevel() != TransactionDefinition.ISOLATION_DEFAULT) {
|
||||
// We should set a specific isolation level but are not allowed to...
|
||||
throw new InvalidIsolationLevelException(
|
||||
"HibernateTransactionManager is not allowed to support custom isolation levels: " +
|
||||
"make sure that its 'prepareConnection' flag is on (the default) and that the " +
|
||||
"Hibernate connection release mode is set to 'on_close' (the default for JDBC).");
|
||||
}
|
||||
if (logger.isDebugEnabled()) {
|
||||
logger.debug("Not preparing JDBC Connection of Hibernate Session [" + session + "]");
|
||||
else {
|
||||
// Not allowed to change the transaction settings of the JDBC Connection.
|
||||
if (isolationLevelNeeded) {
|
||||
// We should set a specific isolation level but are not allowed to...
|
||||
throw new InvalidIsolationLevelException(
|
||||
"HibernateTransactionManager is not allowed to support custom isolation levels: " +
|
||||
"make sure that its 'prepareConnection' flag is on (the default) and that the " +
|
||||
"Hibernate connection release mode is set to 'on_close' (the default for JDBC).");
|
||||
}
|
||||
if (logger.isDebugEnabled()) {
|
||||
logger.debug("Not preparing JDBC Connection of Hibernate Session [" + session + "]");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -529,13 +540,13 @@ public class HibernateTransactionManager extends AbstractPlatformTransactionMana
|
||||
|
||||
// Register the Hibernate Session's JDBC Connection for the DataSource, if set.
|
||||
if (getDataSource() != null) {
|
||||
Connection con = ((SessionImplementor) session).connection();
|
||||
ConnectionHolder conHolder = new ConnectionHolder(con);
|
||||
SessionImplementor sessionImpl = (SessionImplementor) session;
|
||||
ConnectionHolder conHolder = new ConnectionHolder(sessionImpl::connection);
|
||||
if (timeout != TransactionDefinition.TIMEOUT_DEFAULT) {
|
||||
conHolder.setTimeoutInSeconds(timeout);
|
||||
}
|
||||
if (logger.isDebugEnabled()) {
|
||||
logger.debug("Exposing Hibernate transaction as JDBC transaction [" + con + "]");
|
||||
logger.debug("Exposing Hibernate transaction as JDBC [" + conHolder.getConnectionHandle() + "]");
|
||||
}
|
||||
TransactionSynchronizationManager.bindResource(getDataSource(), conHolder);
|
||||
txObject.setConnectionHolder(conHolder);
|
||||
|
||||
@@ -416,7 +416,7 @@ public class JpaTransactionManager extends AbstractPlatformTransactionManager
|
||||
conHolder.setTimeoutInSeconds(timeoutToUse);
|
||||
}
|
||||
if (logger.isDebugEnabled()) {
|
||||
logger.debug("Exposing JPA transaction as JDBC transaction [" + conHandle + "]");
|
||||
logger.debug("Exposing JPA transaction as JDBC [" + conHandle + "]");
|
||||
}
|
||||
TransactionSynchronizationManager.bindResource(getDataSource(), conHolder);
|
||||
txObject.setConnectionHolder(conHolder);
|
||||
|
||||
-4
@@ -131,10 +131,6 @@ public class EclipseLinkJpaDialect extends DefaultJpaDialect {
|
||||
}
|
||||
return this.connection;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void releaseConnection(Connection con) {
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
+3
-9
@@ -75,7 +75,7 @@ import org.springframework.util.ReflectionUtils;
|
||||
|
||||
/**
|
||||
* {@link org.springframework.orm.jpa.JpaDialect} implementation for
|
||||
* Hibernate EntityManager. Developed against Hibernate 5.0/5.1/5.2.
|
||||
* Hibernate EntityManager. Developed against Hibernate 5.1/5.2/5.3.
|
||||
*
|
||||
* @author Juergen Hoeller
|
||||
* @author Costin Leau
|
||||
@@ -119,10 +119,8 @@ public class HibernateJpaDialect extends DefaultJpaDialect {
|
||||
* Hibernate Session, that is, whether to apply a transaction-specific
|
||||
* isolation level and/or the transaction's read-only flag to the underlying
|
||||
* JDBC Connection.
|
||||
* <p>Default is "true" on Hibernate EntityManager 4.x (with its 'on-close'
|
||||
* connection release mode.
|
||||
* <p>If you turn this flag off, JPA transaction management will not support
|
||||
* per-transaction isolation levels anymore. It will not call
|
||||
* <p>Default is "true". If you turn this flag off, JPA transaction management
|
||||
* will not support per-transaction isolation levels anymore. It will not call
|
||||
* {@code Connection.setReadOnly(true)} for read-only transactions anymore either.
|
||||
* If this flag is turned off, no cleanup of a JDBC Connection is required after
|
||||
* a transaction, since no Connection settings will get modified.
|
||||
@@ -415,10 +413,6 @@ public class HibernateJpaDialect extends DefaultJpaDialect {
|
||||
return doGetConnection(this.session);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void releaseConnection(Connection con) {
|
||||
}
|
||||
|
||||
public static Connection doGetConnection(Session session) {
|
||||
try {
|
||||
Method methodToUse = connectionMethodToUse;
|
||||
|
||||
+1
-1
@@ -41,7 +41,7 @@ import org.springframework.lang.Nullable;
|
||||
|
||||
/**
|
||||
* {@link org.springframework.orm.jpa.JpaVendorAdapter} implementation for Hibernate
|
||||
* EntityManager. Developed and tested against Hibernate 5.0, 5.1 and 5.2;
|
||||
* EntityManager. Developed and tested against Hibernate 5.0, 5.1, 5.2 and 5.3;
|
||||
* backwards-compatible with Hibernate 4.3 at runtime on a best-effort basis.
|
||||
*
|
||||
* <p>Exposes Hibernate's persistence provider and EntityManager extension interface,
|
||||
|
||||
Reference in New Issue
Block a user