-->
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.  [ 3 posts ] 
Author Message
 Post subject: Loading the config file dynamically
PostPosted: Tue Sep 08, 2009 2:13 am 
Newbie

Joined: Thu Aug 27, 2009 3:56 pm
Posts: 10
I'm trying desperately to get hibernate to load the hibernate.cfg.xml file from a path I set in the class that does all the factory configuration. It's not working. Here's the guts of the class that's supposed to do that:
Code:
private static Logger log = Logger.getLogger(MMHibernateSessionFactoryAnnotations.class.getName());

       /**
        * Location of hibernate.cfg.xml file.
        * Location should be on the classpath as Hibernate uses 
        * #resourceAsStream style lookup for its configuration file.
        * The default classpath location of the hibernate config file is
        * in the default package. Use #setConfigFile() to update
        * the location of the configuration file for the current session.   
        */
       private static String CONFIG_FILE_LOCATION = PostEC.hibernatePropertiesFilePath;
      private static final ThreadLocal<Session> threadLocal = new ThreadLocal<Session>();
       private  static Configuration configuration = new AnnotationConfiguration();   
       private static org.hibernate.SessionFactory sessionFactory;
       private static String configFile = CONFIG_FILE_LOCATION;

      static {
          try {
             log.debug("The factory thinks the hibernate config file is at: " + configFile);
            configuration.configure(configFile);
            sessionFactory = configuration.buildSessionFactory();
         } catch (Exception e) {
            System.err
                  .println("%%%% Error Creating SessionFactory %%%%");
            e.printStackTrace();
         }
       }
       private MMHibernateSessionFactoryAnnotations() {
       }
      
      /**
        * Returns the ThreadLocal Session instance.  Lazy initialize
        * the <code>SessionFactory</code> if needed.
        *
        *  @return Session
        *  @throws HibernateException
        */
       public static Session getSession() throws HibernateException {
           Session session = (Session) threadLocal.get();
           log.debug("getting a session");
         if (session == null || !session.isOpen()) {
            if (sessionFactory == null) {
               rebuildSessionFactory();
            }
            session = (sessionFactory != null) ? sessionFactory.openSession()
                  : null;
            threadLocal.set(session);
         }

           return session;
       }

      /**
        *  Rebuild hibernate session factory
        *
        */
      public static void rebuildSessionFactory() {
         log.debug("rebuilding session factory");
         try {
            configuration.configure(configFile);
            sessionFactory = configuration.buildSessionFactory();
         } catch (Exception e) {
            System.err
                  .println("%%%% Error Creating SessionFactory %%%%");
            e.printStackTrace();
         }
      }

      /**
        *  Close the single hibernate session instance.
        *
        *  @throws HibernateException
        */
       public static void closeSession() throws HibernateException {
           Session session = (Session) threadLocal.get();
           threadLocal.set(null);

           if (session != null) {
               session.close();
           }
       }

      /**
        *  return session factory
        *
        */
      public static org.hibernate.SessionFactory getSessionFactory() {
         return sessionFactory;
      }

      /**
        *  return session factory
        *
        *   session factory will be rebuilded in the next call
        */
      public static void setConfigFile(String configFile) {
         log.debug("the new configFile string is: " + configFile);
         MMHibernateSessionFactoryAnnotations.configFile = configFile;
         sessionFactory = null;
      }

      /**
        *  return hibernate configuration
        *
        */
      public static Configuration getConfiguration() {
         return configuration;
      }


And here's the code I use to get it to do what it says it will do and doesn't:
Code:
log.debug("the path to the hibernate config from properties: " + PostEC.hibernatePropertiesFilePath);
         MMHibernateSessionFactoryAnnotations.setConfigFile(PostEC.hibernatePropertiesFilePath);
         Session theSess = MMHibernateSessionFactoryAnnotations.getSession();


The property is set, and it prints out properly, and all I get is the hateful hibernate.properties not found, like this:
Code:
Time spent de-serializing 1 records from disk: 525 ms at:1.9083969465648853/second
Sep 08 02:01:53 (Environment.java:514) INFO  org.hibernate.cfg.Environment  - Hibernate 3.2.5
Sep 08 02:01:53 (Environment.java:547) INFO  org.hibernate.cfg.Environment  - hibernate.properties not found


and the the app exits.

This factory class was generated using the hibernate eclipse plug ins, and given that I had to pretty much redo the pojo files it also generated by hand, I wouldn't be surprised if this generated class failed as well. However, this link indicates that it is the correct approach:
https://www.hibernate.org/171.html

How many chickens and to what god must they be sacrificed to get this working?


Top
 Profile  
 
 Post subject: Re: Loading the config file dynamically
PostPosted: Tue Sep 08, 2009 3:13 am 
Newbie

Joined: Thu Aug 27, 2009 3:56 pm
Posts: 10
Solution:

You can't get a new SessionFactory or configure it inside of another thread, which is what I was doing. I have a main process, which spins off some threads that are designed to just chew through data and insert en masse. I was trying to set the new file path inside one of the threads, and it was just crashing the app. I moved the call into the main thread, and it's working now, loading and configuring based on the dynamically set path to the hibernate.cfg.xml file. It all looks good, but then ends with this now:

Code:
Sep 08 02:59:18 (Configuration.java:1426) INFO  org.hibernate.cfg.Configuration  - configuring from resource: /hibernate.cfg.xml
Sep 08 02:59:18 (Configuration.java:1403) INFO  org.hibernate.cfg.Configuration  - Configuration resource: /hibernate.cfg.xml
Attempting to delete the empty log file.
Successfully deleted the empty log file.

Time spent de-serializing 1 records from disk: 519 ms at:1.9267822736030829/second
Sep 08 02:59:18 (Configuration.java:1541) INFO  org.hibernate.cfg.Configuration  - Configured SessionFactory: null


It's interesting that I get this AFTER I get positive indication that all the properties are read, the annotations and mappings are bound correctly and so on. Unbelievable what a pain in the ass getting a simple app running with hibernate is. In retrospect, I should have stuck with Enterprise Objects Frameworks and focused my efforts on using that outside of a normal WebObjects app; I'm sure I'd be done right now and sleeping instead of debugging this crap.


Top
 Profile  
 
 Post subject: Re: Loading the config file dynamically
PostPosted: Tue Sep 08, 2009 4:39 am 
Newbie

Joined: Thu Aug 27, 2009 3:56 pm
Posts: 10
OK, I am just in a recursive loop of hibernate errors. I finally thought I had this working, then discovered otherwise. I have a file hibernate.cfg.xml located at the root of my file system, just for purposes of testing this out. Here's the output I get now:

Quote:
The factory thinks the hibernate config file is at: /hibernate.cfg.xml
Sep 08 04:33:14 (Configuration.java:1426) INFO org.hibernate.cfg.Configuration - configuring from resource: /hibernate.cfg.xml
Sep 08 04:33:14 (Configuration.java:1403) INFO org.hibernate.cfg.Configuration - Configuration resource: /hibernate.cfg.xml
%%%% Error Creating SessionFactory %%%%
org.hibernate.HibernateException: /hibernate.cfg.xml not found
at org.hibernate.util.ConfigHelper.getResourceAsStream(ConfigHelper.java:147)
at org.hibernate.cfg.Configuration.getConfigurationInputStream(Configuration.java:1405)
at org.hibernate.cfg.Configuration.configure(Configuration.java:1427)
at org.hibernate.cfg.AnnotationConfiguration.configure(AnnotationConfiguration.java:972)
at org.hibernate.cfg.AnnotationConfiguration.configure(AnnotationConfiguration.java:69)
at com.medialets.Ingest.MMHibernateSessionFactoryAnnotations.<clinit>(MMHibernateSessionFactoryAnnotations.java:41)
at com.medialets.service.PostEC.<init>(PostEC.java:159)
at com.medialets.service.PostEC.main(PostEC.java:127)


WTF am I getting this exception? Clearly, it's getting the path as it should, but then this exception. Is it looking within the classpath, or is it looking on the file system? If it's looking in the classpath, why? And how can you make it find the file?


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