-->
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.  [ 2 posts ] 
Author Message
 Post subject: HibernateUtil and static block - why not allow a setter?
PostPosted: Wed May 11, 2005 3:17 pm 
Newbie

Joined: Wed May 11, 2005 3:09 pm
Posts: 1
I have been looking over the standard HibernateUtil provided with Hibernate in Action and thinking about how to test hibernate in my app. The simplest way I can think of is to use HibernateUtil but set my own Configuration on it before it loads the SessionFactory, but you can't do that with the static block as it will always run first using some default. I could swap out config files that the static block looks for, but I'd rather drive the config from my code so I can do this from TestCase setup or decorator. Also, we are not using Spring so there is no easy IoC available that way...

Does anyone see a problem with using a lazily configured SessionFactory so as to allow a setConfig()/load of configuration resources before the factory is loaded? In production, I suppose setConfig and load would be called with the default production db in a startup servlet so the factory gets loaded when the web app starts up. In our test environment, I could do a setConfig before running tests, so we could hit HSQLDB or whatever.

Comments?

- Rob
http://www.robsanheim.com


Top
 Profile  
 
 Post subject: Re: HibernateUtil and static block - why not allow a setter?
PostPosted: Wed May 11, 2005 5:22 pm 
Expert
Expert

Joined: Mon Feb 14, 2005 12:32 pm
Posts: 609
Location: Atlanta, GA - USA
rsanheim wrote:
I have been looking over the standard HibernateUtil provided with Hibernate in Action and thinking about how to test hibernate in my app. The simplest way I can think of is to use HibernateUtil but set my own Configuration on it before it loads the SessionFactory, but you can't do that with the static block as it will always run first using some default. I could swap out config files that the static block looks for, but I'd rather drive the config from my code so I can do this from TestCase setup or decorator. Also, we are not using Spring so there is no easy IoC available that way...

Does anyone see a problem with using a lazily configured SessionFactory so as to allow a setConfig()/load of configuration resources before the factory is loaded? In production, I suppose setConfig and load would be called with the default production db in a startup servlet so the factory gets loaded when the web app starts up. In our test environment, I could do a setConfig before running tests, so we could hit HSQLDB or whatever.

Comments?

- Rob
http://www.robsanheim.com


I'm pretty sure I copied this from a book or somewhere but the lazy-initialization is how I do it.
Code:
    public static Session getSession()  {

        Session session = (Session) threadLocalSession.get();

        if (session == null) {
              if (sessionFactory == null) {
                  Configuration cfg = new Configuration();
                  cfg.configure(CONFIG_FILE_LOCATION);
                  sessionFactory = cfg.buildSessionFactory();
              }
              session = sessionFactory.openSession();
              session.connection().setAutoCommit(false);
              threadLocalSession.set(session);
        }
        return session;
    }


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 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.