【问题标题】:Spring + Hibernate: No Hibernate Session bound to threadSpring + Hibernate:没有绑定到线程的 Hibernate Session
【发布时间】:2013-09-04 08:11:27
【问题描述】:

我正在尝试实现以下内容:在 SpringSecurity 注销处理程序上从 DB 中清除一些详细信息。尝试从数据库获取用户详细信息后出现此错误的主要问题。其余代码甚至相同的方法在其他情况下都可以正常工作。

public class CurrentUserLogoutSuccessHandler extends SimpleUrlLogoutSuccessHandler {

    /**
     * 
     */
    @Autowired
    private RequestsService requestsService;

    /**
     * 
     */
    @Autowired
    private OffersService offersService;

    /**
     * 
     */
    @Autowired
    private UsersService usersService;

    /**
     * 
     */
    @Override
    public void onLogoutSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication)
            throws IOException, ServletException {
        if (authentication != null) {
            UserDetailsExtended details = (UserDetailsExtended) authentication.getPrincipal();
            User user = usersService.get(details.getId()); // fails here

            requestsService.unlockAllByBackoffice(user);
            offersService.unlockAllByBackoffice(user);
        }

        setDefaultTargetUrl("/");
        super.onLogoutSuccess(request, response, authentication);
    }
}

配置:

<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="packagesToScan" value="com.ejl.butler.object.data" />
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">${hibernate.dialect}</prop>
                <prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
                <prop key="hibernate.format_sql">${hibernate.format_sql}</prop>
                <prop key="hibernate.cache.use_query_cache">${hibernate.cache.use_query_cache}</prop>
                <prop key="hibernate.cache.region.factory_class">${hibernate.cache.region.factory_class}</prop>
           </props>
        </property>
    </bean>
    <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory" />
    </bean>

道:

public User get(final Long id) {
        Session session = SessionFactoryUtils.getSession(sessionFactory, false);

        return (User) session.get(User.class, id);
    }

Spring 安全配置:

<logout invalidate-session="true" logout-url="/logout" success-handler-ref="logoutSuccessHandler"/>

例外:

No Hibernate Session bound to thread, and configuration does not allow creation of non-transactional one here
    at org.springframework.orm.hibernate3.SessionFactoryUtils.doGetSession(SessionFactoryUtils.java:356)
    at org.springframework.orm.hibernate3.SessionFactoryUtils.getSession(SessionFactoryUtils.java:202)

@Transactional 解决了问题,但我不明白为什么?我的意思是它只在这个处理程序中在所有其他调用中失败,这个方法在没有这个注释的情况下可以正常工作!

提前谢谢你!

更新: 我的临时解决方案是将@Transactional 添加到整个onLogoutSuccess 方法中。它有效)

【问题讨论】:

  • 你的注销url被Spring MVC拦截了吗?
  • @shamimz 我向问题添加了安全配置。不,我只是指定了注销处理程序,但 url 本身没有被 Spring 拦截
  • 你能提交你收到的错误信息吗?
  • @shamimz 我更新了问题
  • @shamimz 顺便说一句.. 我想如果我在配置中指定了注销 url 那么它应该被 Spring 自动拦截

标签: java hibernate spring-mvc


【解决方案1】:

如果您在 spring 上下文中定义了 TransactionManager,则必须在堆栈中的某处指定 @Transactional。否则,您将遇到遇到的异常,因为您尝试在事务之外运行查询。

有一些解决方法,例如在您的休眠配置中将current_session_context_class 指定为thread

<property name="current_session_context_class">org.hibernate.context.ThreadLocalSessionContext</property>

但这不是生产安全的。.

current_session_context_class 的可能值为jtathreadmanaged。此外,jtathread 由 hibernate 开箱即用支持。 thread 上下文用于大多数独立的休眠应用程序或基于轻量级框架(如 Spring)的应用程序,jta 用于 Java EE 环境。

也可以试试sessionFactory.getCurrentSession(),而不是SessionFactoryUtils.getSession()

【讨论】:

  • 两者都不是生产安全的?
  • 我只是想了解为什么同一段代码在其他情况下也能正常工作
  • 您能否详细说明此代码在哪些其他情况下可以正常工作?另外,您在代码中的何处应用 @Transactional 注释?
  • 试试&lt;tx:annotation-driven transaction-manager="transactionManager" /&gt;
  • 当使用 spring 时,千万不要乱用 hibernate 的 current_session_context_class 属性。除非你使用 JTA。 Anuting else(或将其设置为线程)将破坏正确的 Spring 事务管理(Spring 有自己的 CurrentSessionContext 实现以进行正确集成)!。接下来你真的应该使用SessionFactory.getCurrentSession insteaf 你现在拥有的实用程序类。
猜你喜欢
  • 2011-07-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-12-22
  • 2012-06-09
  • 2011-05-09
  • 2011-12-11
  • 2010-11-19
相关资源
最近更新 更多