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

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

Introduction

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

Prototype

public boolean isSynchronizedWithTransaction() 

Source Link

Document

Return whether the resource is synchronized with a transaction.

Usage

From source file:org.dalesbred.integration.spring.SpringTransactionManager.java

@Override
public boolean hasActiveTransaction() {
    ConnectionHolder conHolder = (ConnectionHolder) TransactionSynchronizationManager.getResource(dataSource);
    return conHolder != null
            && (conHolder.getConnectionHandle() != null || conHolder.isSynchronizedWithTransaction());
}

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

/**
 * Looks for a current Spring managed transaction and wraps/returns that as a Ebean transaction.
 * <p>/*w ww. j  a  va2s . c om*/
 * Returns null if there is no current spring transaction (lazy loading outside a spring txn etc).
 * </p>
 */
public Object getCurrentTransaction() {

    // Get the current Spring ConnectionHolder associated to the current spring managed transaction
    ConnectionHolder holder = (ConnectionHolder) TransactionSynchronizationManager.getResource(dataSource);

    if (holder == null || !holder.isSynchronizedWithTransaction()) {
        // no current Spring transaction
        SpiTransaction currentEbeanTransaction = DefaultTransactionThreadLocal.get(serverName);
        if (currentEbeanTransaction != null) {
            // NOT expecting this so log WARNING
            String msg = "SpringTransaction - no current spring txn BUT using current Ebean one "
                    + currentEbeanTransaction.getId();
            logger.log(Level.WARNING, msg);

        } else if (logger.isLoggable(Level.FINEST)) {
            logger.log(Level.FINEST, "Spring Txn - no current transaction ");
        }
        return currentEbeanTransaction;
    }

    SpringTxnListener springTxnLister = getSpringTxnListener();

    if (springTxnLister != null) {
        // we have already seen this transaction 
        return springTxnLister.getTransaction();

    } else {
        // This is a new spring transaction that we have not seen before.
        // "wrap" it in a SpringJdbcTransaction for use with Ebean 
        SpringJdbcTransaction newTrans = new SpringJdbcTransaction(holder, transactionManager);

        // Create and register a Spring TransactionSynchronization for this transaction
        springTxnLister = createSpringTxnListener(newTrans);
        TransactionSynchronizationManager.registerSynchronization(springTxnLister);

        // also put in Ebean ThreadLocal
        DefaultTransactionThreadLocal.set(serverName, newTrans);
        return newTrans;
    }
}

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 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;
}