我如何使用Spring在同一应用程序中连接两个不同的数据库,一个用于读取,一个用于写入?

问题描述 投票:0回答:1

用例:我正在运行一个带有订单的spring应用程序。我可以为这些订单创建计划。我的开发环境/数据库没有任何订单,它们来自不同的应用程序(我们共享相同的数据库)。因此,有没有一种方法可以从生产数据库中读取订单,但是当我创建计划时,它将被保存到开发数据库中?

我的persistence.xml如下


    <?xml version="1.0" encoding="UTF-8" ?>
    <persistence xmlns="http://java.sun.com/xml/ns/persistence"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
        version="2.0">
        <persistence-unit name="Demo_PU" transaction-type="RESOURCE_LOCAL">
            <provider>org.hibernate.ejb.HibernatePersistence</provider>
            <class>com.entity.ErrorMessage</class>
            <class>com.entity.ErrorMessageTxt</class>
            <class>com.entity.ErrorMessageTxtId</class>
            <class>com.entity.Language</class>
            <class>com.entity.TextEntity</class>
            <properties>
                <property name="hibernate.dialect" value="org.hibernate.dialect.InformixDialect" />
                <property name="hibernate.show_sql" value="false" />
                <property name="hibernate.format_sql" value="true" />
                <property name="hibernate.id.new_generator_mappings" value="true" />

                <property name="hibernate.cache.region.factory_class"
                    value="net.sf.ehcache.hibernate.SingletonEhCacheRegionFactory" />
                <property name="hibernate.cache.use_second_level_cache"
                    value="true" />
                <property name="hibernate.cache.use_query_cache" value="true" />
                <property name="hibernate.generate_statistics" value="false" />

                <property name="hibernate.connection.useUnicode" value="true" />
                <property name="hibernate.connection.characterEncoding"
                    value="UTF-8" />

                <!-- Adding timeouts for Lock Mode and Query -->
                <property name="javax.persistence.query.timeout" value="60000" />
                <property name="javax.persistence.lock.timeout" value="60000" />

            </properties>
        </persistence-unit>
    </persistence>

我的spring-config.xml如下

<?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:aop="http://www.springframework.org/schema/aop"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:task="http://www.springframework.org/schema/task"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="
       http://www.springframework.org/schema/beans 
       http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
       http://www.springframework.org/schema/tx 
       http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
       http://www.springframework.org/schema/context 
       http://www.springframework.org/schema/context/spring-context-3.0.xsd
       http://www.springframework.org/schema/aop 
       http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
       http://www.springframework.org/schema/util 
       http://www.springframework.org/schema/util/spring-util.xsd
       http://www.springframework.org/schema/task 
       http://www.springframework.org/schema/task/spring-task-3.0.xsd">

    <context:component-scan base-package="com.project"/>      
    <context:component-scan base-package="com.project2"/>   

    <task:annotation-driven executor="myExecutor" scheduler="myScheduler"/>
<task:executor id="myExecutor"/>
<task:scheduler id="myScheduler"/>

    <bean id="qa-informix" class="org.springframework.jndi.JndiObjectFactoryBean">
        <property name="jndiName" value="jdbc/dcSysCommon"/>
    </bean>     

    <bean id="pum" class="org.springframework.orm.jpa.persistenceunit.DefaultPersistenceUnitManager">
        <property name="persistenceXmlLocations">
            <list>
                <value>classpath*:META-INF/persistence.xml</value>
            </list>
        </property>
        <property name="dataSources">
            <map>
                <entry key="localDataSource" value-ref="qa-informix" />
            </map>
        </property>
        <property name="defaultDataSource" ref="qa-informix"/>      
    </bean>

    <bean id="emf" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        <property name="persistenceUnitManager" ref="pum" />
        <property name="jpaDialect">
            <bean class="com.project.utils.IsolationSupportHibernateJpaDialect"/>
        </property>
        <property name="persistenceUnitName" value="PYD_PU" />
    </bean>

    <!-- bean post-processor for JPA annotations -->
    <bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" />

    <!-- this enables the configuration of transactional behavior based on annotations -->
    <tx:annotation-driven transaction-manager="txManager"/>

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

    <bean id="interpolator" class="org.hibernate.validator.messageinterpolation.ResourceBundleMessageInterpolator" />       
    <bean id="validator" class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean">
        <property name="messageInterpolator" ref="interpolator" />
    </bean>

    <bean class="com.project3.common.core.bo.LanguageBusinessObject" primary="true" />

    <bean name="restUtils" class="com.project1.utils.RestUtils" />

    <bean id="srvPropertyFile" class="org.springframework.jndi.JndiObjectFactoryBean">
        <property name="jndiName" value="srvPropertyFile" />
    </bean>

      <bean id="beanConfig" class="io.swagger.jaxrs.config.BeanConfig">
        <property name="title" value="Swagger UI for QAS"/>
        <property name="version" value="1.0.0" />
        <property name="schemes" value="http" />
        <property name="host" value="#{srvPropertyFile.hostName}:#{srvPropertyFile.pydPort}" />
        <property name="basePath" value="planyourday/#{srvPropertyFile.countryCode}/#{srvPropertyFile.dcNumber}"/>
        <property name="resourcePackage" value="com.project1.resources"/>
        <property name="license" value="Apache 2.0 License"/>
        <property name="licenseUrl" value="http://www.apache.org/licenses/LICENSE-2.0.html"/>
        <property name="scan" value="true"/>
    </bean>

    <bean id="apiListingResource" class="com.project1.utils.SwaggerApiListingResource"/>
    <bean id="swaggerSerializers" class="io.swagger.jaxrs.listing.SwaggerSerializers" scope="singleton"/>

</beans>

在我的Jetty服务器配置中,我定义了srvPropertyFile,该文件引用了与开发数据库具有数据库连接的environment.properties文件。

我应该如何管理我的持久性上下文,以便在读取订单时使用的实体管理器和在创建计划时使用的实体管理器连接到不同的数据库?

用例:我正在运行一个带有订单的spring应用程序。我可以为这些订单创建计划。我的开发环境/数据库没有任何订单,它们来自不同的应用程序(我们共享...

spring hibernate jpa persistence.xml
1个回答
0
投票

您必须分别配置两个持久性单元,数据源和事务管理器(一个用于读取,另一个用于写入)。

© www.soinside.com 2019 - 2024. All rights reserved.