-->
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.  [ 8 posts ] 
Author Message
 Post subject: Case of multiple databases
PostPosted: Tue May 11, 2004 2:09 pm 
Newbie

Joined: Wed Feb 11, 2004 12:03 pm
Posts: 15
Hi,
I'm actually develloping an application using a tomcat webserver, hibernate and multiple mysql databases on differents computers.
I use actually MySQL Connector/J to link one of these database to my web application but as MySQL Connector/J will at the end link others database I was wondering if hibernate will be able to switch from one database to another ? (in sql i just would have to specify manually the database used like this : SELECT dbname.tablename.colname FROM dbname.tablename... but with hibernate xml parsing and HQL ?)

In clear does hibernate support a multiple database architecture ?


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 11, 2004 5:08 pm 
Regular
Regular

Joined: Thu Jan 29, 2004 10:34 am
Posts: 52
Location: Austin, TX
create a new session factory for each database and use the appropriate session (conection) while accessing/creating objects.

the curent design pattern is aimed towards one datasource per database

of course you can't expect associations between objects from different databses to work.


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 11, 2004 7:04 pm 
Newbie

Joined: Wed Feb 11, 2004 12:03 pm
Posts: 15
so you mean that I must put more than one session factory in my hibernate.cfg.xml file ?
Ok with that but how will i then specify the database to use when i create my factory object in my javacode ?
here here is my factory in the xml file :

public void init(FilterConfig filterConfig) throws ServletException

<session-factory>
<property name="connection.datasource">java:comp/env/jdbc/projet2</property>
<property name="show_sql">false</property>
<property name="dialect">net.sf.hibernate.dialect.MySQLDialect</property>
<!-- Mapping files -->
<mapping resource="data/DataSet.hbm.xml"/>
<mapping resource="data/Measure.hbm.xml"/>
<mapping resource="data/Parameter.hbm.xml"/>
</session-factory>

and is the java code i use to initialize my factory :

{
// Initialize hibernate
try
{
factory = new Configuration().configure().buildSessionFactory();
}
catch (HibernateException ex) { throw new ServletException(ex); }
}


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 11, 2004 11:54 pm 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
If these are just different mysql databases on the same physical server, you can also play with the schema attribute in the mappings. Probably you need to create your own dialect overriding getSchemaSeperator()


Top
 Profile  
 
 Post subject:
PostPosted: Wed May 12, 2004 12:13 am 
Regular
Regular

Joined: Thu Jan 29, 2004 10:34 am
Posts: 52
Location: Austin, TX
regardless of how you choose to configure the factory note that it results in a single Confugration object to be created.

in your case there needs to be on cfg.xml for per factory. i prefer to do this programatically (for e.g. having a separate startup bean for each factory).

since you say you have the databases on separate servers, you can init all the connections from a single app server


Top
 Profile  
 
 Post subject:
PostPosted: Wed May 12, 2004 4:28 am 
Newbie

Joined: Wed Feb 11, 2004 12:03 pm
Posts: 15
Wow great !
thanks very much :)


Top
 Profile  
 
 Post subject:
PostPosted: Wed May 12, 2004 4:38 am 
Newbie

Joined: Wed Feb 11, 2004 12:03 pm
Posts: 15
ravi wrote:
create a new session factory for each database and use the appropriate session (conection) while accessing/creating objects.

the curent design pattern is aimed towards one datasource per database

of course you can't expect associations between objects from different databses to work.


Sorry to bother you again but I still don't see what you mean by use the appropriate session where in my java code will I have to specify the database in use and how ^^;
another thing is : How can I have more than one hibernate.cfg.xml ? isn't this file supposed to be placed in the root on my classes diectory ?


Top
 Profile  
 
 Post subject:
PostPosted: Wed May 12, 2004 5:16 am 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
Create two Configuration objects with two hibernate.cfg.xml, build two SessionFactories from those Configurations, get Sessions from each. You can't link database that way, only use them.

_________________
JAVA PERSISTENCE WITH HIBERNATE
http://jpwh.org
Get the book, training, and consulting for your Hibernate team.


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