-->
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.  [ 7 posts ] 
Author Message
 Post subject: Restart Server?
PostPosted: Wed Mar 24, 2004 2:53 pm 
Newbie

Joined: Wed Mar 24, 2004 10:51 am
Posts: 12
It seems that I need to restart my app server (Resin) whenever I change my .hbm.xml map file in order to load the new configuration.

Is there any way to avoid this?

Thanks,

Drew


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 24, 2004 5:43 pm 
Beginner
Beginner

Joined: Wed Mar 17, 2004 4:13 pm
Posts: 21
Location: San Diego, CA
I believe the only way for Hibernate to see changes in its configuration files is to reload the configuration...re-load the configuration...re-initialize the session factory...and make sure you get new sessions from this new factory.

I don't think there is any built in mechanism to do it automatically. If you want that, you'd have to create your own. Like some sort of "file watcher" that detects when a config file has changed and then reload the configuration yourself.

Typically, I would think your database settings are relatively stable. The only time you'd realy find this feature usefull is during development. Once you have your mappings set, you'll probably never have to touch them. Certainly, in a live production environment I have a hard time seeing a good reason to reload hibernate's configuration.

Chrisjan


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 24, 2004 6:29 pm 
Newbie

Joined: Wed Mar 24, 2004 10:51 am
Posts: 12
Yeah, it's really just a development issue.

Do you think I could reload the config temporarily while I'm working on my database schema, etc. and then remove that when I'm finished with development?

What would I have to do to HibernateUtil.java to make this work?

Get rid of the if (s == null) {} in HibernateUtil.currentSession() ?

Drew


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 24, 2004 7:33 pm 
Beginner
Beginner

Joined: Wed Mar 17, 2004 4:13 pm
Posts: 21
Location: San Diego, CA
I'm confused about HibernateUtil. I can't find this class in the api documentation or the src code I have. Is this util class your own?

Anyways, your code change looks like it would reload the configuration for every session. You could do that. But, of course, this will slow down your development too.

Since this is for dev only, when I do things like this, I like to make a utility button or JSP or something that I can call while my app is up that will force a config reload. That way, I can quickly force a reload manually and still guage performace that's close to production. It's simple, but that's what I like.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 24, 2004 8:34 pm 
Newbie

Joined: Wed Mar 24, 2004 10:51 am
Posts: 12
It's just this little class I pulled out of the quickstart:

Code:
import net.sf.hibernate.*;
import net.sf.hibernate.cfg.*;

public class HibernateUtil {
  private static final SessionFactory sessionFactory;
  static {
    try {
      sessionFactory = new Configuration().configure().buildSessionFactory();
    }
    catch (HibernateException ex) {
      throw new RuntimeException("Exception building SessionFactory: " +
                                 ex.getMessage(), 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();
    }
  }
}


I think I like your idea better.

Do you have any code snippets that show how to force the config to reload?

I've just started working with Hibernate a few hours ago.

It would be nice to have something that I could just drop in the container to do this. :)

Drew


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 24, 2004 9:20 pm 
Beginner
Beginner

Joined: Wed Mar 17, 2004 4:13 pm
Posts: 21
Location: San Diego, CA
Oh, I see. Then getting rid of that line wouldn't do it. You need a new session factory from a new configuration.

I think you need to add a reload method to the HibernateUtil. Something like:
Code:
private static void reloadConfig()
    throws HibernateException
{
    sessionFactory = new Configuration().configure().buildSessionFactory();
}


Then, your code would just call this new method HibernateUtil.reloadConfig() when you want to force a reload.

Chrisjan


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 24, 2004 9:27 pm 
Newbie

Joined: Wed Mar 24, 2004 10:51 am
Posts: 12
Thanks, I'll give it a try.

Drew


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