-->
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.  [ 5 posts ] 
Author Message
 Post subject: One SessionFactory, Many Databases
PostPosted: Thu Aug 19, 2004 9:46 am 
Beginner
Beginner

Joined: Thu Aug 19, 2004 5:36 am
Posts: 30
Location: Italy
It would be nice if I could use only one SessionFactory to work with many databases; obviously I mean many databases on the same database server and with identical schema.

I think to define one Configuration and obtain a SessionFactory from it; after that, obtain Session from the SessionFactory providing the JDBC Connection to the choosen database (even if I'm not very happy to provide connection...).

But ...
Is this working correctly?
And, if it is possible, is there a best approach?

Thanks


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 19, 2004 10:00 am 
Senior
Senior

Joined: Tue Sep 23, 2003 8:18 am
Posts: 137
Location: Johannesburg, South Africa
I might be corrected by one of the Admins, but I believe that the rules is simple, only one SessionFactory per Database, as it in essence represents control of all the various sessions that will act on that database.

As such, each SessionFactory has its own config and sets of mapping files. I have a Database class where I map to 8 different databases using 8 different SessionFactories.

It could also be seen as good practise to keep different DB configurations nicely separated.

-G


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 19, 2004 10:28 am 
Beginner
Beginner

Joined: Thu Aug 19, 2004 5:36 am
Posts: 30
Location: Italy
Brannor McThife wrote:
I might be corrected by one of the Admins, but I believe that the rules is simple, only one SessionFactory per Database, as it in essence represents control of all the various sessions that will act on that database.

As such, each SessionFactory has its own config and sets of mapping files. I have a Database class where I map to 8 different databases using 8 different SessionFactories.

It could also be seen as good practise to keep different DB configurations nicely separated.

-G


First of all, thank you very much for replying so quickly.

From the point of view of a good design, I absolutely agree with you.
At the moment I'm starting a new project with a database schema that will have more or less one hundred of quite big tables; and the application require to have different databases built on the same schema (the business logic is the same, of course).
Since I read from documentation that a SessionFactory is a "hevy" object, that contains all mappings and statements, I would like to know if this approach was possible.

Thank you very much, Brannor


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 19, 2004 10:41 am 
Senior
Senior

Joined: Tue Sep 23, 2003 8:18 am
Posts: 137
Location: Johannesburg, South Africa
Some of my databases are the same, i.e. same table names, same tables, etc. except on different machines. To do this is a bit of a drag, in that you basically copy all the mapping files to a new directory, and change a few minor settings. The nice thing is though, that all the different mapping files can use the exact same Java objects, so you can quite literally load an object (row) from on SessionFactory, and save that very same object into another SessionFactory.

I understand what you would like to do, and if the tables are the same, and the logic the same, then you can create methods which accept some form of identifier for which database you want to work with (a variable) and have a central Database class that takes that variable and knows which session factory to use, that way, the same workflow can be used on multiple databases.

I hope that's not too confusing.

As I like showing people, it's really nice to be able to load one object tree from one database (SessionFactory), and simply tell it to save the entire tree into another database (SessionFactory), and that all in a few lines. :)

-G


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 19, 2004 11:52 am 
Beginner
Beginner

Joined: Thu Aug 19, 2004 5:36 am
Posts: 30
Location: Italy
Brannor McThife wrote:
I understand what you would like to do, and if the tables are the same, and the logic the same, then you can create methods which accept some form of identifier for which database you want to work with (a variable) and have a central Database class that takes that variable and knows which session factory to use, that way, the same workflow can be used on multiple databases.

I hope that's not too confusing.


Absolutely clear; I think I will create this class Database, holding all SessionFactory, and bound it to a JNDI resource or simply to an application context param in the servlet container.

Quote:

As I like showing people, it's really nice to be able to load one object tree from one database (SessionFactory), and simply tell it to save the entire tree into another database (SessionFactory), and that all in a few lines. :)

-G


You are right. Hibernate is performing magic...


Bye


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