-->
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: SessionFactory initialization
PostPosted: Mon Jun 24, 2013 11:11 am 
Newbie

Joined: Mon Jun 24, 2013 7:58 am
Posts: 1
Hi!

I've been developing with Hibernate for a while and have just now been faced with this question,

What is the best approach to open the Session Factory? Open it when the application starts? Or for each set of instructions in the database?
Well, I ask this because I've been opening it for each set of instructions.

For example, when I want to list some data I do the following (Only map the necessary tables)
Code:
Configuration configuration = new Configuration()         
    .addAnnotatedClass(table1.class)
    .addAnnotatedClass(table2.class)
    .addAnnotatedClass(table3.class)
    .configure();

serviceRegistry = new ServiceRegistryBuilder().applySettings(hibConfiguration.getProperties()).buildServiceRegistry();
sessionFactory = hibConfiguration.buildSessionFactory(serviceRegistry);
session = sessionFactory.withOptions().openSession();

In the Hibernate documentation they say "A SessionFactory is set up once for an application". This would mean that all the tables will be added to the Session Factory on the application start right? Or will be set in the Hibernate.cfg.xml and for consequence will be loaded on the application start.

But does this applies to big databases? My application database has about 150 tables. Wouldn't it be to much heavy?

What do you advice?

Thanks in advance :)


Top
 Profile  
 
 Post subject: Re: SessionFactory initialization
PostPosted: Sat Jul 27, 2013 2:54 am 
Newbie

Joined: Sat Jul 27, 2013 1:47 am
Posts: 5
It wont be heavy. Only Configurations will be loaded. None of the data is going to be loaded.

So it is always a good practice to have one SessionFactory for an application and it has to be initialized on startup.


Top
 Profile  
 
 Post subject: Re: SessionFactory initialization
PostPosted: Sun Jul 28, 2013 5:30 pm 
Newbie

Joined: Wed Mar 19, 2008 11:19 am
Posts: 15
Location: Orlando
You don't "open" a SessionFactory. As the name implies, it's a Factory which produces Session objects. The Session is what you open to get the data. What you probably want to know is if you should use Session-Per-Operation, or Session-Per-Application and the answer is neither. Read up on the Session-Per-Request pattern.


Top
 Profile  
 
 Post subject: Re: SessionFactory initialization
PostPosted: Mon Jul 29, 2013 7:29 am 
Beginner
Beginner

Joined: Wed Feb 06, 2013 2:43 am
Posts: 46
Exactly. There should be only one sessionFactory.

I have around 500 tables and it works good.
And it is not going to load all the Data from those tables. It just loads the structure of tables.

_________________
Thanks,
Ajit Singh
ajits@mindfiresolutions.com
www.mindfiresolutions.com


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.