Using spring cloud namespace and two DataSources

1.3k Views Asked by At

I have a Spring Integration WAR component that I'm updating to run in private PCF. I have two DataSources and a RabbitMQ connection factory defined in the application.

I see an article from Thomas Risberg on using the cloud namespace and handling multiple services of the same time - https://spring.io/blog/2011/11/09/using-cloud-foundry-services-with-spring-part-3-the-cloud-namespace. This is handled by using @Autowired and @Qualifier annotations.

I'm wondering how this can be achieved though when we're not @Autowired and @Qualifier annotations, e.g. wiring a DataSource into a JdbcTemplate. Here we do not have the ability to specify a @Qualifier annotation.

My application is Spring XML config based. I do have ability to use @Autowired and @Qualifier annotations on one of the DataSources, but the other is JPA entity manager. See code snippet.

Any help is much appreciated.

    <bean id="entityManagerFactory"
        class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="persistenceUnitName" value="activity-monitor" />
        <property name="jpaVendorAdapter" ref="jpaVendorAdapter"/>
        <property name="jpaProperties">
            <value>
                hibernate.format_sql=true
            </value>
        </property>
    </bean>

    <beans profile="cloud">
        <cloud:data-source id="dataSource" service-name="actmon-db-service" />
    </beans>

Java Build Pack: java_buildpack_offline java-buildpack-offline-v2.4.zip Spring Auto-reconfiguration version 1.4.0.

UPDATE: This is the full config for both data sources, including PropertySourcesPlaceholderConfigurer with properties loaded from data source using DAO.

<bean id="cic.application.ppc" class="org.springframework.context.support.PropertySourcesPlaceholderConfigurer"> 
    <property name="properties" ref="cic.application.properties"/> 
    <property name="locations" ref="cic.application.propertyLocations"/> 
</bean>

<bean id="cic.application.properties" class="java.util.Properties">
    <constructor-arg value="#{cicPropertiesService.properties}"></constructor-arg>
</bean>

<bean id="cic.properties.propertiesService" name="cicPropertiesService"
    class="com.emc.it.eis.properties.service.DefaultPropertiesService">
    <constructor-arg index="0"
        ref="cic.properties.propertiesDao" />
</bean>

<bean id="cic.properties.propertiesDao" class="com.emc.it.eis.properties.dao.JdbcPropertiesDao">
    <constructor-arg ref="cic.properties.dataSource" />
</bean>

<beans profile="default">
    <jee:jndi-lookup id="cic.properties.dataSource"
        jndi-name="jdbc/intdb" />
</beans>

<beans profile="cloud">
    <cloud:data-source id="cic.properties.dataSource" service-name="oracle-cicadm-db-service" />
</beans>

<beans>
    <bean id="entityManagerFactory"
        class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        <property name="dataSource" ref="actmonDataSource" />
        <property name="persistenceUnitName" value="activity-monitor" />
        <property name="jpaVendorAdapter" ref="jpaVendorAdapter"/>
        <property name="jpaProperties">
            <value>
                hibernate.format_sql=true
            </value>
        </property>
    </bean>

    <bean id="transactionManager"
        class="org.springframework.orm.jpa.JpaTransactionManager">
        <property name="entityManagerFactory" ref="entityManagerFactory" />
    </bean>
</beans>

<beans profile="default">
    <jee:jndi-lookup id="dataSource"
        jndi-name="jdbc/actmon" />
</beans>

<beans profile="cloud">
    <cloud:data-source id="actmonDataSource" service-name="postgres-actmon-db-service" />
</beans>

<beans profile="default,cloud">
    <bean id="jpaVendorAdapter"
        class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
        <property name="database" value="POSTGRESQL" />
    </bean>
</beans>

Output from CF when I deploy https://gist.github.com/anonymous/3986a1a7cea4f20c096e. Note it is skipping auto re-configuration of javax.sql.DataSources

2

There are 2 best solutions below

2
On

First of all, the post from Thomas is pretty old, and references a deprecated support library. Instead of the org.cloudfoundry:cloudfoundry-runtime:0.8.1 dependency, you should use Spring Cloud Connectors dependencies instead.

You can then follow the instructions provided for using XML configuration with Spring Cloud Connectors. With multiple services of the same type, you will need to specify the name of the service for each bean. Following your example, and assuming you created two CF database services named inventory-db and customer-db, that might look something like this:

<bean id="entityManagerFactory"
   class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    <property name="dataSource" ref="inventory-dataSource" />
    <property name="persistenceUnitName" value="activity-monitor" />
    <property name="jpaVendorAdapter" ref="jpaVendorAdapter"/>
    <property name="jpaProperties">
        <value>
            hibernate.format_sql=true
        </value>
    </property>
</bean>

<beans profile="cloud">
    <cloud:data-source id="inventory-dataSource" service-name="inventory-db">
    <cloud:data-source id="customer-dataSource" service-name="customer-db">
</beans>
0
On

I've managed to resolve the issue by using the factory bean used by the spring cloud:data-source, CloudDataSourceFactory. Creating an instance of this and wiring up the config including the service-name of the CF service. This avoids the issue of our PropertySourcesPlaceholderConfigurer trying to use the data source before our the bean has even been defined.

    <!--
        configure cloud data source for using CloudDataSourceFactory; this is what spring cloud:data-source is using;
        required to manually wire this data source bean as cloud:data-source bean gets defined in a phase after our
        PropertySourcesPlaceholderConfigurer bean.
    -->
    <bean id="cic.properties.dataSource" class="org.springframework.cloud.service.relational.CloudDataSourceFactory">
        <constructor-arg value="oracle-cicadm-db-service" />
        <constructor-arg>
            <!-- configuring minimal data source as it is used only to bootstrap properties on app start-up -->
            <bean class="org.springframework.cloud.service.relational.DataSourceConfig">
                <constructor-arg>
                    <bean class="org.springframework.cloud.service.PooledServiceConnectorConfig.PoolConfig">
                        <constructor-arg value="0" />
                        <constructor-arg value="2" />
                        <constructor-arg value="180" />
                    </bean>
                </constructor-arg>
                <!-- ConnectionConfig not required for cic.properties.dataSource so setting to null -->
                <constructor-arg value="#{ null }" />
            </bean>
        </constructor-arg>
    </bean>