Example usage for org.springframework.data.neo4j.transaction SessionHolder SessionHolder

List of usage examples for org.springframework.data.neo4j.transaction SessionHolder SessionHolder

Introduction

In this page you can find the example usage for org.springframework.data.neo4j.transaction SessionHolder SessionHolder.

Prototype

public SessionHolder(Session session) 

Source Link

Usage

From source file:org.springframework.data.neo4j.transaction.SessionFactoryUtils.java

public static Session getSession(SessionFactory sessionFactory) throws IllegalStateException {

    Assert.notNull(sessionFactory, "No SessionFactory specified");

    SessionHolder sessionHolder = (SessionHolder) TransactionSynchronizationManager.getResource(sessionFactory);
    if (sessionHolder != null) {
        if (!sessionHolder.isSynchronizedWithTransaction()
                && TransactionSynchronizationManager.isSynchronizationActive()) {
            sessionHolder.setSynchronizedWithTransaction(true);
            TransactionSynchronizationManager
                    .registerSynchronization(new SessionSynchronization(sessionHolder, sessionFactory, false));
        }//w  w w.j a  v  a  2  s  .  co m
        return sessionHolder.getSession();
    }

    if (!TransactionSynchronizationManager.isSynchronizationActive()) {
        return null;
    }

    Session session = sessionFactory.openSession();

    if (TransactionSynchronizationManager.isCurrentTransactionReadOnly()) {
        System.out.println("hitting read only txn.");
    }

    logger.debug("Registering transaction synchronization for Neo4j Session");
    // Use same Session for further Neo4j actions within the transaction.
    // Thread object will get removed by synchronization at transaction completion.

    sessionHolder = new SessionHolder(session);
    sessionHolder.setSynchronizedWithTransaction(true);
    TransactionSynchronizationManager
            .registerSynchronization(new SessionSynchronization(sessionHolder, sessionFactory, true));
    TransactionSynchronizationManager.bindResource(sessionFactory, sessionHolder);

    return session;
}

From source file:org.springframework.data.neo4j.web.support.OpenSessionInViewInterceptor.java

@Override
public void preHandle(WebRequest request) throws DataAccessException {
    String participateAttributeName = getParticipateAttributeName();

    WebAsyncManager asyncManager = WebAsyncUtils.getAsyncManager(request);
    if (asyncManager.hasConcurrentResult()) {
        if (applyCallableInterceptor(asyncManager, participateAttributeName)) {
            return;
        }//w  w w .j av a 2 s  . c  o  m
    }

    if (TransactionSynchronizationManager.hasResource(getSessionFactory())) {
        // Do not modify the Session: just mark the request accordingly.
        Integer count = (Integer) request.getAttribute(participateAttributeName, WebRequest.SCOPE_REQUEST);
        int newCount = (count != null ? count + 1 : 1);
        request.setAttribute(getParticipateAttributeName(), newCount, WebRequest.SCOPE_REQUEST);
    } else {
        logger.debug("Opening Neo4j OGM Session in OpenSessionInViewInterceptor");
        Session session = sessionFactory.openSession();
        SessionHolder sessionHolder = new SessionHolder(session);
        TransactionSynchronizationManager.bindResource(getSessionFactory(), sessionHolder);

        AsyncRequestInterceptor interceptor = new AsyncRequestInterceptor(getSessionFactory(), sessionHolder);
        asyncManager.registerCallableInterceptor(participateAttributeName, interceptor);
        asyncManager.registerDeferredResultInterceptor(participateAttributeName, interceptor);
    }
}