-->
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.  [ 6 posts ] 
Author Message
 Post subject: Delayed/lazy startup of Enitiy Manager Factory
PostPosted: Mon Feb 04, 2008 10:40 am 
Newbie

Joined: Tue Aug 03, 2004 2:54 pm
Posts: 9
Hi

I am using hibernate as my JPA implementation, but I assume my question applies to hibernate in general.

It takes quite some time, a minute or two, to startup the entity manager factory (similar to a session) because hibernate has to read through all classes on the classpath, looking for @Entity annotations, and then parse them. It takes so long because its a very large project and there are lots of entities to parse.

1) Is there any way to delay the startup of the entity manager factory until the first time the database is needed?

2) Or better yet to serialize the factory after startup and read it back in the next startup. I realize this would only work if the database tier didn't change in between startups, but this method would actually be preferred in our environment.

Thanks all!
Nate


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 04, 2008 12:10 pm 
Newbie

Joined: Tue Aug 03, 2004 2:54 pm
Posts: 9
Something like this, but for JPA annotations would be awesome... does anyone know if it exists?

http://www.hibernate.org/194.html


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 04, 2008 2:12 pm 
Newbie

Joined: Mon Feb 04, 2008 7:29 am
Posts: 7
Location: Belarus
Hello.

You can enable dynamic SQL generation to reduce startup delay. Can you try this and compare the results?

_________________
Alexander Semenov

My Jabber ID: bohtvaroh@jabby.org


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 04, 2008 3:29 pm 
Newbie

Joined: Tue Aug 03, 2004 2:54 pm
Posts: 9
Bohtvaroh wrote:
You can enable dynamic SQL generation to reduce startup delay.


I'm looking for a global property to set for this... is there one, or is this a per mapping setting?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 04, 2008 3:55 pm 
Newbie

Joined: Mon Feb 04, 2008 7:29 am
Posts: 7
Location: Belarus
natjohns wrote:
I'm looking for a global property to set for this... is there one, or is this a per mapping setting?


Yeah, unfortunately this is the per class setting.

_________________
Alexander Semenov

My Jabber ID: bohtvaroh@jabby.org


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 04, 2008 4:46 pm 
Newbie

Joined: Tue Aug 03, 2004 2:54 pm
Posts: 9
Well, I overwrote HibernatePersistence.java for now... not ideal, but speeds it up by more than a factor of 2. The code needs cleaned up, but here's the idea...

Code:
   public EntityManagerFactory createContainerEntityManagerFactory(PersistenceUnitInfo info, Map map) {
      if (factory != null) {
         return factory;
      }

      Ejb3Configuration cfg = null;

      try {
         ObjectInputStream in = new ObjectInputStream(new FileInputStream("/tmp/Ejb3Configuration.out"));
         cfg = (Ejb3Configuration) in.readObject();
         factory = cfg.buildEntityManagerFactory();
      } catch (Exception e) {
         // Serialized configuration not found, so build it and serialize it for next time.
         try {
            if (cfg == null) {
               cfg = new Ejb3Configuration();
               Ejb3Configuration configured = cfg.configure(info, map);
               if (configured != null) {
                  factory = configured.buildEntityManagerFactory();
                  ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("/tmp/Ejb3Configuration.out"));
                  oos.writeObject(configured);
                  oos.close();
               }
            }
         } catch (Exception e2) {
            e2.printStackTrace();
         }
      }

      return factory;
   }


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