Community   SpringSource   Projects    Downloads    Documentation    Forums    Training   Exchange   Blogs

Go Back   Spring Community Forums > Core Spring Projects > Data Access

Reply
 
Thread Tools Display Modes
  #1  
Old Sep 15th, 2008, 02:12 AM
JuneGal JuneGal is offline
Junior Member
 
Join Date: Sep 2008
Posts: 11
Default @PersistenceContext and multiple datasource

Can anyone tell me whether is it possible to use @PersistenceContext injection with multiple datasource? Spring has been giving me these errors:

Code:
Context initialization failed
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'adminService': Injection of persistence methods 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.orm.jpa.support.PersistenceAnnotationBeanPostProcessor.postProcessPropertyValues(PersistenceAnnotationBeanPostProcessor.java:324)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:996)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:470)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory$1.run(AbstractAutowireCapableBeanFactory.java:409)
	at java.security.AccessController.doPrivileged(Native Method)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:380)
	at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:264)
	at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:217)
	at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:261)
	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:185)
	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:164)
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:429)
	at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:729)
	at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:381)
	at org.springframework.web.context.ContextLoader.createWebApplicationContext(ContextLoader.java:255)
	at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:199)
	at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:45)
	at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:3764)
	at org.apache.catalina.core.StandardContext.start(StandardContext.java:4216)
	at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1014)
	at org.apache.catalina.core.StandardHost.start(StandardHost.java:736)
	at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1014)
	at org.apache.catalina.core.StandardEngine.start(StandardEngine.java:443)
	at org.apache.catalina.core.StandardService.start(StandardService.java:448)
	at org.apache.catalina.core.StandardServer.start(StandardServer.java:700)
	at org.apache.catalina.startup.Catalina.start(Catalina.java:552)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
	at java.lang.reflect.Method.invoke(Unknown Source)
	at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:295)
	at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:433)
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No unique bean of type [javax.persistence.EntityManagerFactory] is defined: expected single bean but found 2
	at org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor.findDefaultEntityManagerFactory(PersistenceAnnotationBeanPostProcessor.java:513)
	at org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor.findEntityManagerFactory(PersistenceAnnotationBeanPostProcessor.java:473)
	at org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor$PersistenceElement.resolveEntityManager(PersistenceAnnotationBeanPostProcessor.java:598)
	at org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor$PersistenceElement.getResourceToInject(PersistenceAnnotationBeanPostProcessor.java:569)
	at org.springframework.beans.factory.annotation.InjectionMetadata$InjectedElement.inject(InjectionMetadata.java:192)
	at org.springframework.beans.factory.annotation.InjectionMetadata.injectMethods(InjectionMetadata.java:117)
	at org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor.postProcessPropertyValues(PersistenceAnnotationBeanPostProcessor.java:321)
	... 31 more
Reply With Quote
  #2  
Old Sep 15th, 2008, 02:23 AM
Marten Deinum Marten Deinum is offline
Senior Member
 
Join Date: Jun 2006
Location: The Netherlands
Posts: 9,521
Default

I suggest the javadocs... Specify the name of the context you want to inject.
__________________
Marten Deinum
  • Senior Java Consultant
  • SpringSource Certified Trainer
Conspect ICT diensten
Blog
LinkedIn
Use the [ code ] tags, young padawan
Reply With Quote
  #3  
Old Sep 15th, 2008, 04:41 AM
JuneGal JuneGal is offline
Junior Member
 
Join Date: Sep 2008
Posts: 11
Default

After checking the docs, I have changed the @PersistenceContext to @PersistenceUnit(name="emf") pointing to the specific EntityManagerFactory in applicationContext.xml but I'm still getting the same error as the first posting. Would appreciate anyone's help....
Reply With Quote
  #4  
Old Sep 15th, 2008, 05:41 AM
mgervais@agaetis.fr mgervais@agaetis.fr is offline
Member
 
Join Date: Sep 2008
Location: Clermont-Ferrand
Posts: 32
Default

You can specify your default persistence unit name in config file:
Code:
<!-- Read META-INF/persistence.xml in classpath -->
	<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalEntityManagerFactoryBean">
		<property name="persistenceUnitName" value="my-unit-name" />
	</bean>
__________________
:::::::::::::::::::::::::::::::::::::::::::::::
Mickael Gervais
Agaetis

Site : www.agaetis.fr
:::::::::::::::::::::::::::::::::::::::::::::::
Reply With Quote
  #5  
Old Sep 16th, 2008, 05:38 AM
JuneGal JuneGal is offline
Junior Member
 
Join Date: Sep 2008
Posts: 11
Default

Thanks Mickael for your suggestion, however....I ran into another error. I manage to usage Mickael's suggestion and configure the entityManagerFactory to point to a persistenceUnitName (I have 2 entityManagerFactory and 2 persistence-unit). Tomcat 5.5 build without error but when I tried to test run, it output the following error
Code:
SEVERE: Servlet.service() for servlet jsp threw exception
org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'entityManagerFactory' is defined
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanDefinition(DefaultListableBeanFactory.java:387)
	at org.springframework.beans.factory.support.AbstractBeanFactory.getMergedLocalBeanDefinition(AbstractBeanFactory.java:968)
	at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:246)
	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:185)
	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:168)
	at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:885)
	at org.springframework.orm.jpa.support.OpenEntityManagerInViewFilter.lookupEntityManagerFactory(OpenEntityManagerInViewFilter.java:151)
	at org.springframework.orm.jpa.support.OpenEntityManagerInViewFilter.lookupEntityManagerFactory(OpenEntityManagerInViewFilter.java:134)
	at org.springframework.orm.jpa.support.OpenEntityManagerInViewFilter.doFilterInternal(OpenEntityManagerInViewFilter.java:93)
	at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:215)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:188)
	at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:213)
	at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:174)
	at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
	at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:117)
	at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:108)
	at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:174)
	at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:874)
	at org.apache.coyote.http11.Http11BaseProtocol$Http11ConnectionHandler.processConnection(Http11BaseProtocol.java:665)
	at org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:528)
	at org.apache.tomcat.util.net.LeaderFollowerWorkerThread.runIt(LeaderFollowerWorkerThread.java:81)
	at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:689)
	at java.lang.Thread.run(Unknown Source)

the applicationContext looks like this:
Code:
<bean
		class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" />

	<bean id="emfOmstrade"
		class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
		<property name="dataSource" ref="dsOmstrade" />
		<property name="jpaVendorAdapter">
			<bean
				class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
				<property name="database" value="SQL_SERVER" />
				<property name="showSql" value="true" />
			</bean>
		</property>	
		<property name="persistenceUnitName" value="omstradeManager" />
	</bean>

	<bean id="emfOmsweb"
		class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
		<property name="dataSource" ref="dsOmsweb" />
		<property name="jpaVendorAdapter">
			<bean
				class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
				<property name="database" value="SQL_SERVER" />
				<property name="showSql" value="true" />				
			</bean>
		</property>
		<property name="persistenceUnitName" value="omswebManager" />	
	</bean>

	<bean id="dsOmsweb"
		class="org.enhydra.jdbc.standard.StandardXADataSource">
		<property name="transactionManager" ref="jotm" />
		<property name="driverName"
			value="net.sourceforge.jtds.jdbc.Driver" />
		<property name="url"
			value="jdbc:jtds:sqlserver://k" />
		<property name="user" value="x" />
		<property name="password" value="y" />
	</bean>

	<bean id="dsOmstrade"
		class="org.enhydra.jdbc.standard.StandardXADataSource">
		<property name="transactionManager" ref="jotm" />		
		<property name="driverName"
			value="net.sourceforge.jtds.jdbc.Driver" />
		<property name="url"
			value="jdbc:jtds:sqlserver://o" />
		<property name="user" value="x" />
		<property name="password" value="y" />
	</bean>
	
	<bean id="jotm" class="org.springframework.transaction.jta.JotmFactoryBean"/>
  
	<bean id="txManager"
		class="org.springframework.transaction.jta.JtaTransactionManager">
		<property name="userTransaction" ref="jotm" />
	</bean>

	<context:annotation-config />
	<tx:annotation-driven transaction-manager="txManager" />
persistence.xml looks like this:
Code:
<persistence-unit name="omswebManager">
		<provider>org.hibernate.ejb.HibernatePersistence</provider>
        <properties>
            <property name="hibernate.show_sql" value="true" />
            <property name="hibernate.format_sql" value="true" />
 
            <property name="hibernate.connection.driver_class" 
                      value="net.sourceforge.jtds.jdbc.Driver" />
            <property name="hibernate.connection.url" 
                      value="jdbc:jtds:sqlserver://k" />
        
            <property name="hibernate.connection.username" value="x" />
            <property name="hibernate.connection.password" value="y" />
 
            <property name="hibernate.dialect" 
                      value="org.hibernate.dialect.SQLServerDialect" />
        </properties>
    </persistence-unit>
  
    <persistence-unit name="omstradeManager">
    	<provider>org.hibernate.ejb.HibernatePersistence</provider>
        <properties>
            <property name="hibernate.show_sql" value="true" />
            <property name="hibernate.format_sql" value="true" />
 
            <property name="hibernate.connection.driver_class" 
                      value="net.sourceforge.jtds.jdbc.Driver" />
            <property name="hibernate.connection.url" 
                      value="jdbc:jtds:sqlserver://o" />
            <property name="hibernate.connection.username" value="x" />
            <property name="hibernate.connection.password" value="y" />
 
            <property name="hibernate.dialect" 
                      value="org.hibernate.dialect.SQLServerDialect" />
        </properties>
    </persistence-unit>
below is a portion of my service class:
Code:
import org.springframework.transaction.annotation.Transactional;

@Transactional
public class AdminService {

	private EntityManager em;
	
	@PersistenceContext(unitName="omswebManager")
	public void setEm(EntityManager em) {
		this.em = em;
	}
As you can see, I have not use a 'entityManagerFactory' bean but did point to a unitname at @PersistenceContext. My question is, is 'entityManagerFactory' bean a default bean for spring injection and I can't manually specify my own bean for EntityManagerFactory? I believe this shld not be the case but the error tells me otherwise. Desperately need some answers and advice.
Reply With Quote
  #6  
Old Sep 16th, 2008, 06:49 AM
mgervais@agaetis.fr mgervais@agaetis.fr is offline
Member
 
Join Date: Sep 2008
Location: Clermont-Ferrand
Posts: 32
Default

try to rename emfOmstrade to entityManagerFactory
Code:
<bean id="entityManagerFactory"
		class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
		<property name="dataSource" ref="dsOmstrade" />
		<property name="jpaVendorAdapter">
			<bean
				class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
				<property name="database" value="SQL_SERVER" />
				<property name="showSql" value="true" />
			</bean>
		</property>	
		<property name="persistenceUnitName" value="omstradeManager" />
	</bean>
__________________
:::::::::::::::::::::::::::::::::::::::::::::::
Mickael Gervais
Agaetis

Site : www.agaetis.fr
:::::::::::::::::::::::::::::::::::::::::::::::
Reply With Quote
  #7  
Old Sep 24th, 2008, 10:40 AM
rdelaplante rdelaplante is offline
Member
 
Join Date: Aug 2008
Posts: 54
Default

I recently battled Spring with this problem and found a way to make it work:

http://forum.springframework.org/showthread.php?t=60729

Note that I don't mention a datasource in applicationContext.xml because the JTA jndi datasource is defined in persistence.xml
Reply With Quote
  #8  
Old Sep 25th, 2008, 12:33 AM
JuneGal JuneGal is offline
Junior Member
 
Join Date: Sep 2008
Posts: 11
Default

Hi Ryan,

Thanks for the insights. Can you post your persistence.xml as well?
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -5. The time now is 01:58 AM.


Contegix provides first-class managed hosting and partial sponsorship of these forums.

Powered by vBulletin® Version 3.8.4
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.