【问题标题】:Why we are getting currentsession twice in the application?为什么我们在应用程序中获得了两次 currentsession?
【发布时间】:2011-03-22 13:13:15
【问题描述】:

最近我做了一些工作来检查,在我们的应用程序中,在哪里提供与会话相关的代码, 即.获取当前会话(sessionFactory.getCurrentSession());

我已经在应用程序中两次看到此代码,一次在 HibernateSessionRequestFileter 类

package com.persistence;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.SessionFactory;
import org.hibernate.StaleObjectStateException;
public class HibernateSessionRequestFilter implements Filter {

    private static Log log = LogFactory.getLog(HibernateSessionRequestFilter.class);

    private SessionFactory sf;

    public void doFilter(ServletRequest request,
                         ServletResponse response,
                         FilterChain chain)
            throws IOException, ServletException {

        try {
            log.debug("Starting a database transaction");
            sf.getCurrentSession().beginTransaction();

            // Call the next filter (continue request processing)
            chain.doFilter(request, response);

            // Commit and cleanup
            log.debug("Committing the database transaction");
            sf.getCurrentSession().getTransaction().commit();

        } catch (StaleObjectStateException staleEx) {
            log.error("This interceptor does not implement optimistic concurrency control!");
            log.error("Your application will not work until you add compensation actions!");
            // Rollback, close everything, possibly compensate for any permanent changes
            // during the conversation, and finally restart business conversation. Maybe
            // give the user of the application a chance to merge some of his work with
            // fresh data... what you do here depends on your applications design.
            throw staleEx;
        } catch (Throwable ex) {
            // Rollback only
            ex.printStackTrace();
            try {
                if (sf.getCurrentSession().getTransaction().isActive()) {
                    log.debug("Trying to rollback database transaction after exception");
                    //sf.getCurrentSession().getTransaction().rollback();
                }
            } catch (Throwable rbEx) {
                log.error("Could not rollback transaction after exception!", rbEx);
            }

            // Let others handle it... maybe another interceptor for exceptions?
            throw new ServletException(ex);
        }
    }

    public void init(FilterConfig filterConfig) throws ServletException {
        log.debug("Initializing filter...");
        log.debug("Obtaining SessionFactory from static HibernateUtil singleton");
        sf = HibernateUtil.getSessionFactory();
    }

    public void destroy() {}


}

GenericHibernateDAO 类中的另一个,如下所示,

protected Session getSession() {

        if (session == null) {
            session = HibernateUtil.getSessionFactory().getCurrentSession();
        } else if (!session.isConnected()) {
            session = HibernateUtil.getSessionFactory().getCurrentSession();
        }
        return session;
    }

谁能帮我理解,为什么我们必须在两个地方获取当前会话? 当我们开始事务时,我们得到了 currentsession ,就像我们从数据库中持久化或获取对象一样,我们又得到了 currentsession , 为什么会这样?

【问题讨论】:

    标签: hibernate session filter dao


    【解决方案1】:

    这看起来像 OpenSessionInView 模式的一个版本,其中当接收到请求时打开 Hibernate Session,并在呈现响应后关闭。

    在过滤器中打开一个会话并启动一个事务。

    然后处理请求,在 Dao 中调用 getCurrentSession() 只获取当前打开的会话,而不是创建新的会话。

    道做它的工作。 然后过滤器提交事务并关闭会话。

    【讨论】:

    • 是的,我同意你的解释,但我仍然无法区分你如何告诉下面的代码只获取通用 dao 中的当前会话和相同的代码在过滤器中获取新会话班级 ? session = HibernateUtil.getSessionFactory().getCurrentSession();
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-14
    • 1970-01-01
    • 2023-03-24
    • 1970-01-01
    相关资源
    最近更新 更多