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.openvpms.component.business.service.archetype.Notifier.java

/**
 * Returns the notifier for the given service and current thread.
 * <p/>//from w w w  .jav a  2  s. c o  m
 * If one does not exist, it will be created.
 *
 * @param service the archetype service
 * @return the context
 */
public static Notifier getNotifier(ArchetypeService service) {
    Notifier notifier;
    if (TransactionSynchronizationManager.isSynchronizationActive()) {
        if (!TransactionSynchronizationManager.hasResource(service)) {
            notifier = new Notifier(service, true);
            TransactionSynchronizationManager.bindResource(service, notifier);
            TransactionSynchronizationManager.registerSynchronization(new NotifierSynchronization(notifier));
        } else {
            notifier = (Notifier) TransactionSynchronizationManager.getResource(service);
        }
    } else {
        notifier = new Notifier(service, false);
    }
    return notifier;
}

From source file:com.busimu.core.dao.impl.UserMngDaoPolicyJpaImpl.java

/**
 * {@inheritDoc}//from ww  w. jav  a 2 s . com
 */
@Override
public User authenticateUser(String userName, String passwd) throws DataValidationException {
    EntityManager em = ((EntityManagerHolder) TransactionSynchronizationManager.getResource(emf))
            .getEntityManager();
    User user = getUserByName(em, userName);
    if (user == null) {
        throw new DataValidationException("Authentication Fail - NO user called " + userName,
                DataValidationErrorCode.NONE_EXISTED_USER);
    } else if (!user.getPasswd().equals(SecurityUtil.getMD5ByString(passwd))) {
        throw new DataValidationException("Authentication Fail - Password is NOT right for " + userName,
                DataValidationErrorCode.WRONG_PASSWORD);
    } else {
        return user;
    }
}

From source file:com.busimu.core.dao.impl.UserMngDaoPolicyJpaImpl.java

/**
 * {@inheritDoc}//from  www . j  ava  2s  .c om
 */
@Override
public LoginHistory addLoginHistory(User user, Date loginDate, Date logoutDate) {
    EntityManager em = ((EntityManagerHolder) TransactionSynchronizationManager.getResource(emf))
            .getEntityManager();
    EntityTransaction tx = em.getTransaction();
    if (!tx.isActive()) {
        tx.begin();
    }
    LoginHistory history = user.addLoginHistory(loginDate, logoutDate);
    tx.commit();
    return history;
}

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

/**
 * Checks if {@code SqlSession} passed as an argument is managed by Spring {@code TransactionSynchronizationManager}
 * If it is not, it closes it, otherwise it just updates the reference counter and
 * lets Spring call the close callback when the managed transaction ends
 *
 * @param session//from w ww  . ja v  a2s. c o m
 * @param sessionFactory
 */
public static void closeSqlSession(SqlSession session, SqlSessionFactory sessionFactory) {
    notNull(session, NO_SQL_SESSION_SPECIFIED);
    notNull(sessionFactory, NO_SQL_SESSION_FACTORY_SPECIFIED);

    SqlSessionHolder holder = (SqlSessionHolder) TransactionSynchronizationManager.getResource(sessionFactory);
    if ((holder != null) && (holder.getSqlSession() == session)) {
        if (LOGGER.isDebugEnabled()) {
            LOGGER.debug("Releasing transactional SqlSession [" + session + "]");
        }
        holder.released();
    } else {
        if (LOGGER.isDebugEnabled()) {
            LOGGER.debug("Closing non transactional SqlSession [" + session + "]");
        }
        session.close();
    }
}

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

/**
 * Checks if {@code SqlSession} passed as an argument is managed by Spring
 * {@code TransactionSynchronizationManager} If it is not, it closes it,
 * otherwise it just updates the reference counter and lets Spring call the
 * close callback when the managed transaction ends
 *
 * @param session//  w w  w . j av  a2  s  . c om
 * @param sessionFactory
 */
public static void closeSqlSession(SqlSession session, SqlSessionFactory sessionFactory) {

    Assert.notNull(session, "No SqlSession specified");
    Assert.notNull(sessionFactory, "No SqlSessionFactory specified");

    SqlSessionHolder holder = (SqlSessionHolder) TransactionSynchronizationManager.getResource(sessionFactory);
    if ((holder != null) && (holder.getSqlSession() == session)) {
        if (logger.isDebugEnabled()) {
            logger.debug("Releasing transactional SqlSession [" + session + "]");
        }
        holder.released();
    } else {
        if (logger.isDebugEnabled()) {
            logger.debug("Closing no transactional SqlSession [" + session + "]");
        }
        session.close();
    }
}

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

/**
 * Determine whether the given XDBC Session is transactional, that is,
 * bound to the current thread by Spring's transaction facilities.
 * @param ses the Session to check//from  w w w  . j  av  a 2 s  .c  o m
 * @param contentSource the ContentSource that the Session was obtained from
 * (may be {@code null})
 * @return whether the Session is transactional
 */
public static boolean isSessionTransactional(Session ses, ContentSource contentSource) {
    if (contentSource == null) {
        return false;
    }
    SessionHolder sesHolder = (SessionHolder) TransactionSynchronizationManager.getResource(contentSource);
    return (sesHolder != null && sessionEquals(sesHolder, ses));
}

From source file:com.busimu.core.dao.impl.UserMngDaoPolicyJpaImpl.java

/**
 * {@inheritDoc}/*  w w  w  .j a  v a2 s. co m*/
 */
@Override
public User getUserById(String userId) {
    try {
        EntityManager em = ((EntityManagerHolder) TransactionSynchronizationManager.getResource(emf))
                .getEntityManager();
        Long id = Long.parseLong(userId);
        User user = em.find(User.class, id);
        return user;
    } catch (NumberFormatException e) {
        return null;
    }
}

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

@Override
protected Object doGetTransaction() {
    ContentSourceTransactionObject txObject = new ContentSourceTransactionObject();
    SessionHolder sesHolder = (SessionHolder) TransactionSynchronizationManager.getResource(this.contentSource);
    txObject.setSessionHolder(sesHolder, false);
    return txObject;
}

From source file:grails.orm.HibernateCriteriaBuilder.java

@Override
protected void createCriteriaInstance() {
    {/*from www  . j ava  2s .  c  o m*/
        if (TransactionSynchronizationManager.hasResource(sessionFactory)) {
            participate = true;
            hibernateSession = ((SessionHolder) TransactionSynchronizationManager.getResource(sessionFactory))
                    .getSession();
        } else {
            hibernateSession = sessionFactory.openSession();
        }

        criteria = hibernateSession.createCriteria(targetClass);
        cacheCriteriaMapping();
        criteriaMetaClass = GroovySystem.getMetaClassRegistry().getMetaClass(criteria.getClass());
    }
}

From source file:com.busimu.core.dao.impl.UserMngDaoPolicyJpaImpl.java

/**
 * {@inheritDoc}/*from w  ww.j  a  va  2  s  .c om*/
 */
@Override
public List<LoginHistory> getRealLoginHistory(User user, Campaign campaign) {
    EntityManager em = ((EntityManagerHolder) TransactionSynchronizationManager.getResource(emf))
            .getEntityManager();
    Date startDate = new Date(Long.parseLong(campaign.getMeta(MetaKey.CAMPAIGN_START_TIME)));
    List<Round> rounds = campaign.getSortedRoundsAsList();
    Date endDate = rounds.get(rounds.size() - 1).getEndDate();
    TypedQuery<LoginHistory> q = em.createQuery(
            "select lh from LoginHistory lh where lh.user = :user and lh.loginDate between :start and :end",
            LoginHistory.class);
    q.setParameter("user", user);
    q.setParameter("start", startDate, TemporalType.TIMESTAMP);
    q.setParameter("end", endDate, TemporalType.TIMESTAMP);
    return q.getResultList();
}