class JcrInterceptor extends JcrAccessor implements MethodInterceptor {

    public Object invoke(MethodInvocation methodInvocation) throws Throwable {
        boolean existingTransaction = false;
        Session session = SessionFactoryUtils.getSession(getSessionFactory(), true);
        if (TransactionSynchronizationManager.hasResource(getSessionFactory())) {
            LOG.debug("Found thread-bound Session for JCR interceptor");
            existingTransaction = true;
        } else {
            LOG.debug("Using new Session for JCR interceptor");
            TransactionSynchronizationManager.bindResource(getSessionFactory(), getSessionFactory().getSessionHolder(session));
        }
        try {
            Object retVal = methodInvocation.proceed();
            // flushIfNecessary(session, existingTransaction);
            return retVal;
        } finally {
            if (existingTransaction) {
                LOG.debug("Not closing pre-bound JCR Session after interceptor");
            } else {
                TransactionSynchronizationManager.unbindResource(getSessionFactory());
                SessionFactoryUtils.releaseSession(session, getSessionFactory());
            }
        }
    }

}