【问题标题】:Hibernate Session handling休眠会话处理
【发布时间】:2014-09-22 08:20:29
【问题描述】:

我正在尝试学习休眠和检票口。我实际上不确定何时打开和关闭休眠会话。我已经搜索了很多并阅读了很多关于会话工厂的内容,但我仍然不明白。

我想从我的数据库中获取一些数据并将其显示在浏览器的表格中。如果我是第一次访问该网站,这实际上是可行的,但是如果我使用后退按钮并再次访问该网站,它会向我显示此错误:

Last cause: Unknown service requested [org.hibernate.engine.jdbc.connections.spi.ConnectionProvider]

我认为这是因为我提前关闭了我的 SessionFactory 或类似的东西。但我不知道如何解决这个问题。

我的 Java 类:

public class CategoryPanel extends Panel {
    private WebMarkupContainer categoryContainer;
    public CategoryPanel(String id) {
        super(id);
        SessionFactory sessionFactory = DbFactory.getSessionFactory();                  // creating the Session Factory     
        StandardDao<Category> categoryDao = StandardDao.getInstance(sessionFactory);    // creating dao to access data
        List<Category> categoryList = categoryDao.getAll(Category.class);               // get data of the db  
        CategoryDataProvider dataProvider = new CategoryDataProvider(categoryList);        
        categoryContainer = new WebMarkupContainer("categoryTable");

        final DataView dv = new DataView("categoryList", dataProvider) {

          @Override
          protected void populateItem(Item item) {
              final Category category = (Category) item.getModelObject();
              final CompoundPropertyModel<Category> categoryModel = new     CompoundPropertyModel<Category>(category);

              item.add(new Label("catTitle", categoryModel.bind("title")));
          }
         };
        categoryContainer.add(new Label("categoryTitle", Model.of("Titel")));
        add(categoryContainer);
        sessionFactory.close(); // here I close the factory, this seems to be wrong. I dont know if i close it anyway..
    }
}

我的道:

public class StandardDao<T> {
 private static StandardDao instance = null;
    private static final Object syncObject = new Object();
    private SessionFactory sessionFactory;


    private StandardDao(SessionFactory session){
     this.sessionFactory = session;
    }

     public static StandardDao getInstance(SessionFactory session) {
        if (instance == null) {
            synchronized (syncObject) {
                if (instance == null) {
                    instance = new StandardDao(session);
                }
            }
        }
        return instance;
    }   
        public List<T> getAll( Class theClass) {
            List<T> entity = null;
            Session session = sessionFactory.openSession();
            try {
                entity = session.createCriteria(theClass).list();
            } catch (RuntimeException e) {
                e.printStackTrace();
            }finally {
            session.flush();
            // if I close the session here, I cant load lazy
         }
            return entity;
         }
  }

【问题讨论】:

    标签: java hibernate session wicket


    【解决方案1】:

    您不应关闭 SessionFactory。 SessionFactory 用于向您返回一个 Session,您在该 Session 上工作并仅关闭该 Session。

    此外,您通常不会在控制器代码中打开和关闭会话,而是将此处理委托给外部组件,该组件可以在每次请求到达时从会话工厂打开一个新会话,最重要的是仅在以下情况下关闭它请求完成,这意味着视图部分也完成了它的工作。

    这种模式通常称为“Open Session in View”。

    在 Java Web 服务器中,这通常使用您在 web.xml 中配置的 servlet 过滤器来完成。

    在 Google 中搜索 Java Open Session in View 过滤器,您会发现大量示例和说明。

    【讨论】:

      猜你喜欢
      • 2013-10-11
      • 2013-04-30
      • 2021-10-29
      • 1970-01-01
      • 1970-01-01
      • 2012-06-02
      • 1970-01-01
      相关资源
      最近更新 更多