在线等待啊!! 集成Shiro 一直报错 提示org.hibernate.HibernateException: No Session found for current thread

码农小菜MAX 发布于 2014/09/03 14:41
阅读 1K+
收藏 0

版本   Spring  4.0.6.RELEASE
        Hibernate  4.3.6.Final
        Shiro     1.2.3
        Ehcache    2.5.1


异常信息:

严重: Servlet.service() for servlet [springmvc] in context with path [/test] threw exception [org.hibernate.HibernateException: No Session found for current thread] with root cause
org.hibernate.HibernateException: No Session found for current thread
    at org.springframework.orm.hibernate4.SpringSessionContext.currentSession(SpringSessionContext.java:106)
    at org.hibernate.internal.SessionFactoryImpl.getCurrentSession(SessionFactoryImpl.java:1014)
    at com.common.base.BaseDao.getsession(BaseDao.java:25)
    at com.common.base.BaseDao.createQuery(BaseDao.java:55)
    at com.common.base.BaseDao.findByHQL(BaseDao.java:47)
    at com.modelus.service.imp.UserServiceImp.account(UserServiceImp.java:39)
    at com.modelus.service.imp.UserServiceImp$$FastClassBySpringCGLIB$$f3b65bb1.invoke(<generated>)
    at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204)
    at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:708)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157)
    at org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:98)
    at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:262)
    at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:95)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
    at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:644)
    at com.modelus.service.imp.UserServiceImp$$EnhancerBySpringCGLIB$$734b389d.account(<generated>)
    at com.common.shiro.AuthorizingRealmImp.doGetAuthorizationInfo(AuthorizingRealmImp.java:32)
    at org.apache.shiro.realm.AuthorizingRealm.getAuthorizationInfo(AuthorizingRealm.java:341)
    at org.apache.shiro.realm.AuthorizingRealm.isPermitted(AuthorizingRealm.java:461)
    at org.apache.shiro.realm.AuthorizingRealm.isPermitted(AuthorizingRealm.java:457)
    at org.apache.shiro.authz.ModularRealmAuthorizer.isPermitted(ModularRealmAuthorizer.java:223)
    at org.apache.shiro.authz.ModularRealmAuthorizer.isPermittedAll(ModularRealmAuthorizer.java:291)
    at org.apache.shiro.mgt.AuthorizingSecurityManager.isPermittedAll(AuthorizingSecurityManager.java:129)
    at org.apache.shiro.subject.support.DelegatingSubject.isPermittedAll(DelegatingSubject.java:182)
    at org.apache.shiro.web.filter.authz.PermissionsAuthorizationFilter.isAccessAllowed(PermissionsAuthorizationFilter.java:49)
    at org.apache.shiro.web.filter.AccessControlFilter.onPreHandle(AccessControlFilter.java:162)
    at org.apache.shiro.web.filter.PathMatchingFilter.isFilterChainContinued(PathMatchingFilter.java:203)
    at org.apache.shiro.web.filter.PathMatchingFilter.preHandle(PathMatchingFilter.java:178)
    at org.apache.shiro.web.servlet.AdviceFilter.doFilterInternal(AdviceFilter.java:131)
    at org.apache.shiro.web.servlet.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:125)
    at org.apache.shiro.web.servlet.ProxiedFilterChain.doFilter(ProxiedFilterChain.java:66)
    at org.apache.shiro.web.servlet.AdviceFilter.executeChain(AdviceFilter.java:108)
    at org.apache.shiro.web.servlet.AdviceFilter.doFilterInternal(AdviceFilter.java:137)
    at org.apache.shiro.web.servlet.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:125)
    at org.apache.shiro.web.servlet.ProxiedFilterChain.doFilter(ProxiedFilterChain.java:66)
    at org.apache.shiro.web.servlet.AbstractShiroFilter.executeChain(AbstractShiroFilter.java:449)
    at org.apache.shiro.web.servlet.AbstractShiroFilter$1.call(AbstractShiroFilter.java:365)
    at org.apache.shiro.subject.support.SubjectCallable.doCall(SubjectCallable.java:90)
    at org.apache.shiro.subject.support.SubjectCallable.call(SubjectCallable.java:83)
    at org.apache.shiro.subject.support.DelegatingSubject.execute(DelegatingSubject.java:383)
    at org.apache.shiro.web.servlet.AbstractShiroFilter.doFilterInternal(AbstractShiroFilter.java:362)
    at org.apache.shiro.web.servlet.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:125)
    at org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:344)
    at org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:261)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:501)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:950)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1040)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:607)
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:316)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    at java.lang.Thread.run(Unknown Source)


在hibernate的XML中有如下配置:

        SessionFactory:

            <prop key="hibernate.show_sql">true</prop>
                <prop key="hibernate.format_sql">true</prop>
                <prop key="hibernate.cache.region.factory_class">org.hibernate.cache.ehcache.EhCacheRegionFactory
                </prop>
                <prop key="hibernate.cache.use_second_level_cache">true</prop>
                <prop key="hibernate.cache.use_query_cache">true</prop>


<!-- 注解式事物 -->
    <bean id="transactionManager"
        class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource" />
    </bean>
    <tx:annotation-driven transaction-manager="transactionManager"
        proxy-target-class="true" />


shiro配置文件:


<bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">
        <property name="realm" ref="shiroDbRealm" />
    </bean>
    <bean id="shiroDbRealm" class="com.common.shiro.AuthorizingRealmImp"></bean>

    <!-- Shiro Filter -->
    <bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean">
        <property name="securityManager" ref="securityManager" />
        <property name="loginUrl" value="/" />
        <property name="successUrl" value="index" />
        <property name="unauthorizedUrl" value="/views/error" />
        <property name="filterChainDefinitions">
            <value>
                /user/** = anon
                /dict/** =authc,perms[role:edit,role:view]
            </value>
        </property>
    </bean>


web.xml 配置文件:

        <filter>
        <filter-name>openSessionInViewFilter</filter-name>
        <filter-class>org.springframework.orm.hibernate4.support.OpenSessionInViewFilter</filter-class>
           <init-param>
            <param-name>singleSession</param-name>
            <param-value>true</param-value>
        </init-param>
        <init-param>
            <param-name>sessionFactoryBean</param-name>
            <param-value>sessionFactory</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>openSessionInViewFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>


因为我这个demo是全注解的所以service层 都是@service  和  @Transactional  标注的 事物是绝对开启了的


Dao层:

@Autowired
    private SessionFactory sessionFactory;

    public Session getsession() {
        return sessionFactory.getCurrentSession();
    }


经过调试我发现  当用户经过shiro安全框架登录调用service层的登录方法时 在dao层开启的session貌似没有关闭  而后面去获取权限信息时通过同一个service类 同一个方法到达dao层的时候 session依然还在 并且直接导致这次获取session失败  后来我加了个判断要是session不为空就不再去获取一次  但是 加了判断后还是报错  这次报的是  session  是封闭的   不能使用


这问题困扰了好几天了  求大神解惑  谢谢

加载中
0
xiaoqqq
xiaoqqq
把你当前的类 加到事务里
xiaoqqq
xiaoqqq
回复 @码农小菜MAX : 作为一个有理想、有道德、有知识、有纪律的程序员, 解决了把解决办法放出来。 放出你的解决方案, 我看有没有超出事务和openSessionInview两种。
码农小菜MAX
你所指的当前类是Shiro的AuthorizingRealm实现类? 还是什么类?
0
平安北京
是不是需要在web.xml中配openSessionInViewFilter啊
0
码农小菜MAX

引用来自“冷泉”的评论

是不是需要在web.xml中配openSessionInViewFilter啊

有这个配置啊!!!

<filter>

        <filter-name>openSessionInViewFilter</filter-name>
        <filter-class>org.springframework.orm.hibernate4.support.OpenSessionInViewFilter</filter-class>
        <!-- singleSession默认为true,若设为false则等于没用OpenSessionInView 。所以默认可以不写 -->
        <init-param>
            <param-name>singleSession</param-name>
            <param-value>true</param-value>
        </init-param>
        <!-- 指定org.springframework.orm.hibernate4.LocalSessionFactoryBean在spring配置文件中的名称,默认值为sessionFactory。
            如果LocalSessionFactoryBean在spring中的名称不是sessionFactory,该参数一定要指定,否则会出现找不到sessionFactory的例外。所以默认可以不写 -->
        <init-param>
            <param-name>sessionFactoryBean</param-name>
            <param-value>sessionFactory</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>openSessionInViewFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
921977939qqcom
921977939qqcom
这个不好,低b做法
0
码农小菜MAX
谢谢各位的回答  问题已经解决了   但是还是要谢谢你们的回答
l
luckyyezi
答案是啥?
返回顶部
顶部