Announcement Announcement Module
Collapse
No announcement yet.
EntityManagerFactory injection problem for Multiple PersistentUnits Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • EntityManagerFactory injection problem for Multiple PersistentUnits

    Hi,

    I have two DBs(both Postgres) and have defined two persistence units(one for each DB) and in turn defined two EntitManagerFactories one for each DB/PersistenceUnit. I have read numerous threads on this topic in the forum and made many modifications to my configuration but can't get it to work;

    Here is the exception thrown on startup(yea I'm running Eclipse Virgo, with SpringFramework 3.0);

    Code:
    Start failed for plan 'astral.plan' version '1.0.0'. org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.osgi.service.exporter.support.OsgiServiceFactoryBean#0': Invocation of init method failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'componentDao': Injection of persistence dependencies failed; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No unique bean of type [javax.persistence.EntityManagerFactory] is defined: expected single bean but found 2
            at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1412)
            at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:519
    My bundle-context.xml file is as follows:
    Code:
    	
            <!-- post-processors for all standard config annotations -->
    	<context:annotation-config/>
    	
    	<context:component-scan base-package="org.company.project.dataaccess.domain.dao.hibernatejpa"/>
    
    	
    	<bean id="componentDao" class="org.company.project.dataaccess.domain.dao.hibernatejpa.ComponentDaoImpl"/>
    	
    
    	<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    		<property name="persistenceUnitName" value="AstralPU" />
    		<property name="dataSource" ref="dataSource"/>
    		<property name="persistenceXmlLocation" value="classpath:META-INF/persistence.xml"/>
    		
    		<property name="jpaVendorAdapter">
    			<bean class="org.springframework.orm.jpa.vendor.EclipseLinkJpaVendorAdapter">
    				 <property name="databasePlatform" value="org.eclipse.persistence.platform.database.PostgreSQLPlatform"/> 
    				 <property name="generateDdl" value="false"/>
    				 <property name="showSql" value="true"/>
    			</bean>
    		</property>
    		<property name="loadTimeWeaver">
    			<bean class="org.eclipse.equinox.weaving.springweaver.EquinoxAspectsLoadTimeWeaver">
    				<property name="weaverScope" value="APPLICATION"/>
    			</bean>
     		</property>
    	</bean>
    	
    	<bean id="entityManagerFactoryAnalytics" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    		<property name="persistenceUnitName" value="AstralAnalyticsPU" />
    		<property name="dataSource" ref="dataSourceAnalytics"/>
    		<property name="persistenceXmlLocation" value="classpath:META-INF/persistence-analytics.xml"/>
    		
    		<property name="jpaVendorAdapter">
    			<bean class="org.springframework.orm.jpa.vendor.EclipseLinkJpaVendorAdapter">
    				 <property name="databasePlatform" value="org.eclipse.persistence.platform.database.PostgreSQLPlatform"/> 
    				 <property name="generateDdl" value="false"/>
    				 <property name="showSql" value="true"/>
    			</bean>
    		</property>
    		<property name="loadTimeWeaver">
    			<bean class="org.eclipse.equinox.weaving.springweaver.EquinoxAspectsLoadTimeWeaver">
    				<property name="weaverScope" value="APPLICATION"/>
    			</bean>
     		</property>
    	</bean>
    	
    	<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
    		<property name="entityManagerFactory" ref="entityManagerFactory"/>
    	</bean>
    My DAO is as follows;
    Code:
    public class ComponentDaoImpl implements ComponentDao {
    
    	@PersistenceContext(unitName = "AstralPU")
    	private EntityManager _em ;
    	......
    }
    As you can see from above exception its stating that 'No unique bean found' so its fails to inject.

    Any help here is much appreciated.

    Chris.

  • #2
    It now seems to work for some reason . I changed the PersistenceContext annotation from the field to a setter in the DAO(at least thats all I think I changed - I have been trying to resolve this for some time now!) and it works, everything starts up correctly.

    I have gone back and removed the setter and moved the PersistenceContext back onto the field for the EntityManager and this works also!!!!! Confusing to say the least.

    Apologies, but the above configuration is now working

    Comment


    • #3
      Ok, When I said that it was working, it was that it all started up ok!!! But when I actually test it, i.e. attempt to read something from the other database(astral_analytics) it fails stating that the table in question doesn't exist. When I look into the trace some more it shows that it is attempting to connect to the original DB(astral_test). So even though the dependency injection of the EntityManager succeed it looks as if it is using the EM for the original DB.

      Here is an extract from the log;
      Code:
      [2010-11-26 15:00:39.921] AGGREGATION_THREAD           System.out                                                        [EL Config]: 2010-11-26 15:00:39.921--ServerSession(16520985)--Connection(7698449)--Thread(Thread[AGGREGATION_THREAD,5,main])--Connected: jdbc:postgresql://localhost/astral_test 
      [2010-11-26 15:00:39.921] AGGREGATION_THREAD           System.out                                                               User: test 
      [2010-11-26 15:00:39.921] AGGREGATION_THREAD           System.out                                                               Database: PostgreSQL  Version: 8.4.4 
      [2010-11-26 15:00:39.921] AGGREGATION_THREAD           System.out                                                               Driver: PostgreSQL Native Driver  Version: PostgreSQL 8.3 JDBC4 with SSL (build 604) 
      [2010-11-26 15:00:39.937] AGGREGATION_THREAD           System.out                                                        [EL Info]: 2010-11-26 15:00:39.937--ServerSession(16520985)--Thread(Thread[AGGREGATION_THREAD,5,main])--file:/C:/virgo-web-server-2.1.0.M06-incubation/work/osgi/configuration/org.eclipse.osgi/bundles/36/data/store/org.eclipse.osgi/bundles/88/1/bundlefile/_AstralAnalyticsPU login successful
      So it says that it is connected to jdbc: postgresql://localhost/astral_test which is incorrect it should be jdbc: postgresql://localhost/astral_analytics but it goes on to say, also visible in the log above that '....._AstralAnalyticsPU login successful' so it has used the correct Persistent Unit in some form!

      I have my two data sources defined correctly
      Code:
      	<!-- Data Sources -->
      	<bean id="dataSource"
      				class="org.apache.commons.dbcp.BasicDataSource"
      				destroy-method="close"
      				scope="singleton">
      		<property name="driverClassName" value="${jdbc.driverClassName}" />
      		<property name="url" value="${jdbc.url}" />
      		<property name="username" value="${jdbc.username}" />
      		<property name="password" value="${jdbc.password}" />
      	</bean>
      	
      	<bean id="dataSourceAnalytics"
      				class="org.apache.commons.dbcp.BasicDataSource"
      				destroy-method="close"
      				scope="singleton">
      		<property name="driverClassName" value="${jdbc.driverClassName}" />
      		<property name="url" value="${jdbc.url.analytics}" />
      		<property name="username" value="${jdbc.username}" />
      		<property name="password" value="${jdbc.password}" />
      	</bean>
      I also have my DAO annotated correctly;
      Code:
      	@PersistenceContext(unitName="AstralAnalyticsPU")
      	public void setEntityManager(EntityManager entityManager) {
      		this._em = entityManager;
      	}
      I'm not sure what I'm missing to make this work, has this something to do with the Transaction Manager, any guidance appreciated!

      Chris.

      Comment


      • #4
        Did anyone have solve this issue.

        I have exact the same problem. I need access to two databases (datasources). All components have unique bean identifiers. But my DAOs will goes all times against the first loaded datasource (running in Eclipse Virgo Server).

        thanks
        Stephan

        Comment


        • #5
          Hi TerryTornado,

          Did you solve this problem.

          I have exact same problem. I need to connect Two DataBases, both are Mysql. please guide me, if you have find a solution.

          Comment

          Working...
          X