-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 
Author Message
 Post subject: Why hibernate create a new SessionFactory everytime?
PostPosted: Tue Aug 31, 2004 8:14 pm 
Senior
Senior

Joined: Wed Dec 17, 2003 4:24 am
Posts: 188
hi everyone:

I want to know if hibernate create a new SessionFactory when I execute it like
Code:
  from User as u


It is in the J2EE environment.(server :Tomcat).
Because building sessionFactory is take too much time. So I want to know if hibernate create a sessionFactory everytime when I invoke it. Thks

_________________
You are not alone...


Top
 Profile  
 
 Post subject:
PostPosted: Wed Sep 01, 2004 1:46 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
Hibernate does NOT create SessionFactorie's - Users do.

Check your j2ee application for code that might construct the sessionfactory each time a bean (or servlet) is created...

_________________
Max
Don't forget to rate


Last edited by max on Thu Sep 02, 2004 1:28 am, edited 1 time in total.

Top
 Profile  
 
 Post subject: My code is simple
PostPosted: Wed Sep 01, 2004 8:36 pm 
Senior
Senior

Joined: Wed Dec 17, 2003 4:24 am
Posts: 188
Code:
Hibernate : 2.1.6
mysql      : 3.23.56
Tomcat    :5.16


The hibernate.cfg.xml is following:
Code:
   <hibernate-configuration>
   <session-factory>
      <property name="show_sql">true</property>
      <property name="dialect">net.sf.hibernate.dialect.MySQLDialect</property>
      <mapping resource="Trade.hbm.xml"/>
   </session-factory>
</hibernate-configuration>


The mapping file:
Code:
<hibernate-mapping>
<class name="lyo.test.bean.Trade" table="tradetest">
   <id name="trade_id" column="trade_id" type="int">
       <generator class="native"/>
   </id>
   <property name="trade_name" column="trade_name"/>
   <property name="trade_desc" column="trade_desc"/>
</class>
</hibernate-mapping>


I have a java helper class:
Code:
public class HibernateUtil {

    //private static Logger  log = LogFactory.getLog(HibernateUtil.class);

    private static final SessionFactory sessionFactory;

    static {
        try {
            // Create the SessionFactory
            sessionFactory = new Configuration().configure().buildSessionFactory();
        } catch (Throwable ex) {
      //      log.error("Initial SessionFactory creation failed.", ex);
            throw new ExceptionInInitializerError(ex);
        }
    }

    public static final ThreadLocal session = new ThreadLocal();

    public static Session currentSession() throws HibernateException {
        Session s = (Session) session.get();
        // Open a new Session, if this Thread has none yet
        if (s == null) {
            s = sessionFactory.openSession();
            session.set(s);
        }
        return s;
    }

    public static void closeSession() throws HibernateException {
        Session s = (Session) session.get();
        session.set(null);
        if (s != null)
            s.close();
    }
}

hibernate.properties File is:
Code:
#hibernate.connection.driver_class org.gjt.mm.mysql.Driver
#hibernate.connection.url jdbc:mysql://localhost:3306/poweracl
#hibernate.connection.username lyo
#hibernate.connection.password qijiashe

I use it in this way:
Code:
Session s=HibernateUtil.currentSession();
      Transaction t=s.beginTransaction();
List list=s.find("from Trade as trade");
      t.commit();
      HibernateUtil.closeSession();
return list;

Everytime I run it ,Tomcat console will output:

Code:
08:20:37,281  INFO TransactionManagerLookupFactory:33 - No TransactionManagerLoo
kup configured (in JTA environment, use of process level read-write cache is not
recommended)
08:20:37,484  INFO SettingsFactory:103 - Use scrollable result sets: true
08:20:37,484  INFO SettingsFactory:106 - Use JDBC3 getGeneratedKeys(): false
08:20:37,484  INFO SettingsFactory:109 - Optimize cache for minimal puts: false
08:20:37,484  INFO SettingsFactory:115 - echoing all SQL to stdout
08:20:37,484  INFO SettingsFactory:118 - Query language substitutions: {no='N',
true=1, yes='Y', false=0}
.............................................................................................
........................................................................
....................................................................
08:20:37,515  INFO Configuration:1116 - instantiating and configuring caches
08:20:37,734  INFO SessionFactoryImpl:118 - building session factory
08:20:38,343  INFO SessionFactoryObjectFactory:82 - Not binding factory to JNDI,
no JNDI name configured
08:20:38,359  INFO UpdateTimestampsCache:35 - starting update timestamps cache a
t region: net.sf.hibernate.cache.UpdateTimestampsCache
08:20:38,359  INFO StandardQueryCache:41 - starting query cache at region: net.s
f.hibernate.cache.StandardQueryCache
Hibernate: select trade0_.trade_id as trade_id, trade0_.trade_name as trade_name
, trade0_.trade_desc as trade_desc from tradetest trade0_


Is there any problem ?

_________________
You are not alone...


Top
 Profile  
 
 Post subject:
PostPosted: Thu Sep 02, 2004 1:29 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
not apparently - but why don't you try to step through it with a debugger ?

Hibernate don't create SessionFactorie's - your code does somehow !

_________________
Max
Don't forget to rate


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.