Example usage for org.springframework.jdbc.datasource ConnectionHolder hasConnection

List of usage examples for org.springframework.jdbc.datasource ConnectionHolder hasConnection

Introduction

In this page you can find the example usage for org.springframework.jdbc.datasource ConnectionHolder hasConnection.

Prototype

protected boolean hasConnection() 

Source Link

Document

Return whether this holder currently has a Connection.

Usage

From source file:org.springframework.jdbc.datasource.DataSourceUtils.java

/**
 * Actually obtain a JDBC Connection from the given DataSource.
 * Same as {@link #getConnection}, but throwing the original SQLException.
 * <p>Is aware of a corresponding Connection bound to the current thread, for example
 * when using {@link DataSourceTransactionManager}. Will bind a Connection to the thread
 * if transaction synchronization is active (e.g. if in a JTA transaction).
 * <p>Directly accessed by {@link TransactionAwareDataSourceProxy}.
 * @param dataSource the DataSource to obtain Connections from
 * @return a JDBC Connection from the given DataSource
 * @throws SQLException if thrown by JDBC methods
 * @see #doReleaseConnection/*from  w ww. j a va2 s.  c o  m*/
 */
public static Connection doGetConnection(DataSource dataSource) throws SQLException {
    Assert.notNull(dataSource, "No DataSource specified");

    ConnectionHolder conHolder = (ConnectionHolder) TransactionSynchronizationManager.getResource(dataSource);
    if (conHolder != null && (conHolder.hasConnection() || conHolder.isSynchronizedWithTransaction())) {
        conHolder.requested();
        if (!conHolder.hasConnection()) {
            logger.debug("Fetching resumed JDBC Connection from DataSource");
            conHolder.setConnection(fetchConnection(dataSource));
        }
        return conHolder.getConnection();
    }
    // Else we either got no holder or an empty thread-bound holder here.

    logger.debug("Fetching JDBC Connection from DataSource");
    Connection con = fetchConnection(dataSource);

    if (TransactionSynchronizationManager.isSynchronizationActive()) {
        logger.debug("Registering transaction synchronization for JDBC Connection");
        // Use same Connection for further JDBC actions within the transaction.
        // Thread-bound object will get removed by synchronization at transaction completion.
        ConnectionHolder holderToUse = conHolder;
        if (holderToUse == null) {
            holderToUse = new ConnectionHolder(con);
        } else {
            holderToUse.setConnection(con);
        }
        holderToUse.requested();
        TransactionSynchronizationManager
                .registerSynchronization(new ConnectionSynchronization(holderToUse, dataSource));
        holderToUse.setSynchronizedWithTransaction(true);
        if (holderToUse != conHolder) {
            TransactionSynchronizationManager.bindResource(dataSource, holderToUse);
        }
    }

    return con;
}

From source file:org.springframework.jdbc.datasource.DataSourceUtils.java

/**
 * Determine whether the given two Connections are equal, asking the target
 * Connection in case of a proxy. Used to detect equality even if the
 * user passed in a raw target Connection while the held one is a proxy.
 * @param conHolder the ConnectionHolder for the held Connection (potentially a proxy)
 * @param passedInCon the Connection passed-in by the user
 * (potentially a target Connection without proxy)
 * @return whether the given Connections are equal
 * @see #getTargetConnection/*from  w  w  w .  java 2  s. c om*/
 */
private static boolean connectionEquals(ConnectionHolder conHolder, Connection passedInCon) {
    if (!conHolder.hasConnection()) {
        return false;
    }
    Connection heldCon = conHolder.getConnection();
    // Explicitly check for identity too: for Connection handles that do not implement
    // "equals" properly, such as the ones Commons DBCP exposes).
    return (heldCon == passedInCon || heldCon.equals(passedInCon)
            || getTargetConnection(heldCon).equals(passedInCon));
}