Spring + Hibernate:没有Hibernate会话绑定到线程

时间:2013-09-04 08:11:28

标签: java hibernate spring-mvc

我正在尝试实现以下内容:在SpringSecurity注销处理程序中清除DB上的一些细节。尝试从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>

DAO:

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解决了这个问题,但我不明白为什么?我的意思是它在所有其他调用中只在这个处理程序中失败,如果没有这个注释,这个方法可以正常工作!

提前谢谢!

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

1 个答案:

答案 0 :(得分:1)

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

有一些解决方法可以将hibernate配置中的current_session_context_class指定为thread

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

但这不符合生产安全。

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

同时尝试使用sessionFactory.getCurrentSession()代替SessionFactoryUtils.getSession()