简体   繁体   English

会话和会话工厂在休眠

[英]Session and session factory in hibernate

I am creating an application with Java, Hibernate and SQL Server. 我正在使用Java,Hibernate和SQL Server创建应用程序。 I have a doubt regarding the declaration of objects for Session and Session Factory. 我对Session和Session Factory对象的声明有疑问。 At first when I started to create the application I used to create session and session factory at all the times when needed. 首先,当我开始创建应用程序时,我经常在需要时创建会话和会话工厂。 But now I notices that my application becomes slow when I create session and session factory as said above so I started creating all the needed sessions and session factories at my Log in page and specified the modifiers as public static. 但是现在我注意到,当我如上所述创建会话和会话工厂时,我的应用程序变慢,因此我开始在“登录”页面上创建所有需要的会话和会话工厂,并将修饰符指定为public static。

    public static Session session = null;
    public static SessionFactory sessionfactory = new org.hibernate.cfg.Configuration().configure().buildSessionFactory();
    public static Session session1 = null;
    public static SessionFactory sessionfactory1 = new org.hibernate.cfg.Configuration().configure().buildSessionFactory();
    public static Session session2 = null;
    public static SessionFactory sessionfactory2 = new org.hibernate.cfg.Configuration().configure().buildSessionFactory();

Now whenever I need the use the sessions and session factories I used to call the sessions as Login.session (loginpagefilename.sessionobject). 现在,每当需要使用会话和会话工厂时,我都习惯将这些会话称为Login.session(loginpagefilename.sessionobject)。

 Login.session = Login.sessionfactory.openSession();
        Login.session.beginTransaction();
        String hql = "FROM TEST_SERVICES_POJO lts WHERE NOT EXISTS (SELECT lsm.inttestid FROM PARAMETER_MAPPING_POJO lsm WHERE lsm.status = 1 and lsm.inttestid=lts.inttestid)";
        org.hibernate.Query query = Login.session.createQuery(hql);
        DefaultTableModel model = (DefaultTableModel) testtable.getModel();
        model.setRowCount(0);
        for (Iterator it = query.iterate(); it.hasNext();) {
            TEST_SERVICES_POJO slp = (TEST_SERVICES_POJO) it.next();
            Object[] row = {slp.getTestname()};
            model.addRow(row);
        }
        Login.session.getTransaction().commit();

Here in first I didn't used rollback feature of hibernate. 首先,我没有使用休眠的回滚功能。 But now I wish to implement rollback feature of hibernate. 但是现在我希望实现休眠的回滚功能。 So following which method will give me better performance. 因此,遵循以下方法将为我带来更好的性能。 Kindly suggest me a way. 请给我一种方法。 Thanks in advance. 提前致谢。

The most important thing: never ever create more than one SessionFactory per application (unless you have more than one persistence-context, then create one per appplication and persistence-context). 最重要的事情:每个应用程序永远不要创建多个SessionFactory (除非您拥有多个持久化上下文,然后再为每个应用程序和持久性上下文创建一个)。 Keeping creating SessionFactories will first kill your performance and then your app-server due to OutOfMemoryError s. 继续创建SessionFactories将首先导致性能SessionFactories ,然后由于OutOfMemoryError应用服务器。 Create a SessionFactory at application startup and keep it around where it is accessible - store it in jndi, keep it in a global class or something like that. 在应用程序启动时创建一个SessionFactory ,并将其保留在可访问的位置-将其存储在jndi中,将其保留在全局类中。

Create Session s as needed - the best match often is one Session per transaction, so hibernate can utilise its caching-mechanism. 根据需要创建Session最佳匹配通常是每个事务一个Session ,因此休眠可以利用其缓存机制。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM