java - JPA Hibernate Spring MySql Tomcat - Connect to 2 databases -


i've been searching, reading, trying code 2 days , have not been successful.

i need able connect 2 different databases, not simultaneously, using technologies listed in header. i'm using tomcat7, not j2ee container.

below have application context. works fine 1 database. need configure two? how tell daos connection use? in advance.

<beans xmlns="http://www.springframework.org/schema/beans"    xmlns:context="http://www.springframework.org/schema/context"    xmlns:xsi="http://www.w3.org/2001/xmlschema-instance"    xmlns:tx="http://www.springframework.org/schema/tx"    xmlns:aop="http://www.springframework.org/schema/aop"    xsi:schemalocation="     http://www.springframework.org/schema/beans     http://www.springframework.org/schema/beans/spring-beans.xsd     http://www.springframework.org/schema/context     http://www.springframework.org/schema/context/spring-context.xsd     http://www.springframework.org/schema/aop     http://www.springframework.org/schema/aop/spring-aop-4.0.xsd     http://www.springframework.org/schema/tx     http://www.springframework.org/schema/tx/spring-tx.xsd">   <tx:annotation-driven/> <context:component-scan base-package="org.aaa.slds"/> <context:property-placeholder location="classpath:db.properties" />  <bean id="datasourcewccc" class="org.apache.commons.dbcp2.basicdatasource">     <property name="username" value="${dbuser_wccc}"/>     <property name="password" value="${dbpassword_wccc}"/>     <property name="url" value="${dburl_wccc}"/>     <property name="driverclassname" value="${dbdriver_wccc}"/> </bean>      <bean id="datasourcedws" class="org.apache.commons.dbcp2.basicdatasource">     <property name="username" value="${dbuser_dws}"/>     <property name="password" value="${dbpassword_dws}"/>     <property name="url" value="${dburl_dws}"/>     <property name="driverclassname" value="${dbdriver_dws}"/> </bean>  <bean id="entitymanagerfactorywccc" class="org.springframework.orm.jpa.localcontainerentitymanagerfactorybean">     <property name="datasource" ref="datasourcewccc"/>     <property name="jpavendoradapter">         <bean class="org.springframework.orm.jpa.vendor.hibernatejpavendoradapter"/>     </property>     <property name="jpaproperties">         <map>             <entry key="hibernate.hbm2ddl.auto" value="create-drop"/>             <entry key="hibernate.show_sql" value="false"/>         </map>     </property>     <property name="packagestoscan" value="org.aaa.slds.core.models.entities.wccc"/> </bean>  <bean id="transactionmanager" class="org.springframework.orm.jpa.jpatransactionmanager"/> 

as add 2nd entitymanager runtime errpr /// info: hhh000204: processing persistenceunitinfo [ name: punit2 ...] may 18, 2015 1:01:22 pm org.apache.catalina.core.standardcontext startinternal severe: error listenerstart

here complete appcontext file ...

<beans xmlns="http://www.springframework.org/schema/beans"    xmlns:context="http://www.springframework.org/schema/context"    xmlns:xsi="http://www.w3.org/2001/xmlschema-instance"    xmlns:tx="http://www.springframework.org/schema/tx"    xmlns:aop="http://www.springframework.org/schema/aop"    xsi:schemalocation="     http://www.springframework.org/schema/beans     http://www.springframework.org/schema/beans/spring-beans.xsd     http://www.springframework.org/schema/context     http://www.springframework.org/schema/context/spring-context.xsd     http://www.springframework.org/schema/aop     http://www.springframework.org/schema/aop/spring-aop-4.0.xsd     http://www.springframework.org/schema/tx     http://www.springframework.org/schema/tx/spring-tx.xsd">  <context:property-placeholder location="classpath:db.properties"/>  <!-- enable aspectj style of spring aop --> <aop:aspectj-autoproxy/>  <context:annotation-config/> <bean class="org.springframework.orm.jpa.support.persistenceannotationbeanpostprocessor"/> <context:component-scan base-package="gov.wyo.slds"/>  <!-- configure aspect beans, without aspects advices won't execute <bean name="loggingaspect" class="loggingaspect" />  -->  <bean id="datasourcewccc" class="org.springframework.jdbc.datasource.drivermanagerdatasource">     <property name="username" value="${wccc.dbuser}"/>     <property name="password" value="${wccc.dbpassword}"/>     <property name="url" value="${wccc.dburl}"/>     <property name="driverclassname" value="${wccc.dbdriver}"/> </bean>  <bean id="entitymanagerfactorywccc" class="org.springframework.orm.jpa.localcontainerentitymanagerfactorybean">     <property name="persistenceunitname" value="punit"/>      <property name="datasource" ref="datasourcewccc"/>     <property name="jpavendoradapter">         <bean class="org.springframework.orm.jpa.vendor.hibernatejpavendoradapter">             <property name="showsql" value="false"/>         </bean>     </property>     <property name="jpapropertymap">         <map>             <entry key="hibernate.dialect" value="org.hibernate.dialect.mysql5innodbdialect"/>             <entry key="hibernate.hbm2ddl.auto" value="update"/>             <entry key="hibernate.format_sql" value="true"/>         </map>     </property>     <property name="packagestoscan" value="gov.wyo.slds.core.models.entities.wccc"/> </bean> 
<bean id="datasourcedws" class="org.springframework.jdbc.datasource.drivermanagerdatasource">     <property name="username" value="${dws.dbuser}"/>     <property name="password" value="${dws.dbpassword}"/>     <property name="url" value="${dws.dburl}"/>     <property name="driverclassname" value="${dws.dbdriver}"/> </bean>  <bean id="entitymanagerfactorydws" class="org.springframework.orm.jpa.localcontainerentitymanagerfactorybean">     <property name="persistenceunitname" value="punit2"/>      <property name="datasource" ref="datasourcedws"/>     <property name="jpavendoradapter">         <bean class="org.springframework.orm.jpa.vendor.hibernatejpavendoradapter">             <property name="showsql" value="false"/>         </bean>     </property>     <property name="jpapropertymap">         <map>             <entry key="hibernate.dialect" value="org.hibernate.dialect.mysql5innodbdialect"/>             <entry key="hibernate.hbm2ddl.auto" value="create"/>             <entry key="hibernate.format_sql" value="true"/>         </map>     </property>     <property name="packagestoscan" value="gov.wyo.slds.core.models.entities.dws"/> </bean>  <bean id="transactionmanagerwccc" class="org.springframework.orm.jpa.jpatransactionmanager">     <property name="entitymanagerfactory" ref="entitymanagerfactorywccc" />     <qualifier value="wccc"/> </bean>  <bean id="transactionmanagerdws" class="org.springframework.orm.jpa.jpatransactionmanager">     <property name="entitymanagerfactory" ref="entitymanagerfactorydws" />     <qualifier value="dws"/> </bean>  <tx:annotation-driven/>  <bean id="persistenceexceptiontranslationpostprocessor"       class="org.springframework.dao.annotation.persistenceexceptiontranslationpostprocessor"/>  <!-- todo: re-eanble when have services --> <!-- context:component-scan base-package="com.slds.core.services.impl"/>  --> 

and here persistence.xml

<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.1">  <persistence-unit name="punit"> </persistence-unit>  <persistence-unit name="punit2"> </persistence-unit> 

a more complete error is

error creating bean name 'dwswagecontrollerimpl' defined in file [/users/sja/development/p20w-slds-poc/target/slds-1.0-snapshot/web-inf/classes/gov/wyo/slds/api/controllers/impl/dwswagecontrollerimpl.class]  

unsatisfied dependency expressed through constructor argument index 0 of type [gov.wyo.slds.core.services.dwswageservice]: : error creating bean name 'dwswageservice': injection of autowired dependencies failed; nested exception org.springframework.beans.factory.beancreationexception: not autowire field: private gov.wyo.slds.core.repositories.wccc.dwswagerepository gov.wyo.slds.core.services.impl.dwswageserviceimpl.dwswagerepository; nested exception org.springframework.beans.factory.beancreationexception: error creating bean name 'dwswagerepository': injection of persistence dependencies failed; nested exception org.springframework.beans.factory.nouniquebeandefinitionexception: no qualifying bean of type [javax.persistence.entitymanagerfactory] defined:

expected single matching bean found 2: entitymanagerfactorywccc,entitymanagerfactorydws;

any appreciated.

for need have:

  1. two entitymanagerfactory:

    <bean id="entitymanagerfactorywccc" class="org.springframework.orm.jpa.localcontainerentitymanagerfactorybean">     <property name="datasource" ref="datasourcewccc"/>     <property name="jpavendoradapter">         <bean class="org.springframework.orm.jpa.vendor.hibernatejpavendoradapter"/>     </property>     <property name="jpaproperties">         <map>             <entry key="hibernate.hbm2ddl.auto" value="create-drop"/>             <entry key="hibernate.show_sql" value="false"/>         </map>     </property>     <property name="packagestoscan" value="org.aaa.slds.core.models.entities.wccc"/> </bean>  <bean id="entitymanagerfactorydws" class="org.springframework.orm.jpa.localcontainerentitymanagerfactorybean">     <property name="datasource" ref="datasourcedws"/>     <property name="jpavendoradapter">         <bean class="org.springframework.orm.jpa.vendor.hibernatejpavendoradapter"/>     </property>     <property name="jpaproperties">         <map>             <entry key="hibernate.hbm2ddl.auto" value="create-drop"/>             <entry key="hibernate.show_sql" value="false"/>         </map>     </property>     <property name="packagestoscan" value="org.aaa.slds.core.models.entities.dws"/> </bean> 
  2. two jpa transaction managers:

    <bean id="transactionmanagerwccc" class="org.springframework.orm.jpa.jpatransactionmanager">     <property name="entitymanagerfactory" ref="entitymanagerfactorywccc" />     <qualifier value="wccc"/> </bean>  <bean id="transactionmanagerdws" class="org.springframework.orm.jpa.jpatransactionmanager">     <property name="entitymanagerfactory" ref="entitymanagerfactorydws" />     <qualifier value="dws"/> </bean> 
  3. and service methods annotated specific transaction manager:

    @transactional("wccc") 

    or

    @transactional("dws") 

if want enlist both data sources in global transaction, need use jta transactions , jta transaction manager.


Comments

Popular posts from this blog

apache - PHP Soap issue while content length is larger -

asynchronous - Python asyncio task got bad yield -

javascript - Complete OpenIDConnect auth when requesting via Ajax -