-->
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: Using multiple databases
PostPosted: Wed Jan 21, 2004 10:36 am 
Beginner
Beginner

Joined: Wed Jan 21, 2004 10:15 am
Posts: 24
Location: Munich, Germany
Hello!

I have to access many MySQL databases that share the same schema via Hibernate. I can't add a connection parameter set for all of them to a configuration file, since they are very many and their number increases. Nevertheless, they all reside on the same MySQL server and the only difference in the connection parameters is their database name.

Is there a way to get different Sessions or SessionFactorys depending on a parameter (i.e. the database name)? Or do I have to create the connections by myself and hand them to the Session? In addition, I'd like to use the Spring Framework, which hides very much of the session stuff, which makes it more complicated, I suppose...

Does anybody have some experience with this scenario?

Thanks in advance!
Christian Gruber


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jan 21, 2004 10:40 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
Use Configuration.configure(DOM) api and build the appropriate DOM based on a template.

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jan 21, 2004 12:18 pm 
Regular
Regular

Joined: Tue Aug 26, 2003 3:09 pm
Posts: 58
You can change the hibernate.connection.url property just before you build the session factory by calling Configuration.addProperties().

You could also leave the database name off the hibernate.connection.url property and set the hibernate.default_schema property to the name of the database. If you do this though, you need to subclass MySQLDialect and override getSchemaSeperator() and return a dot instead of an underscore.

I'm not entirely sure how you would do this with spring. I guess it depends on how/when you create the spring context. But it looks like there are a couple of ways to set those properties using the LocalSessionFactoryBean. First is by setting either of the above properties in the LocalSessionFactoryBean definition's hibernateProperties property. The other is by subclassing LocalSessionFactoryBean and override postProcessConfiguration() to add the properties manually.

Hope this helps...

Joe


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 27, 2004 10:48 am 
Beginner
Beginner

Joined: Wed Jan 21, 2004 10:15 am
Posts: 24
Location: Munich, Germany
Hi!

Thanks for your help! Here's what I have found out and tried:

In Hibernate, It was rather easy:

Code:
Configuration configuration = new Configuration().configure();
configuration.setProperty("hibernate.connection.url", ".......");
SessionFactory sessionFactory = configuration.buildSessionFactory();


In Spring Framework, it was more complicated, but I have now the following solution, which might be a bit strange and perhaps not according to the intention of Spring:

In the beans XML file, I use a PropertyPlaceholderConfigurer to replace bean values like "${db}" by the real database. The database name which is determined at runtime is stored in a singleton class. The "db" property is fetched out of this singleton class in an own class that is derived from a PropertiesFactoryBean. For each session to a specific database, I create a new ApplicationContext after setting the database name in my singleton class. Sounds complicated, but it works... Or does somebody have a better idea?

Greetings,
Christian


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 27, 2004 1:31 pm 
Senior
Senior

Joined: Wed Aug 27, 2003 6:04 am
Posts: 161
Location: Linz, Austria
gruberc wrote:
In Spring Framework, it was more complicated, but I have now the following solution, which might be a bit strange and perhaps not according to the intention of Spring:

In the beans XML file, I use a PropertyPlaceholderConfigurer to replace bean values like "${db}" by the real database. The database name which is determined at runtime is stored in a singleton class. The "db" property is fetched out of this singleton class in an own class that is derived from a PropertiesFactoryBean. For each session to a specific database, I create a new ApplicationContext after setting the database name in my singleton class. Sounds complicated, but it works... Or does somebody have a better idea?


First of all, I assume you mean "for each SessionFactory to a specific database, I create a new ApplicationContext...".

If you're just doing this to create a LocalSessionFactoryBean for a specific database URL, then there is an easier way: Simply instantiate LocalSessionFactoryBean yourself, invoke its setters, invoke afterPropertiesSet, and fetch the SessionFactory via getObject():

Code:
LocalSessionFactoryBean lsfb = new LocalSessionFactoryBean();
lsfb.setMappingResources(...);
lsfb.setHibernateProperties(...);
lsfb.afterPropertiesSet();
SessionFactory sf = (SessionFactory) lsfb.getObject();


Instead of setting the database URL via "hibernateProperties", you could also pass a corresponding pre-determined DataSource into setDataSource.

Note that you can use Spring's transaction management and HibernateTemplate with any SessionFactory, even if manually created. HibernateTransactionManager and HibernateTemplate just take a SessionFactory reference in their respective setters, not caring where it came from - LocalSessionFactoryBean is just a convenient way to create one.

Even if our documentation and our examples focus on using Spring as framework, many parts of it can also be used in a library style. After all, that's the point of an IoC framework: The components do not unnecessarily depend on the framework but typically lend themselves nicely to standalone usage as plain objects.

Juergen


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.