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.grails.datastore.gorm.support.AbstractDatastorePersistenceContextInterceptor.java

public void init() {

    final SessionHolder sessionHolder = (SessionHolder) TransactionSynchronizationManager
            .getResource(datastore);//from   w w  w . ja va 2  s  . c  o m
    if (sessionHolder == null) {
        LOG.debug("Opening single Datastore session in DatastorePersistenceContextInterceptor");
        Session session = getSession();
        session.setFlushMode(FlushModeType.AUTO);
        try {
            DatastoreUtils.bindSession(session, this);
        } catch (IllegalStateException e) {
            // ignore, already bound
        }
    }
}

From source file:org.grails.datastore.gorm.support.AbstractDatastorePersistenceContextInterceptor.java

public void destroy() {
    // single session mode
    final SessionHolder sessionHolder = (SessionHolder) TransactionSynchronizationManager
            .getResource(datastore);//from   ww w  .  ja v a  2s.  c o m
    if (sessionHolder != null && this == sessionHolder.getCreator()) {
        SessionHolder holder = (SessionHolder) TransactionSynchronizationManager.unbindResource(datastore);
        LOG.debug("Closing single Datastore session in DatastorePersistenceContextInterceptor");
        try {
            Session session = holder.getSession();
            DatastoreUtils.closeSession(session);
        } catch (RuntimeException ex) {
            LOG.error("Unexpected exception on closing Datastore Session", ex);
        }
    }
}

From source file:org.grails.orm.hibernate4.support.HibernatePersistenceContextInterceptor.java

private Session getSession(boolean allowCreate) {

    Object value = TransactionSynchronizationManager.getResource(getSessionFactory());
    if (value instanceof Session) {
        return (Session) value;
    }//from  ww  w . j av  a  2s  .c  om

    if (value instanceof SessionHolder) {
        SessionHolder sessionHolder = (SessionHolder) value;
        return sessionHolder.getSession();
    }

    if (allowCreate && hibernateDatastore != null) {
        return hibernateDatastore.openSession();
    }

    throw new IllegalStateException(
            "No Hibernate Session bound to thread, and configuration does not allow creation of non-transactional one here");
}

From source file:org.opoo.oqs.spring.jdbc.TransactionSupportConnectionManager.java

public Connection getConnection() throws DataAccessException {
    if (dataSource == null) {
        throw new IllegalArgumentException("No DataSource specified");
    }//from  w  w  w  . j  a  v  a 2  s  .  c  om

    Connection conn = (Connection) TransactionSynchronizationManager.getResource(dataSource);
    if (conn != null) {
        log.debug("Get connection from ThreadLocal.");
    } else { //(conn == null)
        conn = DataSourceUtils.getConnection(dataSource);
    }
    return conn;
}

From source file:org.opoo.oqs.spring.jdbc.TransactionSupportConnectionManager.java

public void releaseConnection(Connection con) {
    if (dataSource == null) {
        throw new IllegalArgumentException("No DataSource specified");
    }/*from  w  w w  .  j a  va 2s  . c o m*/
    Connection conn = (Connection) TransactionSynchronizationManager.getResource(dataSource);
    if (conn != null && (con == conn || conn.equals(con))) {
        log.debug("Connection in Transaction, do not close.");
    } else {
        JdbcUtils.closeConnection(con);
    }
}

From source file:org.osaf.cosmo.scheduler.HibernateSessionFilter.java

private void releaseSession() {
    log.debug("unbinding session to thread");

    // Unbind session from TransactionManager and close
    SessionHolder holder = (SessionHolder) TransactionSynchronizationManager.getResource(sessionFactory);
    Session s = holder.getSession();//  w  ww  .  ja  v a2  s  .  com
    TransactionSynchronizationManager.unbindResource(sessionFactory);
    SessionFactoryUtils.closeSession(s);
}

From source file:org.seedstack.spring.internal.SpringEntityManagerLink.java

private EntityManager getEntityManager(Map<String, EntityManagerFactory> mapEntityFactories) {
    if (mapEntityFactories.isEmpty()) {
        throw SeedException.createNew(SpringErrorCode.NO_SPRING_ENTITYMANAGER).put("class", currentClass);
    }/*  ww w  .  ja  v  a2  s .  c  o  m*/

    EntityManager entityManager;
    String unitFromClass = configurationProvider.getConfiguration(currentClass).getString(JPA_UNIT_PROPERTY);
    if (StringUtils.isBlank(unitFromClass)) {
        if (mapEntityFactories.size() > 1) {
            throw SeedException.createNew(SpringErrorCode.AMBIGUOUS_SPRING_ENTITYMANAGER)
                    .put("class", currentClass.getName())
                    .put("currentTransaction", TransactionSynchronizationManager.getCurrentTransactionName());
        } else {
            entityManager = ((EntityManagerHolder) TransactionSynchronizationManager
                    .getResource(mapEntityFactories.entrySet().iterator().next().getValue()))
                            .getEntityManager();
        }
    } else {
        EntityManagerFactory entityManagerFactory = mapEntityFactories.get(unitFromClass);
        if (entityManagerFactory == null) {
            throw SeedException.createNew(SpringErrorCode.UNKNOWN_SPRING_ENTITYMANAGER)
                    .put("class", currentClass).put("unit", unitFromClass)
                    .put("currentTransaction", TransactionSynchronizationManager.getCurrentTransactionName());
        }
        entityManager = ((EntityManagerHolder) TransactionSynchronizationManager
                .getResource(entityManagerFactory)).getEntityManager();
    }

    return entityManager;
}

From source file:org.springframework.amqp.rabbit.connection.ConnectionFactoryUtils.java

/**
 * Determine whether the given RabbitMQ Channel is transactional, that is,
 * bound to the current thread by Spring's transaction facilities.
 * /*from   ww  w . j  av  a2 s. co m*/
 * @param channel
 *            the RabbitMQ Channel to check
 * @param connectionFactory
 *            the RabbitMQ ConnectionFactory that the Channel originated
 *            from
 * @return whether the Channel is transactional
 */
public static boolean isChannelTransactional(Channel channel, ConnectionFactory connectionFactory) {
    if (channel == null || connectionFactory == null) {
        return false;
    }
    RabbitResourceHolder resourceHolder = (RabbitResourceHolder) TransactionSynchronizationManager
            .getResource(connectionFactory);
    return (resourceHolder != null && resourceHolder.containsChannel(channel));
}

From source file:org.springframework.amqp.rabbit.connection.ConnectionFactoryUtils.java

/**
 * Obtain a RabbitMQ Channel that is synchronized with the current
 * transaction, if any.//from w  ww. jav a2s  .co  m
 * 
 * @param connectionFactory
 *            the RabbitMQ ConnectionFactory to bind for (used as
 *            TransactionSynchronizationManager key)
 * @param resourceFactory
 *            the ResourceFactory to use for extracting or creating RabbitMQ
 *            resources
 * @return the transactional Channel, or <code>null</code> if none found
 */
private static RabbitResourceHolder doGetTransactionalResourceHolder(ConnectionFactory connectionFactory,
        ResourceFactory resourceFactory) {

    Assert.notNull(connectionFactory, "ConnectionFactory must not be null");
    Assert.notNull(resourceFactory, "ResourceFactory must not be null");

    RabbitResourceHolder resourceHolder = (RabbitResourceHolder) TransactionSynchronizationManager
            .getResource(connectionFactory);
    if (resourceHolder != null) {
        Channel channel = resourceFactory.getChannel(resourceHolder);
        if (channel != null) {
            return resourceHolder;
        }
    }
    RabbitResourceHolder resourceHolderToUse = resourceHolder;
    if (resourceHolderToUse == null) {
        resourceHolderToUse = new RabbitResourceHolder();
    }
    Connection connection = resourceFactory.getConnection(resourceHolderToUse);
    Channel channel = null;
    try {
        boolean isExistingCon = (connection != null);
        if (!isExistingCon) {
            connection = resourceFactory.createConnection();
            resourceHolderToUse.addConnection(connection);
        }
        channel = consumerChannel.get();
        if (channel == null) {
            channel = resourceFactory.createChannel(connection);
        }
        if (null != channel && channel.isOpen()) {
            resourceHolderToUse.addChannel(channel, connection);
        }

        if (resourceHolderToUse != resourceHolder) {
            bindResourceToTransaction(resourceHolderToUse, connectionFactory,
                    resourceFactory.isSynchedLocalTransactionAllowed());
        }

        return resourceHolderToUse;

    } catch (IOException ex) {
        RabbitUtils.closeChannel(channel);
        RabbitUtils.closeConnection(connection);
        throw new AmqpIOException(ex);
    }
}

From source file:org.springframework.amqp.rabbit.connection.ConnectionFactoryUtils.java

/**
 *
 *//* www.  j  a va2  s .c  o m*/
public static void registerDeliveryTag(ConnectionFactory connectionFactory, Channel channel, Long tag)
        throws IOException {

    Assert.notNull(connectionFactory, "ConnectionFactory must not be null");

    RabbitResourceHolder resourceHolder = (RabbitResourceHolder) TransactionSynchronizationManager
            .getResource(connectionFactory);
    if (resourceHolder != null) {
        resourceHolder.addDeliveryTag(channel, tag);
    }
}