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

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

Introduction

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

Prototype

public Connection getConnection() 

Source Link

Document

Return the current Connection held by this ConnectionHolder.

Usage

From source file:com.avaje.ebean.springsupport.txn.SpringJdbcTransaction.java

public SpringJdbcTransaction(ConnectionHolder holder, TransactionManager manager) {
    super("s" + holder.hashCode(), true, holder.getConnection(), manager);
    this.holder = holder;
}

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//  w w  w  .j  a v  a2s .  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   www . ja  va  2 s.c o m
 */
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));
}

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

/**
 * This implementation rolls back to the given JDBC 3.0 Savepoint.
 * @see java.sql.Connection#rollback(java.sql.Savepoint)
 *//*from  w w  w  .j  av a  2 s .co m*/
@Override
public void rollbackToSavepoint(Object savepoint) throws TransactionException {
    ConnectionHolder conHolder = getConnectionHolderForSavepoint();
    try {
        conHolder.getConnection().rollback((Savepoint) savepoint);
        conHolder.resetRollbackOnly();
    } catch (Throwable ex) {
        throw new TransactionSystemException("Could not roll back to JDBC savepoint", ex);
    }
}

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

/**
 * This implementation releases the given JDBC 3.0 Savepoint.
 * @see java.sql.Connection#releaseSavepoint
 *//*from  w  ww .java 2  s .co  m*/
@Override
public void releaseSavepoint(Object savepoint) throws TransactionException {
    ConnectionHolder conHolder = getConnectionHolderForSavepoint();
    try {
        conHolder.getConnection().releaseSavepoint((Savepoint) savepoint);
    } catch (Throwable ex) {
        logger.debug("Could not explicitly release JDBC savepoint", ex);
    }
}