Spring websocket OpenSessionInviewWinterCeptor Spring WebSocket

Spring websocket OpenSessionInviewWinterCeptor Spring WebSocket,spring-websocket,spring-orm,Spring Websocket,Spring Orm,有人在SpringWebSocket消息上实现了OpenSessionInView吗?我需要实现这一点,并希望一些现有的材料 我创建了一些测试内容,如下面所示,但我遇到了TransactionException,可能是因为当您发回回复时,您的会话需要同时处理发送和接收消息 public class OpenSessionInViewChannelInterceptor extends ChannelInterceptorAdapter{ @Inject private Sess

有人在SpringWebSocket消息上实现了OpenSessionInView吗?我需要实现这一点,并希望一些现有的材料

我创建了一些测试内容,如下面所示,但我遇到了TransactionException,可能是因为当您发回回复时,您的会话需要同时处理发送和接收消息

public class OpenSessionInViewChannelInterceptor extends ChannelInterceptorAdapter{

    @Inject
    private SessionFactory sessionFactory;

    @Override
    public Message<?> preSend(Message<?> message, MessageChannel channel) {
        SessionFactory sf = sessionFactory;
        if (!TransactionSynchronizationManager.hasResource(sf)) {
            // New Session to be bound for the current method's scope...
            Session session = openSession();
            try {
                TransactionSynchronizationManager.bindResource(sf, new SessionHolder(session));
                return super.preSend(message, channel);
            }
            finally {
                SessionFactoryUtils.closeSession(session);
                TransactionSynchronizationManager.unbindResource(sf);
            }
        }
        else {
            // Pre-bound Session found -> simply proceed.
            return super.preSend(message, channel);
        }       

    }

    protected Session openSession() throws DataAccessResourceFailureException {
        try {
            Session session = sessionFactory.openSession();
            session.setFlushMode(FlushMode.MANUAL);
            return session;
        }
        catch (HibernateException ex) {
            throw new DataAccessResourceFailureException("Could not open Hibernate Session", ex);
        }
    }

}

是的,我创造了这个。我现在的解决方法是在控制器上使用“@Transactional”,这很难看,在ListenableFuture场景中不起作用(还没有测试),这是您想要的“@MessageMapping,也是Spring使用反应式方法的方向
[MSA] DEBUG [2016-06-21T18:40:05,054] SimpAnnotationMethodMessageHandler.processHandlerMethodException(468) | Searching methods to handle HibernateException
[MSA] ERROR [2016-06-21T18:40:05,057] SimpAnnotationMethodMessageHandler.processHandlerMethodException(478) | Unhandled exception
org.hibernate.HibernateException: Could not obtain transaction-synchronized Session for current thread
    at org.springframework.orm.hibernate4.SpringSessionContext.currentSession(SpringSessionContext.java:134) ~[spring-orm-4.1.9.RELEASE.jar:4.1.9.RELEASE]
    at org.hibernate.internal.SessionFactoryImpl.getCurrentSession(SessionFactoryImpl.java:1014) ~[hibernate-core-4.3.11.Final.jar:4.3.11.Final]
    at org.appfuse.dao.hibernate.GenericDaoHibernate.flush(GenericDaoHibernate.java:165) ~[classes/:?]
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_60]
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[?:1.8.0_60]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:1.8.0_60]
    at java.lang.reflect.Method.invoke(Method.java:497) ~[?:1.8.0_60]
    at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:317) ~[spring-aop-4.1.9.RELEASE.jar:4.1.9.RELEASE]
    at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:190) ~[spring-aop-4.1.9.RELEASE.jar:4.1.9.RELEASE]
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157) ~[spring-aop-4.1.9.RELEASE.jar:4.1.9.RELEASE]
    at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:136) ~[spring-tx-4.1.9.RELEASE.jar:4.1.9.RELEASE]
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) ~[spring-aop-4.1.9.RELEASE.jar:4.1.9.RELEASE]
    at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:207) ~[spring-aop-4.1.9.RELEASE.jar:4.1.9.RELEASE]