0

有时在生产中我们会遇到下一个错误:

2013-04-18 20:47:50,199 http-0.0.0.0-8443-35 ERROR [Jersey Web Application]] [userId: 57916] Servlet.service() for servlet Jersey Web Application threw exception
java.lang.IllegalStateException: Already value [org.springframework.orm.hibernate3.SessionHolder@4cccafeb] for key [org.hibernate.impl.SessionFactoryImpl@1457e272] bound to thread [http-0.0.0.0-8443-35]
    at org.springframework.transaction.support.TransactionSynchronizationManager.bindResource(TransactionSynchronizationManager.java:189)       

对于 Jersey RESTFull 服务的每个请求,我们都有用于打开会话的 Hibernate 会话过滤器。

com.mycompany.SessionPerRequestHelper {
    public static Session openSession() {
        SessionFactory sessionFactory = getSessionFactory();

        Session sess = sessionFactory.openSession();
        sessThreadLocal.set(sess);

        TransactionSynchronizationManager.bindResource(sessionFactory, new SessionHolder(sess));
        TransactionSynchronizationManager.initSynchronization();

        return sess;
    }
}

休眠上下文配置:

<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="configLocation" value="classpath:com/transportexchangegroup/domain/hibernate.cfg.xml" />
        <property name="configurationClass" value="org.hibernate.cfg.AnnotationConfiguration" />
    </bean>

    <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory" />
    </bean>

和hibernate.cfg

<hibernate-configuration>
    <session-factory>
        <property name="dialect">org.hibernatespatial.postgis.PostgisDialect</property>
        <property name="transaction.factory_class">org.hibernate.transaction.JDBCTransactionFactory</property>
        <property name="current_session_context_class">thread</property>
    </session-factory>
</hibernate-configuration>
4

1 回答 1

0

最好使用OpenSessionInViewFilter而不是您的SessionPerRequestHelper.

于 2013-05-09T09:05:46.760 回答