Announcement Announcement Module
Collapse
No announcement yet.
Working with two data sources with EclipseLink Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Working with two data sources with EclipseLink

    Hi,

    Is it possible to have several beans working each of them accessing its own datasource using EclipseLink ?

    I want to have access to two databases (kind of client and server) which have exactly the same schema (same entities) and be able to read and write to/from them both using the same DAOs.
    Is this possible?

    I am working in a standalone Java SE app with Spring + EclipseLink.

    my appContext is as bellow:

    and to access the server i am using this line :
    Code:
    springContext.getBean("serverMacsConfigurationService");
    but it is returning the one connected to the localDatabase instead of the server.

    Any clue why???


    spring.xml
    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
    	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx"
    	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
               http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
    
    	<import resource="spring-local.xml" />
    	<import resource="spring-server.xml" />
    
    </beans>
    spring-local.xml:
    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
    	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx"
    	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
               http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
    
    	<!-- Local Database DataSource -->
    	<bean id="localDataSource"
    		class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    	</bean>
    
    	<!-- JPA EntityManagerFactory -->
    	<bean id="localEntityManagerFactory"
    		class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    		<property name="persistenceUnitName" value="macsPersistenceUnit" />
    		<property name="dataSource" ref="localDataSource" />
    		<property name="jpaDialect">
    			<bean class="org.springframework.orm.jpa.vendor.EclipseLinkJpaDialect" />
    		</property>
    		<property name="jpaVendorAdapter">
    			<bean id="jpaAdapter"
    				class="org.springframework.orm.jpa.vendor.EclipseLinkJpaVendorAdapter">
    				<property name="databasePlatform"
    					value="org.eclipse.persistence.platform.database.MySQLPlatform" />
    				<property name="showSql" value="true" />
    			</bean>
    		</property>
    	</bean>
    	<!--
    		A simple transaction manager for our (single) EntityManagerFactory.
    		DataSourceTransactionManager
    	-->
    	<bean id="localTransactionManager"
    		class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    		<property name="dataSource" ref="localDataSource" />
    		<!--
    			<property name="entityManagerFactory" ref="localEntityManagerFactory"
    			/> <property name="jpaDialect"> <bean
    			class="org.springframework.orm.jpa.vendor.EclipseLinkJpaDialect" />
    			</property>
    		-->
    	</bean>
    	<tx:annotation-driven transaction-manager="localTransactionManager" />
    
    	<!-- DAO Beans -->
    	<bean id="localConfigurationDefinitionDAOImpl"
    		class="com.a.macs.common.dao.configurationDefinition.ConfigurationDefinitionDAOImpl">
    		<property name="entityManagerFactory" ref="localEntityManagerFactory" />
    	</bean>
    	<bean id="localConfigurationInstantiationDAOImpl"
    		class="com.a.macs.common.dao.configurationInstantiation.ConfigurationInstantiationDAOImpl">
    		<property name="entityManagerFactory" ref="localEntityManagerFactory" />
    	</bean>
    	<bean id="localMacsConfigurationDAOImpl"
    		class="com.a.macs.common.dao.macsConfiguration.MacsConfigurationDAOImpl">
    		<property name="entityManagerFactory" ref="localEntityManagerFactory" />
    	</bean>
    	<bean id="localDatabaseUtilsDAOImpl"
    		class="com.a.macs.common.dao.DatabaseUtils.DatabaseUtilsDAOImpl">
    		<property name="dataSource" ref="dataSourcePOH" />
    	</bean>
    
    	<!-- Client Service Beans -->
    	<bean id="configurationDefinitionService"
    		class="com.a.macs.common.services.ConfigurationDefinitionService">
    		<property name="configurationDefinitionDAOImpl" ref="localConfigurationDefinitionDAOImpl" />
    		<property name="configurationInstantiationService" ref="configurationInstantiationService" />
    	</bean>
    	<bean id="configurationInstantiationService"
    		class="com.a.macs.common.services.ConfigurationInstantiationService">
    		<property name="configurationInstantiationDAOImpl" ref="localConfigurationInstantiationDAOImpl" />
    		<property name="macsConfigurationService" ref="macsConfigurationService" />
    		<property name="configurationDefinitionService" ref="configurationDefinitionService" />
    	</bean>
    	<bean id="macsConfigurationService"
    		class="com.a.macs.common.services.MacsConfigurationService">
    		<property name="macsConfigurationDAOImpl" ref="localMacsConfigurationDAOImpl" />
    		<property name="configurationDefinitionService" ref="configurationDefinitionService" />
    	</bean>
    
    	<bean id="databaseUtilsService"
    		class="com.a.macs.common.services.DatabaseUtilsService">
    		<property name="databaseUtilsDAOImpl" ref="localDatabaseUtilsDAOImpl" />
    	</bean>
    	<bean id="POHDatabaseService"
    		class="com.a.macs.common.services.POHDatabaseService">
    		<property name="databaseUtilsService" ref="databaseUtilsService" />
    	</bean>
    
    </beans>
    spring-server.xml
    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
    	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx"
    	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
               http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
    
    	<!-- Server Database DataSource -->
    	<bean id="serverDataSource"
    		class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    	</bean>
    
    	<!-- JPA EntityManagerFactory -->
    	<bean id="serverEntityManagerFactory"
    		class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    		<property name="persistenceUnitName" value="macsPersistenceUnit" />
    		<property name="dataSource" ref="serverDataSource" />
    		<property name="jpaDialect">
    			<bean class="org.springframework.orm.jpa.vendor.EclipseLinkJpaDialect" />
    		</property>
    		<property name="jpaVendorAdapter">
    			<bean id="jpaAdapter"
    				class="org.springframework.orm.jpa.vendor.EclipseLinkJpaVendorAdapter">
    				<property name="databasePlatform"
    					value="org.eclipse.persistence.platform.database.MySQLPlatform" />
    				<property name="showSql" value="true" />
    			</bean>
    		</property>
    	</bean>
    
    	<!-- Transaction Manager -->
    	<bean id="serverTransactionManager"
    		class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    		<property name="dataSource" ref="serverDataSource" />
    		<!--
    			<property name="entityManagerFactory"
    			ref="serverEntityManagerFactory" /> <property name="jpaDialect">
    			<bean
    			class="org.springframework.orm.jpa.vendor.EclipseLinkJpaDialect" />
    			</property>
    		-->
    	</bean>
    	<tx:annotation-driven transaction-manager="serverTransactionManager" />
    	<tx:jta-transaction-manager>
    		<ref bean="serverTransactionManager" />
    	</tx:jta-transaction-manager>
    	<!-- DAO Beans -->
    	<bean id="serverConfigurationDefinitionDAOImpl"
    		class="com.a.macs.common.dao.configurationDefinition.ConfigurationDefinitionDAOImpl">
    		<property name="entityManagerFactory" ref="serverEntityManagerFactory" />
    	</bean>
    	<bean id="serverConfigurationInstantiationDAOImpl"
    		class="com.a.macs.common.dao.configurationInstantiation.ConfigurationInstantiationDAOImpl">
    		<property name="entityManagerFactory" ref="serverEntityManagerFactory" />
    	</bean>
    	<bean id="serverMacsConfigurationDAOImpl"
    		class="com.a.macs.common.dao.macsConfiguration.MacsConfigurationDAOImpl">
    		<property name="entityManagerFactory" ref="serverEntityManagerFactory" />
    	</bean>
    
    	<!-- Server Service Beans -->
    	<bean id="serverConfigurationDefinitionService"
    		class="com.a.macs.common.services.ConfigurationDefinitionService">
    		<property name="configurationDefinitionDAOImpl" ref="serverConfigurationDefinitionDAOImpl" />
    		<property name="configurationInstantiationService" ref="serverConfigurationInstantiationService" />
    	</bean>
    	<bean id="serverConfigurationInstantiationService"
    		class="com.a.macs.common.services.ConfigurationInstantiationService">
    		<property name="configurationInstantiationDAOImpl" ref="serverConfigurationInstantiationDAOImpl" />
    		<property name="macsConfigurationService" ref="serverMacsConfigurationService" />
    		<property name="configurationDefinitionService" ref="serverConfigurationDefinitionService" />
    	</bean>
    	<bean id="serverMacsConfigurationService"
    		class="com.a.macs.common.services.MacsConfigurationService">
    		<property name="macsConfigurationDAOImpl" ref="serverMacsConfigurationDAOImpl" />
    		<property name="configurationDefinitionService" ref="serverConfigurationDefinitionService" />
    	</bean>
    
    </beans>
    (i've passed the last days trying to accomplish this, and so far it seems impossible)

  • #2
    Same prob

    I'm not trying to use 2 identical databases but 2 different schemas in one ORA-DB.
    I configured to datasources, ems and jpaTemplates but it doesn't work. Seems, that there is only one connection to db.

    Any help?

    Comment

    Working...
    X