Announcement Announcement Module
No announcement yet.
Spring + JPA + Hibernate and multiple Database connection Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • Spring + JPA + Hibernate and multiple Database connection

    I have a problem connecting more than one database, I have spent 2 days without succes. I have read through different forums without getting results. I do not understand how something as simple as connecting to two databases so difficult when using Spring + JPA + Hibernate.

    I need two datasource and can make transactions between them. I put my settings for only one connection.

    HTML Code:
     <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
            <property name="driverClassName" value="${jdbc.driverClassName}"/>
            <property name="url" value="${jdbc.url}"/>
            <property name="username" value="${jdbc.username}"/>
            <property name="password" value="${jdbc.password}"/>
    <bean id="entityManagerFactory"
    		<property name="jpaPropertyMap">
    				<entry key="hibernate.generate_statistics" value="${persistence.hibernate.generate_statistics:false}" />
    				<entry key="hibernate.jdbc.use_scrollable_resultset" value="${persistence.hibernate.jdbc.use_scrollable_resultset:true}" />
    <bean id="jpaVendorAdapter" class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
    		<property name="showSql" value="${persistence.showSQL}" />
    		<property name="generateDdl" value="${persistence.generateDdl}" />
    		<property name="databasePlatform" value="${persistence.dialect}" />
    <tx:advice id="txAdvice" transaction-manager="txManager">
    	    	<tx:method name="get*" read-only="true"/>
        		<tx:method name="*"/>
        	<aop:pointcut id="serviceOperation" expression="execution(* setting.springjpa.service.*Service.*(..))"/>
        	<aop:advisor advice-ref="txAdvice" pointcut-ref="serviceOperation"/>
      <bean id="txManager" class="org.springframework.orm.jpa.JpaTransactionManager"

    I read that you could create two LocalContainerEntityManagerFactoryBean, this always gives an exception in the application startup:

    org.springframework.beans.factory.NoSuchBeanDefini tionException: No unique bean of type [javax.persistence.EntityManagerFactory] is defined: expected single bean but found 2

    And I think if I have also created two EntityManagerFactory to create two JpaTransactionManager and that's not the goal.

    In my search for internet or watching Spring documentation to create multiple persistence units must be used org.springframework.orm.jpa.persistenceunit.Defaul tPersistenceUnitManager, I've tried without success, how to tell the DAO datasource to use?

    In other post I read said that Spring with JPA can only connect to a datasource to connect to more than one is to use JNDI, is that true?

    I am new to Spring and whether the approach has to be one or it is advisable to use JNDI, I'm open to suggestions.

    Whatever it takes to complete the information you just have to ask.

    Thank you very much!

  • #2

    I don't think this is still and issue for you anymore... but, It could be for the next colleague developer with the same question though.

    I posted one solution for this configuration @

    BTW, A fellow from Italy is uploading a video about it -as I write- too. Check my blog. I will have the link posted there soon.

    jD @

    I wonder what key words did you use that you didn't find my article in the first place. I can re-tag the article for better location for people in the future. If you remember send them to me. thx.
    Last edited by delgad9; May 4th, 2012, 12:17 PM.