Results 1 to 2 of 2

Thread: Spring + JPA + Hibernate and multiple Database connection

  1. #1
    Join Date
    Nov 2011
    Posts
    2

    Default 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>
    
    <bean id="entityManagerFactory"
    		class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"
    		p:dataSource-ref="dataSource" 
    		p:persistenceXmlLocation="classpath:/resources/config/persistence/persistence.xml"
    		p:jpaVendorAdapter-ref="jpaVendorAdapter">
    		<property name="jpaPropertyMap">
    			<map>
    				<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}" />
    			</map>
    		</property>
    </bean>
    
    <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}" />
    </bean>
    
    <tx:advice id="txAdvice" transaction-manager="txManager">
    	    <tx:attributes>
    	    	<tx:method name="get*" read-only="true"/>
        		<tx:method name="*"/>
      	     </tx:attributes>
     </tx:advice>
      
     <aop:config>
        	<aop:pointcut id="serviceOperation" expression="execution(* setting.springjpa.service.*Service.*(..))"/>
        	<aop:advisor advice-ref="txAdvice" pointcut-ref="serviceOperation"/>
      </aop:config>
      
      <bean id="txManager" class="org.springframework.orm.jpa.JpaTransactionManager"
      		p:entityManagerFactory-ref="entityManagerFactory">
      </bean>

    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. #2
    Join Date
    Jun 2010
    Posts
    440

    Default

    @pau,

    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 @ http://viralpatel.net/blogs/2011/01/...iguration.html

    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.

    B.Roogards
    jD @ http://pragmatikroo.blogspot.com

    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 at 12:17 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •