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!


Reply With Quote