我正在尝试使用OpenSessionInViewFilter来避免不时出现的臭名昭着的延迟加载错误。不过,我已经花了大约一天的时间,但显然我做错了。根据我从日志文件中可以看出的内容,Spring确实报告它正在从过滤器中打开一个事务。然而它似乎也打开和关闭其他Sessions(主要是与交易一起,因为如果我[B]没有[/ B]配置了过滤器,我预计会出现这种情况。)
以下是我认为是一些相关的日志,表明Spring正在尝试 - 但未通过 - 打开单个会话以跨越单个HTTP请求:
(当我发出HTTP请求时,这是第一行)
DEBUG 2010-10-29 14:29:41,218 - Using SessionFactory 'sessionFactory' for OpenSessionInViewFilter
DEBUG 2010-10-29 14:29:41,218 - Returning cached instance of singleton bean 'sessionFactory'
DEBUG 2010-10-29 14:29:41,218 - Opening single Hibernate Session in OpenSessionInViewFilter
DEBUG 2010-10-29 14:29:41,218 - Opening Hibernate Session
DEBUG 2010-10-29 14:29:41,218 - opened session at timestamp: 12883877812
TRACE 2010-10-29 14:29:41,218 - setting flush mode to: MANUAL
TRACE 2010-10-29 14:29:41,218 - Bound value [org.springframework.orm.hibernate3.SessionHolder@7cd25883] for key [org.hibernate.impl.SessionFactoryImpl@349955ab] to thread [http-8443-exec-6]
DEBUG 2010-10-29 14:29:41,219 - Creating new transaction with name [com.xxx.dao.IssueDAO.getIssue]: PROPAGATION_REQUIRED,ISOLATION_DEFAULT,readOnly
DEBUG 2010-10-29 14:29:41,219 - opened session at timestamp: 12883877812
DEBUG 2010-10-29 14:29:41,219 - Opened new Session [org.hibernate.impl.SessionImpl@65baeab6] for Hibernate transaction
DEBUG 2010-10-29 14:29:41,219 - Preparing JDBC Connection of Hibernate Session [org.hibernate.impl.SessionImpl@65baeab6]
DEBUG 2010-10-29 14:29:41,219 - opening JDBC connection
DEBUG 2010-10-29 14:29:41,219 - Creating new JDBC DriverManager Connection to [jdbc:postgresql://localhost:5432/spdb]
DEBUG 2010-10-29 14:29:41,228 - Setting JDBC Connection [org.postgresql.jdbc3.Jdbc3Connection@528786e7] read-only
TRACE 2010-10-29 14:29:41,232 - setting flush mode to: MANUAL
DEBUG 2010-10-29 14:29:41,232 - begin
DEBUG 2010-10-29 14:29:41,233 - current autocommit status: true
DEBUG 2010-10-29 14:29:41,233 - disabling autocommit
TRACE 2010-10-29 14:29:41,233 - after transaction begin
DEBUG 2010-10-29 14:29:41,233 - Exposing Hibernate transaction as JDBC transaction [org.postgresql.jdbc3.Jdbc3Connection@528786e7]
TRACE 2010-10-29 14:29:41,233 - Bound value [org.springframework.jdbc.datasource.ConnectionHolder@41bc1a83] for key [org.springframework.jdbc.datasource.DriverManagerDataSource@173ceeb6] to thread [http-8443-exec-6]
TRACE 2010-10-29 14:29:41,233 - Bound value [org.springframework.orm.hibernate3.SessionHolder@2470b02c] for key [org.hibernate.impl.SessionFactoryImpl@22e5b6b0] to thread [http-8443-exec-6]
TRACE 2010-10-29 14:29:41,233 - Initializing transaction synchronization
TRACE 2010-10-29 14:29:41,233 - Getting transaction for [com.xxx.dao.IssueDAO.getIssue]
TRACE 2010-10-29 14:29:41,233 - Retrieved value [org.springframework.orm.hibernate3.SessionHolder@2470b02c] for key [org.hibernate.impl.SessionFactoryImpl@22e5b6b0] bound to thread [http-8443-exec-6]
... lotta东西正在加载实体
DEBUG 2010-10-29 14:29:41,241 - done entity load
TRACE 2010-10-29 14:29:41,241 - Completing transaction for [com.xxx.dao.IssueDAO.getIssue]
TRACE 2010-10-29 14:29:41,241 - Triggering beforeCommit synchronization
TRACE 2010-10-29 14:29:41,241 - Triggering beforeCompletion synchronization
DEBUG 2010-10-29 14:29:41,241 - Initiating transaction commit
DEBUG 2010-10-29 14:29:41,241 - Committing Hibernate transaction on Session [org.hibernate.impl.SessionImpl@65baeab6]
DEBUG 2010-10-29 14:29:41,241 - commit
TRACE 2010-10-29 14:29:41,241 - before transaction completion
TRACE 2010-10-29 14:29:41,241 - before transaction completion
DEBUG 2010-10-29 14:29:41,242 - re-enabling autocommit
DEBUG 2010-10-29 14:29:41,242 - committed JDBC Connection
TRACE 2010-10-29 14:29:41,242 - after transaction completion
DEBUG 2010-10-29 14:29:41,242 - transaction completed on session with on_close connection release mode; be sure to close the session to release JDBC resources!
TRACE 2010-10-29 14:29:41,242 - after transaction completion
TRACE 2010-10-29 14:29:41,242 - Triggering afterCommit synchronization
TRACE 2010-10-29 14:29:41,242 - Triggering afterCompletion synchronization
TRACE 2010-10-29 14:29:41,242 - Clearing transaction synchronization
TRACE 2010-10-29 14:29:41,242 - Removed value [org.springframework.orm.hibernate3.SessionHolder@2470b02c] for key [org.hibernate.impl.SessionFactoryImpl@22e5b6b0] from thread [http-8443-exec-6]
TRACE 2010-10-29 14:29:41,242 - Removed value [org.springframework.jdbc.datasource.ConnectionHolder@41bc1a83] for key [org.springframework.jdbc.datasource.DriverManagerDataSource@173ceeb6] from thread [http-8443-exec-6]
DEBUG 2010-10-29 14:29:41,242 - Resetting read-only flag of JDBC Connection [org.postgresql.jdbc3.Jdbc3Connection@528786e7]
DEBUG 2010-10-29 14:29:41,243 - Closing Hibernate Session [org.hibernate.impl.SessionImpl@65baeab6] after transaction
DEBUG 2010-10-29 14:29:41,243 - Closing Hibernate Session
TRACE 2010-10-29 14:29:41,243 - closing session
TRACE 2010-10-29 14:29:41,243 - performing cleanup
DEBUG 2010-10-29 14:29:41,243 - releasing JDBC connection [ (open PreparedStatements: 0, globally: 0) (open ResultSets: 0, globally: 0)]
TRACE 2010-10-29 14:29:41,243 - after transaction completion
DEBUG 2010-10-29 14:29:41,243 - transaction completed on session with on_close connection release mode; be sure to close the session to release JDBC resources!
TRACE 2010-10-29 14:29:41,243 - after transaction completion
DEBUG 2010-10-29 14:29:41,243 - Creating new transaction with name [com.xxx.dao.SPProjectDAO.getAvailableActiveProjects]: PROPAGATION_REQUIRED,ISOLATION_DEFAULT,readOnly
DEBUG 2010-10-29 14:29:41,244 - opened session at timestamp: 12883877812
(请参阅下一次DAO通话开始时如何打开另一个会话)
我已经配置了我的web.xml(只显示过滤器内容):
<filter>
<filter-name>hibernateFilter</filter-name>
<filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
<init-param>
<param-name>sessionFactoryBeanName</param-name>
<param-value>sessionFactory</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>hibernateFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/classes/com/xxx/biz/config/spring-biz-context.xml</param-value>
</context-param>
和我的spring-biz-context.xml:
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="mappingResources">
<list>
... hbm files here...
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</prop>
<prop key="hibernate.show.sql">true</prop>
<prop key="hibernate.show_sql">true</prop>
<!-- prop key="current_session_context_class">thread</prop-->
<!--prop key="hibernate.current_session_context_class">org.hibernate.context.ThreadLocalSessionContext</prop-->
<!--prop key="hibernate.current_session_context_class">org.hibernate.context.ManagedSessionContext</prop-->
<prop key="hibernate.connection.release_mode">auto</prop>
<prop key="hibernate.transaction.auto_close_session">false</prop>
</props>
</property>
</bean>
<bean id="hibernateTemplate"
class="org.springframework.orm.hibernate3.HibernateTemplate">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="dataSource" ref="dataSource" />
<property name="sessionFactory" ref="sessionFactory" />
</bean>
请注意,我已尝试(并注释掉)多个会话上下文类设置。 (另外,由于某种原因,这篇文章中的那些XML snippits中出现了一些空格;它们不存在于实际文件中。)
无论如何,是否有人比我更了解春天是什么/不做什么?提前谢谢!
答案 0 :(得分:0)
你有任何错误吗?
我的设置与您的设置非常相似。我的hibernate会话工厂中没有发布模式和自动关闭会话。我没有注意到大量的会话被打开/关闭。您可能想尝试在hibernateFilter上设置单个会话参数。
在休眠过滤器上,我有:
<filter>
<filter-name>hibernateFilter</filter-name>
<filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
<init-param>
<param-name>sessionFactoryBeanName</param-name>
<param-value>hibernateSessionFactory</param-value>
</init-param>
<init-param>
<param-name>singleSession</param-name>
<param-value>true</param-value>
</init-param>
</filter>
您是在进行基于注释的交易还是自己管理?
在web.xml中我也使用请求上下文侦听器,但我很确定这与使dwr使用会话范围的bean有关。
希望有所帮助,对不起,我无法更有洞察力。如果singleSession参数不能满足您的需求,我可以尝试提高我的日志级别。对于每个懒惰的提取,它可能会打开一个新的会话,这对你所看到的内容有意义。