title: Using Multiple Entity managers in DAO
body: <p>I'm trying to configure Spring+Hibernate+JPA for work with two databases ( one for write only i.e insertion &amp; updation &amp; other is only for retrieval.</p>
  
  <p>I did some research &amp; found these possible solutions:</p>
  
  <ul>
  <li><a href="http://www.studytrails.com/frameworks/spring/spring-hibernate-jpa.jsp" rel="nofollow noreferrer">http://www.studytrails.com/frameworks/spring/spring-hibernate-jpa.jsp</a></li>
  <li><a href="https://stackoverflow.com/questions/1902997/multiple-database-with-springhibernatejpa">Multiple database with Spring+Hibernate+JPA</a></li>
  <li><a href="https://stackoverflow.com/questions/2311614/how-do-i-connect-to-multiple-databases-using-jpa">How do I connect to multiple databases using JPA?</a></li>
  </ul>
  
  <p>But I stuck at one place &amp; getting this error </p>
  
  <pre><code>No qualifying bean of type [javax.persistence.EntityManagerFactory] is defined: expected single matching bean but found 2: entityManagerFactoryReadOnly,entityManagerFactoryWriteOnly
  </code></pre>
  
  <p>What am I doing wrong ?</p>
  
  <h2>persistent.read.only.xml:</h2>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  
  &lt;persistence version="2.0"
      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"&gt;
  
      &lt;persistence-unit name="readOnly" transaction-type="RESOURCE_LOCAL"&gt;        
          &lt;provider&gt;org.hibernate.ejb.HibernatePersistence&lt;/provider&gt;     
          &lt;class&gt;com.demo.domain.Contact&lt;/class&gt;  
      &lt;/persistence-unit&gt;
  &lt;/persistence&gt;
  </code></pre>
  
  <h2>persistent.write.only.xml:</h2>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  
  &lt;persistence version="2.0"
      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"&gt;
  
      &lt;persistence-unit name="writeOnly" transaction-type="RESOURCE_LOCAL"&gt;       
          &lt;provider&gt;org.hibernate.ejb.HibernatePersistence&lt;/provider&gt;     
          &lt;class&gt;com.demo.domain.Contact&lt;/class&gt;  
      &lt;/persistence-unit&gt;
  &lt;/persistence&gt;
  </code></pre>
  
  <h2>mcv-dispatcher-servlet.xml:</h2>
  
  <pre><code>&lt;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:context="http://www.springframework.org/schema/context"
      xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:jee="http://www.springframework.org/schema/jee"
      xmlns:lang="http://www.springframework.org/schema/lang" xmlns:p="http://www.springframework.org/schema/p"
      xmlns:tx="http://www.springframework.org/schema/tx" xmlns:util="http://www.springframework.org/schema/util"
      xmlns:oxm="http://www.springframework.org/schema/oxm"
      xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
          http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
          http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
          http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd
          http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang.xsd
          http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
          http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd
          http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
          http://www.springframework.org/schema/oxm http://www.springframework.org/schema/oxm/spring-oxm-3.2.xsd"&gt;
  
      &lt;!-- Activates various annotations to be detected in bean classes --&gt;
      &lt;context:annotation-config /&gt;
  
      &lt;!-- Scans the classpath for annotated components that will be auto-registered 
          as Spring beans. For example @Controller and @Service. Make sure to set the 
          correct base-package --&gt;
      &lt;context:component-scan base-package="com.demo" /&gt;
  
      &lt;!-- Setup a simple strategy: 1. Take all the defaults. 2. Return XML by 
          default when not sure. --&gt;
      &lt;!-- Total customization - see below for explanation. --&gt;
      &lt;bean id="cnManager"
          class="org.springframework.web.accept.ContentNegotiationManagerFactoryBean"&gt;
          &lt;property name="favorPathExtension" value="true" /&gt;
          &lt;property name="ignoreAcceptHeader" value="true" /&gt;
          &lt;property name="defaultContentType" value="application/json" /&gt;
          &lt;property name="useJaf" value="false" /&gt;
  
          &lt;property name="mediaTypes"&gt;
              &lt;map&gt;
                  &lt;entry key="json" value="application/json" /&gt;
                  &lt;entry key="xml" value="application/xml" /&gt;
              &lt;/map&gt;
          &lt;/property&gt;
      &lt;/bean&gt;
  
      &lt;!-- Make this available across all of Spring MVC --&gt;
      &lt;mvc:annotation-driven
          content-negotiation-manager="cnManager" /&gt;
  
      &lt;bean class="com.demo.view.MvcConfiguringPostProcessor" /&gt;
  
      &lt;bean id="propertyConfigurer"
          class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"
          p:location="/WEB-INF/jdbc.properties" /&gt;
  
  
      &lt;!-- ******************************************************************** --&gt;
      &lt;!--       START: Multiple C3P0 data-sources for DB instance                --&gt;
      &lt;!-- ******************************************************************** --&gt;
  
      &lt;!-- https://stackoverflow.com/questions/12922351/can-i-use-multiple-c3p0-datasources-for-db-instance --&gt;
      &lt;!-- Using Apache DBCP Data Sources --&gt;
      &lt;bean id="dataSource" 
          abstract="true" &gt;
          &lt;property name="driverClass" value="${db.driverClassName}" /&gt;
          &lt;property name="user" value="${db.username}" /&gt;
          &lt;property name="password" value="${db.password}" /&gt;
          &lt;property name="idleConnectionTestPeriod" value="${db.idleConnectionTestPeriod}" /&gt;
          &lt;property name="preferredTestQuery" value="select 1" /&gt;
          &lt;property name="testConnectionOnCheckin" value="true" /&gt;
      &lt;/bean&gt;
  
      &lt;bean id="dataSourceReadOnly" 
          parent="dataSource"
          class="com.mchange.v2.c3p0.ComboPooledDataSource"
          destroy-method="close"&gt;
          &lt;property name="jdbcUrl" value="${db.readOnlyDataBaseUrl}" /&gt;
      &lt;/bean&gt;
      &lt;bean id="dataSourceWriteOnly" 
          parent="dataSource"
          class="com.mchange.v2.c3p0.ComboPooledDataSource"
          destroy-method="close"&gt;
          &lt;property name="jdbcUrl" value="${db.writeOnlyDataBaseUrl}" /&gt;
      &lt;/bean&gt;
  
      &lt;!-- ******************************************************************** --&gt;
      &lt;!--       END: Multiple C3P0 data-sources for DB instance              --&gt;
      &lt;!-- ******************************************************************** --&gt;
  
  
      &lt;bean id="jpaVendorProvider"
          class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"&gt;
          &lt;property name="database" value="MYSQL" /&gt;
          &lt;property name="databasePlatform" value="${db.dialect}" /&gt;
          &lt;property name="showSql" value="true" /&gt;
          &lt;property name="generateDdl" value="true" /&gt;
      &lt;/bean&gt;
  
  &lt;!--    &lt;bean --&gt;
  &lt;!--        class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor"&gt; --&gt;
  &lt;!--        &lt;property name="defaultPersistenceUnitName" value="readOnly" /&gt; --&gt;
  &lt;!--     &lt;/bean&gt; --&gt;
  
      &lt;bean id="persistenceUnitManager"
          class="org.springframework.orm.jpa.persistenceunit.DefaultPersistenceUnitManager"&gt;
          &lt;!-- defining multiple persistence unit --&gt;
          &lt;property name="persistenceXmlLocations"&gt;
              &lt;list&gt;
                  &lt;value&gt;/META-INF/persistence.read.only.xml&lt;/value&gt;
                  &lt;value&gt;/META-INF/persistence.write.only.xml&lt;/value&gt;
              &lt;/list&gt;
          &lt;/property&gt;
          &lt;property name="defaultDataSource" ref="dataSourceReadOnly" /&gt; 
          &lt;property name="dataSources"&gt;
              &lt;map&gt;
                  &lt;entry key="readOnlyDsKey" value-ref="dataSourceReadOnly" /&gt;
                  &lt;entry key="writeOnlyDsKey" value-ref="dataSourceWriteOnly" /&gt;
              &lt;/map&gt;
          &lt;/property&gt;
      &lt;/bean&gt;
  
      &lt;bean id="entityManagerFactoryReadOnly"
          class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"&gt;
  &lt;!--        &lt;property name="dataSource" ref="dataSourceReadOnly" /&gt;      --&gt;
          &lt;property name="persistenceUnitManager" ref="persistenceUnitManager" /&gt;
          &lt;property name="jpaVendorAdapter" ref="jpaVendorProvider" /&gt;
          &lt;property name="persistenceUnitName" value="readOnly" /&gt;
          &lt;!-- entityManagerFactory does not specify persistenceUnitName property 
              because we're defining more than one persistence unit --&gt;
          &lt;!-- &lt;property name="persistenceUnitName" value="hello_mysql" /&gt; --&gt;
          &lt;!-- &lt;property name="persistenceXmlLocation" value="/META-INF/persistence.xml" /&gt; --&gt;
      &lt;/bean&gt;
  
      &lt;bean id="entityManagerFactoryWriteOnly"
          class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"&gt;
  &lt;!--        &lt;property name="dataSource" ref="dataSourceWriteOnly" /&gt;         --&gt;
          &lt;property name="persistenceUnitManager" ref="persistenceUnitManager" /&gt;
          &lt;property name="jpaVendorAdapter" ref="jpaVendorProvider" /&gt;
          &lt;property name="persistenceUnitName" value="writeOnly" /&gt;
      &lt;/bean&gt;
  
      &lt;!-- ******************************************************************** --&gt;
      &lt;!-- Mark bean transactions as annotation driven --&gt;
      &lt;!-- ******************************************************************** --&gt;
      &lt;tx:annotation-driven transaction-manager="transactionManagerReadOnly" /&gt;
      &lt;tx:annotation-driven transaction-manager="transactionManagerWriteOnly" /&gt;
  
      &lt;!-- ******************************************************************** --&gt;
      &lt;!-- Setup the transaction manager --&gt;
      &lt;!-- ******************************************************************** --&gt;
  
      &lt;bean id="transactionManagerReadOnly" class="org.springframework.orm.jpa.JpaTransactionManager"&gt;
          &lt;property name="entityManagerFactory" ref="entityManagerFactoryReadOnly" /&gt;
      &lt;/bean&gt;
  
      &lt;bean id="transactionManagerWriteOnly" class="org.springframework.orm.jpa.JpaTransactionManager"&gt;
          &lt;property name="entityManagerFactory" ref="entityManagerFactoryWriteOnly" /&gt;
      &lt;/bean&gt;
  
  
  &lt;/beans&gt;
  </code></pre>
  
  <p>My DAO:</p>
  
  <pre><code>package com.demo.dao;
  
  import java.util.Collections;
  import java.util.List;
  
  import javax.persistence.EntityManager;
  import javax.persistence.PersistenceContext;
  
  import org.springframework.beans.factory.annotation.Qualifier;
  import org.springframework.stereotype.Repository;
  import org.springframework.transaction.annotation.Transactional;
  
  import com.demo.domain.Contact;
  
  //import java.util.Collections;
  
  @Repository("ContactDAO")
  @Transactional
  public class ContactDAOImpl extends AppDAOimpl&lt;Contact&gt; implements ContactDAO {
  
      /**
       * 
       */
      private static final long serialVersionUID = 3986253823316728444L;
  
      /**
       * EntityManager injected by Spring for persistence unit MYSQL
       * 
       */
      @PersistenceContext(unitName = "readOnly")
      @Qualifier("entityManagerFactoryReadOnly")
      private EntityManager entityManager;
  
      /**
       * Get the entity manager that manages persistence unit MYSQL
       * 
       */
      public EntityManager getEntityManager() {
          return entityManager;
      }
  
      /**
       * EntityManager injected by Spring for persistence unit MYSQL
       * 
       */
      @PersistenceContext(unitName = "writeOnly")
      @Qualifier("entityManagerFactoryWriteOnly")
      private EntityManager woEntityManager;
  
      /**
       * Get the entity manager that manages persistence unit MYSQL
       * 
       */
      public EntityManager getWoEntityManager() {
          return woEntityManager;
      }
  
  
      // other functions goes here 
  }
  </code></pre>
  
  <p>Both the databases have the same schema ( read &amp; write ).</p>