Example usage for org.springframework.transaction.support TransactionSynchronizationManager getResource

List of usage examples for org.springframework.transaction.support TransactionSynchronizationManager getResource

Introduction

In this page you can find the example usage for org.springframework.transaction.support TransactionSynchronizationManager getResource.

Prototype

@Nullable
public static Object getResource(Object key) 

Source Link

Document

Retrieve a resource for the given key that is bound to the current thread.

Usage

From source file:org.mybatis.spring.SqlSessionUtils.java

/**
 * Returns if the {@code SqlSession} passed as an argument is being managed by Spring
 *
 * @param session a MyBatis SqlSession to check
 * @param sessionFactory the SqlSessionFactory which the SqlSession was built with
 * @return true if session is transactional, otherwise false
 *//* ww  w . java 2  s  .co m*/
public static boolean isSqlSessionTransactional(SqlSession session, SqlSessionFactory sessionFactory) {
    notNull(session, NO_SQL_SESSION_SPECIFIED);
    notNull(sessionFactory, NO_SQL_SESSION_FACTORY_SPECIFIED);

    SqlSessionHolder holder = (SqlSessionHolder) TransactionSynchronizationManager.getResource(sessionFactory);

    return (holder != null) && (holder.getSqlSession() == session);
}

From source file:org.hengdao.utils.SqlSessionUtils.java

/**
 * Returns if the {@code SqlSession} passed as an argument is being managed
 * by Spring/* w  w w.jav a2  s  . c  o  m*/
 *
 * @param session
 *            a MyBatis SqlSession to check
 * @param sessionFactory
 *            the SqlSessionFactory which the SqlSession was built with
 * @return true if session is transactional, otherwise false
 */
public static boolean isSqlSessionTransactional(SqlSession session, SqlSessionFactory sessionFactory) {
    Assert.notNull(session, "No SqlSession specified");
    Assert.notNull(sessionFactory, "No SqlSessionFactory specified");

    SqlSessionHolder holder = (SqlSessionHolder) TransactionSynchronizationManager.getResource(sessionFactory);

    return (holder != null) && (holder.getSqlSession() == session);
}

From source file:org.guzz.web.context.spring.TransactionManagerUtils.java

/**
 * Return whether there is a transactional Guzz WriteTranSession for the current thread,
 * that is, a Session bound to the current thread by Spring's transaction facilities.
 * @param transactionManager Guzz TransactionManager to check (may be <code>null</code>)
 * @return whether there is a transactional Session for current thread
 *//*from   w  w  w.java2  s . co  m*/
public static boolean hasTransactionalSession(TransactionManager transactionManager) {
    if (transactionManager == null) {
        return false;
    }

    WriteTranSessionHolder writeTranSessionHolder = (WriteTranSessionHolder) TransactionSynchronizationManager
            .getResource(transactionManager);

    return (writeTranSessionHolder != null && !writeTranSessionHolder.isEmpty());
}

From source file:org.cfr.capsicum.datasource.DataSourceUtils.java

/**
 * Determine whether the given JDBC Connection is transactional, that is,
 * bound to the current thread by Spring's transaction facilities.
 * @param con the Connection to check//from  w ww .  ja v  a  2 s  .c o m
 * @param dataSource the DataSource that the Connection was obtained from
 * (may be <code>null</code>)
 * @return whether the Connection is transactional
 */
public static boolean isConnectionTransactional(Connection con, DataSource dataSource) {
    if (dataSource == null) {
        return false;
    }
    CayenneConnectionHolder conHolder = (CayenneConnectionHolder) TransactionSynchronizationManager
            .getResource(dataSource);
    return conHolder != null && connectionEquals(conHolder, con);
}

From source file:com._4dconcept.springframework.data.marklogic.datasource.ContentSourceUtils.java

/**
 * Apply the specified timeout - overridden by the current transaction timeout,
 * if any - to the given XDBC Statement object.
 * @param ses the XDBC Statement object/* w  w w .  jav  a2  s  .c  o m*/
 * @param contentSource the ContentSource that the Session was obtained from
 * @param timeout the timeout to apply (or 0 for no timeout outside of a transaction)
 * @throws XccException if thrown by XDBC methods
 * @see Session#setTransactionTimeout
 */
public static void applyTimeout(Session ses, ContentSource contentSource, int timeout) throws XccException {
    Assert.notNull(ses, "No Statement specified");
    Assert.notNull(contentSource, "No ContentSource specified");
    SessionHolder holder = (SessionHolder) TransactionSynchronizationManager.getResource(contentSource);
    if (holder != null && holder.hasTimeout()) {
        // Remaining transaction timeout overrides specified value.
        ses.setTransactionTimeout(holder.getTimeToLiveInSeconds());
    } else if (timeout >= 0) {
        // No current transaction timeout -> apply specified value.
        ses.setTransactionTimeout(timeout);
    }
}

From source file:com.github.rholder.spring.transaction.TransactionBindingSupport.java

/**
 * Binds the Alfresco-specific to the transaction resources
 * //from  ww  w . java2  s.  c  o m
 * @return Returns the current or new synchronization implementation
 */
private static TransactionSynchronizationImpl registerSynchronizations() {
    /*
     * No thread synchronization or locking required as the resources are all threadlocal
     */
    if (!TransactionSynchronizationManager.isSynchronizationActive()) {
        Thread currentThread = Thread.currentThread();
        throw new RuntimeException(
                "Transaction must be active and synchronization is required: " + currentThread);
    }
    TransactionSynchronizationImpl txnSynch = (TransactionSynchronizationImpl) TransactionSynchronizationManager
            .getResource(RESOURCE_KEY_TXN_SYNCH);
    if (txnSynch != null) {
        // synchronization already registered
        return txnSynch;
    }
    // we need a unique ID for the transaction
    String txnId = UUID.randomUUID().toString();
    // register the synchronization
    txnSynch = new TransactionSynchronizationImpl(txnId);
    TransactionSynchronizationManager.registerSynchronization(txnSynch);
    // register the resource that will ensure we don't duplication the synchronization
    TransactionSynchronizationManager.bindResource(RESOURCE_KEY_TXN_SYNCH, txnSynch);
    // done
    if (logger.isDebugEnabled()) {
        logger.debug("Bound txn synch: " + txnSynch);
    }
    return txnSynch;
}

From source file:org.guzz.web.context.spring.TransactionManagerUtils.java

/**
 * Return whether the given Guzz WriteTranSession is transactional, that is,
 * bound to the current thread by Spring's transaction facilities.
 * @param session the Guzz WriteTranSession to check
 * @param transactionManager Guzz TransactionManager that the Session was created with
 * (may be <code>null</code>)
 * @return whether the Session is transactional
 *//*from  w w w  .  j a v  a  2 s  .  c  o  m*/
public static boolean isSessionTransactional(WriteTranSession session, TransactionManager transactionManager) {
    if (transactionManager == null) {
        return false;
    }

    WriteTranSessionHolder writeTranSessionHolder = (WriteTranSessionHolder) TransactionSynchronizationManager
            .getResource(transactionManager);

    return (writeTranSessionHolder != null && writeTranSessionHolder.getWriteTranSession() == session);
}

From source file:org.cfr.capsicum.datasource.DataSourceUtils.java

/**
 * Apply the specified timeout - overridden by the current transaction timeout,
 * if any - to the given JDBC Statement object.
 * @param stmt the JDBC Statement object
 * @param dataSource the DataSource that the Connection was obtained from
 * @param timeout the timeout to apply (or 0 for no timeout outside of a transaction)
 * @throws SQLException if thrown by JDBC methods
 * @see java.sql.Statement#setQueryTimeout
 *///w  ww . j a v  a  2s  .  c  om
public static void applyTimeout(Statement stmt, DataSource dataSource, int timeout) throws SQLException {
    Assert.notNull(stmt, "No Statement specified");
    Assert.notNull(dataSource, "No DataSource specified");
    ConnectionHolder holder = (ConnectionHolder) TransactionSynchronizationManager.getResource(dataSource);
    if (holder != null && holder.hasTimeout()) {
        // Remaining transaction timeout overrides specified value.
        stmt.setQueryTimeout(holder.getTimeToLiveInSeconds());
    } else if (timeout > 0) {
        // No current transaction timeout -> apply specified value.
        stmt.setQueryTimeout(timeout);
    }
}

From source file:org.guzz.web.context.spring.TransactionManagerUtils.java

/**
 * Apply the current transaction timeout, if any, to the given
 * Guzz Query object.//from  ww  w .  jav  a 2s .  c  om
 * @param query the Guzz Query object
 * @param transactionManager Guzz TransactionManager that the Query was created for
 * (may be <code>null</code>)
 * @see org.hibernate.Query#setTimeout
 */
public static void applyTransactionTimeout(PreparedStatement pstm, TransactionManager transactionManager) {
    Assert.notNull(pstm, "No PreparedStatement object specified");
    if (transactionManager != null) {
        WriteTranSessionHolder writeTranSessionHolder = (WriteTranSessionHolder) TransactionSynchronizationManager
                .getResource(transactionManager);

        if (writeTranSessionHolder != null && writeTranSessionHolder.hasTimeout()) {
            try {
                pstm.setQueryTimeout(writeTranSessionHolder.getTimeToLiveInSeconds());
            } catch (SQLException e) {
                throw new DataAccessResourceFailureException(e.getMessage(), e);
            }
        }
    }
}

From source file:org.apacheextras.camel.component.wmq.WMQProducer.java

/**
 * {@inheritDoc}/*from   w  w w . j  ava2 s .  co  m*/
 * 
 * Process a message in the following way:
 *   Get the queue manager for this transaction
 *   Create a connection to a destination
 *   Populate headers
 *   Send message
 *   Close connection
 */
public void process(Exchange exchange) throws Exception {

    LOGGER.trace("Get the MQQueueManager for this transaction");
    MQQueueManager manager = (MQQueueManager) TransactionSynchronizationManager.getResource("queueManager");
    String id = (String) TransactionSynchronizationManager.getResource("id");

    LOGGER.debug("Producer transaction started with id " + id + " and mananger " + manager.toString()
            + " on thread " + Thread.currentThread().getId());

    Message in = exchange.getIn();

    LOGGER.trace("Accessing to MQQueue {}", endpoint.getDestinationName());
    int MQOO = MQConstants.MQOO_OUTPUT;
    if (in.getHeader("MQOO") != null) {
        LOGGER.trace("MQOO defined to {}", in.getHeader("MQOO"));
        MQOO = (Integer) in.getHeader("MQOO");
    }

    MQDestination destination = null;
    try {
        destination = wmqUtilities.accessDestination(getEndpoint().getDestinationName(), MQOO, manager);

        LOGGER.trace("Creating MQMessage");
        MQMessage message = new MQMessage();
        populateHeaders(in, message);

        LOGGER.trace("Set message segmentation to on based on segmentation settings");
        if (isSegmented()) {
            message.messageFlags = MQConstants.MQMF_SEGMENTATION_ALLOWED;
            message.groupId = null;
        }

        message.writeString(in.getBody(String.class));

        LOGGER.trace("Putting the message ...");
        MQPutMessageOptions putOptions = createPutMessageOptions(in);
        if (putOptions != null) {
            LOGGER.trace("PutOptions are present");
            destination.put(message, putOptions);
        } else {
            destination.put(message);
        }
        LOGGER.debug("Producer transaction finished with id " + id + " and mananger " + manager.toString()
                + " on thread " + Thread.currentThread().getId());
        destination.close();
    } finally {
        if (destination != null) {
            destination.close();
        }
    }
}