问题描述
使用springmvc、spring3.0.5、hibernate3整的一个框架,hibernate使用懒加载模式时会报couldnotinitializeproxy-noSession,于是配置了OpenSessionInViewFilter,可是还是报错,通过关联spring的源码后debug,这个过滤器确实是执行了,并且创建了Session,然后去Dao层通过SessionFactory.getCurrentSession()执行操作,如下代码:protectedSessiongetSession(){Sessionsession=sessionFactory.getCurrentSession();Sessionsession1=SessionFactoryUtils.doGetSession(sessionFactory,true);LOGGER.info("daoSession:"+session);LOGGER.info("filterSession:"+session1);LOGGER.info(session==session1);returnsession;}代码中session是通过依赖注入的sessionFactory获取的当前session,session1这种方式是查看spring的OpenSessionInViewFilter源码中拷过来的,日志输出session==session1为true,其中的一个dao操作为:@Transactional(readOnly=true,propagation=Propagation.NOT_SUPPORTED)publicTfind(SerializableentityId){//return(T)getSession().get(clazz,entityId);return(T)getSession().load(clazz,entityId);}但是执行这个find方法结果还是报couldnotinitializeproxy-noSession,这就奇怪了,难道session在执行完find方法后就被关闭了?所以在返回到jsp中使用el表达式取值的时候session已经关闭了?求大神指点迷津!!
解决方案
解决方案二:
http://www.yybean.com/opensessioninviewfilter-role-and-configuration
解决方案三:
那篇文章看过了,但是对于解决我的问题效果不大啊。。。
解决方案四:
我得看看你的OpenSessionInViewFilter怎么配置的
解决方案五:
之前我项目中出现过这个问题后来检查发现是我配置的SessionFactory弄错了比如你这个查询是用的aSessionFactory但是你open里面配置的是bSessionFactory这样a还是会报错
解决方案六:
<beanid="sessionFactory"class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"><!--<beanid="sessionFactory"class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">--><propertyname="dataSource"ref="dataSource"/><propertyname="packagesToScan"><list><value>com.jiangnan.cms.domain</value></list></property><propertyname="hibernateProperties"><value>hibernate.dialect=org.hibernate.dialect.MySQLInnoDBDialecthibernate.show_sql=truehibernate.format_sql=falsehibernate.query.substitutions=true1,false0hibernate.jdbc.batch_size=20hibernate.hbm2ddl.auto=updatehibernate.cache.use_query_cache=false</value></property></bean><beanid="transactionManager"class="org.springframework.orm.hibernate3.HibernateTransactionManager"><propertyname="sessionFactory"ref="sessionFactory"/></bean><tx:annotation-driventransaction-manager="transactionManager"/>以上是sessionFactory及事务的配置
解决方案七:
跟OpenSessionInViewFilter配置的顺序有关
解决方案八:
<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有啥问题吗?