2

I have 2 separate modules in separate .jars which operate on 2 different databases.

The applicationContext-hibernate.xml files contain unique names for the session factories, and the web.xml loads (should load? I hope so.) both context.xml files. I also defined one of the custom sessionFactory namesin the web.xml, as you cen see here:

web.xml

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath*:/applicationContext-hibernate.xml</param-value>
</context-param>
[...]
<filter>
    <filter-name>hibernateFilter</filter-name>
    <filter-class>
        org.springframework.orm.hibernate3.support.OpenSessionInViewFilter
    </filter-class>
    <init-param>
        <param-name>sessionFactoryBeanName</param-name>
        <param-value>customsessionFactory</param-value>
    </init-param>
</filter>

So the first database connection works as intended. But how do I go about to add the second one? Only one of the database beans is available from the context (of course, since the other one is not added)

I only found "How to handle multiple database connections using session factories with Spring and Hibernate" but I don't understand the answer there.

5 Answers 5

4
+50

as long as you have defined the 2 sessionFactories with different ID's.. you can pass the path of the 2nd xml file seperating it with a comma

<param-value>classpath*:/applicationContext-hibernate.xml, <<2nd file reference>></param-value>

Once you have this setup.. spring will be able to init 2 sessionFactories..

- Option 1

you need to create 2 instances of hibernateTemplate (one for each session Factory) and inject them both into your DAO, this way you can use the hibernatetemplate to work with multiple sessionFactories..

- Option 2

while working with HibernateDaoSupport you can use the method createHibernateTemplate(sesionFactoryReference) to create a template object with a specific sessionFactory.

Sign up to request clarification or add additional context in comments.

Comments

3

If I understand correctly, you have two files with the same name (and package) in two jars in the classpath, and hope that the ClassLoader will load both.

There is no way for this to work: when the ClassLoader is asked to load a resource by name, it scans all the jars and directories in its classpath, in order, and returns the first resource found with this name.

You have to name your context files with a different name (or package), and load them both. If you want both sessions to be opened in view, you will also have to declare two different filters.

2 Comments

Thanks, this might help a bit. Can I simply set two context-param entries (with different names in the param-value? When declaring 2 filters, do they have to be the same name (hibernateFilter)?
No, the name of a context-parame must be unique. You have to list the two files, separated by a comma, in the param-value. The name of the filters must also be unique. Choose the names you want.
2

since you are using spring and hibernate already, if you go by JPA and hibernate mapping you can define 2 different entity managers in the spring context xml and when you are injecting the entity manager in your DAO layer you can inject using the entity manager name. This way you can talk to multiple database connections. I have configured this myself previously.

Below is a link which i got searching the web, but this is what i exactly did http://viralpatel.net/blogs/2011/01/spring-roo-two-database-configuration.html

Comments

1

The usual way to do this with Spring is to have multiple context files. If you want to make sure that both are found, put them in different packages.

Make sure your beans are named uniquely, e.g. each SessionFactory bean needs to have unique ID, otherwise they will overwrite each other.

Here's a hint for finding out whether they are loaded and constructed by Spring: Turn on logging on debug level for Spring (e.g. in Log4J), Spring will show which files and beans it loads and constructs. This is really helpful.

If both datasources are to be used in the same transaction, make sure that you're using a TransactionManager, preferably also XA (two-phase commit). When dealing with multiple datasources, I usually have Spring provide the transaction demarcation at the service layer. Inside of the service method, everything runs in the same transaction, and I can use multiple datasources there.

Another alternative if both schemas are on the same database is to set up Hibernate to connect to one schema and access the second schema's table through the first one. You can do that in Oracle using grants and synonyms. Is that an option for you, or do you really need to connect to two different databases?

Comments

1

If you can move to Hibernate 4, you can check Multi-tenancy in Hibernate but that requires moving to Spring 3.1 which may introduce other problems.

1 Comment

Thanks, I will check that out. I believe I have to move to H4 and S3.1 anyway soon.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.