0

Use Case: I am running a spring application which has orders. I can create plans for these orders. My dev environment/db does not have any orders and they come from a different application (we share the same db). So, Is there a way I can read orders from production database but when I create a plan, it gets saved to dev database?

My persistence.xml is as below


    <?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>

My spring-config.xml is as below

<?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>

In my Jetty server config, I am defining the srvPropertyFile which references to the environment.properties file which has the database connection to dev db.

How should I manage my persistence context so that the entity manager used while reading orders and the entity manager used while creating plans connects to different databases?

Panic
  • 91
  • 11

1 Answers1

0

you have to configure two persistence unit, data source and transaction manager(one for read and other for write) each.

While using entity manager specify the persistence unit like below.

PersistenceUnit(unitName = "x") EntityManagerFactory entityManagerFactory;

Niraj Jha
  • 455
  • 3
  • 10
  • I added both persistence units to persistence.xml. Created two data sources and transaction managers. Still doesn't work. – Panic Mar 04 '20 at 17:23