Hibernate OpenSessionInViewFilter问题

jianzhongqing 2013-12-12 09:58:40
使用spring mvc、spring3.0.5、hibernate3整的一个框架,hibernate使用懒加载模式时会报could not initialize proxy - no Session,于是配置了OpenSessionInViewFilter,可是还是报错,通过关联spring的源码后debug,这个过滤器确实是执行了,并且创建了Session,然后去Dao层通过SessionFactory.getCurrentSession()执行操作,如下代码:
protected Session getSession(){
Session session = sessionFactory.getCurrentSession();
Session session1 = SessionFactoryUtils.doGetSession(sessionFactory, true);
LOGGER.info("daoSession:" + session);
LOGGER.info("filterSession:" + session1);
LOGGER.info(session == session1);
return session;
}
代码中session是通过依赖注入的sessionFactory获取的当前session,session1这种方式是查看spring的OpenSessionInViewFilter源码中拷过来的,日志输出session == session1为true,其中的一个dao操作为:
@Transactional(readOnly=true,propagation=Propagation.NOT_SUPPORTED)
public T find(Serializable entityId) {
//return (T)getSession().get(clazz, entityId);
return (T)getSession().load(clazz, entityId);
}
但是执行这个find方法结果还是报could not initialize proxy - no Session,这就奇怪了,难道session在执行完find方法后就被关闭了?所以在返回到jsp中使用el表达式取值的时候session已经关闭了?求大神指点迷津!!
...全文
159 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
jianzhongqing 2013-12-19
  • 打赏
  • 举报
回复
<filter> <filter-name>encoding</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>UTF-8</param-value> </init-param> </filter> <filter-mapping> <filter-name>encoding</filter-name> <url-pattern>/cms/*</url-pattern> </filter-mapping> <filter> <filter-name>osivFilter</filter-name> <filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class> </filter> <filter-mapping> <filter-name>osivFilter</filter-name> <url-pattern>/cms/*</url-pattern> </filter-mapping> <!-- 权限配置 --> <filter> <filter-name>privilegeFilter</filter-name> <filter-class>com.jiangnan.cms.filter.PrivilegeFilter</filter-class> </filter> <filter-mapping> <filter-name>privilegeFilter</filter-name> <url-pattern>/cms/admin/*</url-pattern> </filter-mapping> <filter> <filter-name>contextFilter</filter-name> <filter-class>com.jiangnan.cms.filter.ContextFilter</filter-class> </filter> <filter-mapping> <filter-name>contextFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <filter> <filter-name>FileUploadFilter</filter-name> <filter-class>com.ckfinder.connector.FileUploadFilter</filter-class> <init-param> <param-name>sessionCookieName</param-name> <param-value>JSESSIONID</param-value> </init-param> <init-param> <param-name>sessionParameterName</param-name> <param-value>jsessionid</param-value> </init-param> </filter> <filter-mapping> <filter-name>FileUploadFilter</filter-name> <url-pattern> /ckfinder/core/connector/java/connector.java </url-pattern> </filter-mapping> 以上五个filter是web.xml中所有的filter,第一个是设置编码的,第二个就是openSessionInViewFilter,第三个是自定义的,做权限控制的,第四个是也是自定义的,主要将请求request放入ThreadLocal中,以便其他地方使用request,第五个是ckfinder使用到的。这五个filter的绝对顺序是上面这样的,其中前面四个顺序是紧挨着的,第四个和第五个filter之间还有一些servlet的配置,filter的顺序或者拦截的url有啥问题吗?
  • 打赏
  • 举报
回复
跟OpenSessionInViewFilter配置的顺序有关
jianzhongqing 2013-12-18
  • 打赏
  • 举报
回复
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"> <!-- <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> --> <property name="dataSource" ref="dataSource"/> <property name="packagesToScan"> <list> <value>com.jiangnan.cms.domain</value> </list> </property> <property name="hibernateProperties"> <value> hibernate.dialect=org.hibernate.dialect.MySQLInnoDBDialect hibernate.show_sql=true hibernate.format_sql=false hibernate.query.substitutions=true 1, false 0 hibernate.jdbc.batch_size=20 hibernate.hbm2ddl.auto=update hibernate.cache.use_query_cache=false </value> </property> </bean> <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory" /> </bean> <tx:annotation-driven transaction-manager="transactionManager"/> 以上是sessionFactory及事务的配置
放纵的青春 2013-12-16
  • 打赏
  • 举报
回复
之前我项目中出现过这个问题 后来检查发现是 我配置的SessionFactory弄错了 比如你这个查询是用的aSessionFactory 但是你open里面配置的是bSessionFactory 这样a还是会报错
放纵的青春 2013-12-16
  • 打赏
  • 举报
回复
我得看看你的OpenSessionInViewFilter怎么配置的
jianzhongqing 2013-12-13
  • 打赏
  • 举报
回复
那篇文章看过了,但是对于解决我的问题效果不大啊。。。
panzhixiong_cn 2013-12-13
  • 打赏
  • 举报
回复
http://www.yybean.com/opensessioninviewfilter-role-and-configuration

67,513

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧