web-dev-qa-db-fra.com

Aucun bean qualifiant de type [org.springframework.transaction.PlatformTransactionManager] n'est défini

J'utilise 2 sources de données Weblogic; Dans mes configurations XML, j'ai 2 unités de persistance, 2 entityManagerFactories et 2 transactionManagers. Les fichiers sont les suivants:

persistence.xml :

<persistence-unit name="modelPersistence" transaction-type="RESOURCE_LOCAL">
<provider>org.Eclipse.persistence.jpa.PersistenceProvider</provider> 
</persistence-unit>

<persistence-unit name="orderPersistence" transaction-type="RESOURCE_LOCAL">
<provider>org.Eclipse.persistence.jpa.PersistenceProvider</provider>
</persistence-unit>

config.xml :

<!-- transactionManagers -->
    <tx:annotation-driven transaction-manager="transactionManager" /> 
    <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
        <property name="entityManagerFactory" ref="entityManagerFactory" />
    </bean> 

    <bean id="transactionManagerLegacy" class="org.springframework.orm.jpa.JpaTransactionManager">
        <property name="entityManagerFactory" ref="entityManagerFactoryLegacy" />
    </bean>

<!-- entityManagerFactory --> 
    <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        <property name="dataSource" ref="dataSourceModel"/>
        <property name="persistenceXmlLocation" value="classpath*:META-INF/persistence.xml" />
        <property name="persistenceUnitName" value="modelPersistence" />
        <property name="jpaProperties">
            <props>
               <prop key="eclipselink.logging.level">FINE</prop>
            </props>
        </property>
        <property name="loadTimeWeaver">
            <bean class="org.springframework.instrument.classloading.InstrumentationLoadTimeWeaver"/>
        </property> 
        <property name="jpaVendorAdapter">
            <bean class="org.springframework.orm.jpa.vendor.EclipseLinkJpaVendorAdapter">
                <property name="databasePlatform" value="org.Eclipse.persistence.platform.database.OraclePlatform" />
                <property name="generateDdl" value="true"/>
                <property name="showSql" value="true"/>
            </bean>
        </property>
    </bean>     

<!-- entityManagerFactoryLegacy --> 
        <bean id="entityManagerFactoryLegacy" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        <property name="dataSource" ref="dataSourceOrder"/>
        <property name="persistenceXmlLocation" value="classpath*:META-INF/persistence.xml" />
        <property name="persistenceUnitName" value="orderPersistence" />
        <property name="jpaProperties">
            <props>
               <prop key="eclipselink.logging.level">FINE</prop>
            </props>
        </property>
        <property name="loadTimeWeaver">
            <bean class="org.springframework.instrument.classloading.InstrumentationLoadTimeWeaver"/>
        </property>
        <property name="jpaVendorAdapter">
            <bean class="org.springframework.orm.jpa.vendor.EclipseLinkJpaVendorAdapter">
                <property name="databasePlatform" value="org.Eclipse.persistence.platform.database.OraclePlatform" />
                <property name="generateDdl" value="true"/>
                <property name="showSql" value="true"/>
            </bean>
        </property>
    </bean>

<!-- data source 1-->
     <bean id="dataSourceModel" class="org.springframework.jndi.JndiObjectFactoryBean" depends-on="model-datasource-jndi">
        <property name="jndiName" ref="model-datasource-jndi" />
        <property name="resourceRef" value="true" /> 
    </bean> 

<!-- data source 2-->
     <bean id="dataSourceOrder" class="org.springframework.jndi.JndiObjectFactoryBean" depends-on="order-datasource-jndi">
        <property name="jndiName" ref="order-datasource-jndi" />
        <property name="resourceRef" value="true" /> 
    </bean> 

<!-- configure persistenceUnitManager -->
    <bean id="persistenceUnitManager" class="org.springframework.orm.jpa.persistenceunit.DefaultPersistenceUnitManager">
      <property name="persistenceXmlLocation" value="classpath*:META-INF/persistence.xml" />
      <property name="dataSources">
        <map>
            <entry key="jdbc/MODEL-DS" value-ref="dataSourceModel"/>
            <entry key="jdbc/ORDER-DS" value-ref="dataSourceOrder"/> 
        </map>
      </property>
    </bean> 
    <jpa:repositories base-package="org.codingpedia.demo.repositories.ModelRepository" entity-manager-factory-ref="entityManagerFactory" transaction-manager-ref="transactionManager"/>
    <jpa:repositories base-package="org.codingpedia.demo.repositories.OrderRepository" entity-manager-factory-ref="entityManagerFactoryLegacy" transaction-manager-ref="transactionManagerLegacy"/>

ModelRepository :

@Repository ("ModelRepo")
@PersistenceContext(unitName = "modelPersistence")
public interface ModelRepository extends JpaRepository<Model, String> {}

orderRepository :

@Repository ("OrderRepo")
@PersistenceContext(unitName = "orderPersistence")
public interface OrderRepository extends JpaRepository<Model, String> {}

Des suggestions sur la bonne annotation pour gérer plusieurs gestionnaires de transactions? Est-ce que je manque quelque chose d'autre? D'autres exemples de dépassement de pile utilisent des DAO que je n'ai pas trouvé utiles, car j'utilise des référentiels, des modèles et des fichiers Java de contrôleurs. Je n'utilise pas non plus l'hibernation, à laquelle de nombreux exemples font également référence. Votre perspicacité est appréciée.

Je reçois le message d'erreur suivant:

org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type [org.springframework.transaction.PlatformTransactionManager] is defined: expected single matching bean but found 2: transactionManagerLegacy,transactionManager
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.Java:365)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.Java:331)
    at org.springframework.transaction.interceptor.TransactionAspectSupport.determineTransactionManager(TransactionAspectSupport.Java:370)
    at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.Java:271)
    at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.Java:96)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.Java:179)
    at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.Java:136)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.Java:179)
    at org.springframework.data.jpa.repository.support.CrudMethodMetadataPostProcessor$CrudMethodMetadataPopulatingMethodIntercceptor.invoke(CrudMethodMetadataPostProcessor.Java:122)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.Java:179)
    at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.Java:92)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.Java:179)
    at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.Java:207)
    at com.Sun.proxy.$Proxy120.findAll(Unknown Source)
    at org.codingpedia.demo.controller.BaseController.index(BaseController.Java:45)
    at Sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at Sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.Java:62)
    at Sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.Java:43)
    at Java.lang.reflect.Method.invoke(Method.Java:497)
    at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.Java:221)
    at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.Java:137)
    at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.Java:110)
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.Java:777)
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.Java:706)
    at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.Java:85)
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.Java:943)
    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.Java:877)
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.Java:966)
    at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.Java:857)
    at javax.servlet.http.HttpServlet.service(HttpServlet.Java:731)
    at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.Java:842)
    at javax.servlet.http.HttpServlet.service(HttpServlet.Java:844)
    at weblogic.servlet.internal.StubSecurityHelper$ServletServiceAction.run(StubSecurityHelper.Java:280)
    at weblogic.servlet.internal.StubSecurityHelper$ServletServiceAction.run(StubSecurityHelper.Java:254)
    at weblogic.servlet.internal.StubSecurityHelper.invokeServlet(StubSecurityHelper.Java:136)
    at weblogic.servlet.internal.ServletStubImpl.execute(ServletStubImpl.Java:346)
    at weblogic.servlet.internal.ServletStubImpl.execute(ServletStubImpl.Java:243)
    at weblogic.servlet.internal.WebAppServletContext$ServletInvocationAction.wrapRun(WebAppServletContext.Java:3432)
    at weblogic.servlet.internal.WebAppServletContext$ServletInvocationAction.run(WebAppServletContext.Java:3402)
    at weblogic.security.acl.internal.AuthenticatedSubject.doAs(AuthenticatedSubject.Java:321)
    at weblogic.security.service.SecurityManager.runAs(SecurityManager.Java:120)
    at weblogic.servlet.provider.WlsSubjectHandle.run(WlsSubjectHandle.Java:57)
    at weblogic.servlet.internal.WebAppServletContext.doSecuredExecute(WebAppServletContext.Java:2285)
    at weblogic.servlet.internal.WebAppServletContext.securedExecute(WebAppServletContext.Java:2201)
    at weblogic.servlet.internal.WebAppServletContext.execute(WebAppServletContext.Java:2179)
    at weblogic.servlet.internal.ServletRequestImpl.run(ServletRequestImpl.Java:1572)
    at weblogic.servlet.provider.ContainerSupportProviderImpl$WlsRequestExecutor.run(ContainerSupportProviderImpl.Java:255)
    at weblogic.work.ExecuteThread.execute(ExecuteThread.Java:311)
    at weblogic.work.ExecuteThread.run(ExecuteThread.Java:263)
5
Jay113

Vous pouvez utiliser:

@Transactional("transactionManagerName")

Voir org.springframework.transaction.annotation.Transactional (@Transactional) annotation javadoc:

/**
 * A qualifier value for the specified transaction.
 * <p>May be used to determine the target transaction manager,
 * matching the qualifier value (or the bean name) of a specific
 * {@link org.springframework.transaction.PlatformTransactionManager}
 * bean definition.
 */
String value() default "";
6
rgrebski

Dans votre cas, cela devrait être:

<tx:annotation-driven transaction-manager="transactionManager" />
<tx:annotation-driven transaction-manager="transactionManagerLegacy" />

parce que c'est l'identification de vos haricots.

Si vous voulez plusieurs sources de données, je suggère de lire ceci: http://www.baeldung.com/spring-data-jpa-multiple-databases

0
Robert Niestroj

Actuellement, Dali ne supporte que une unité de persistance et un fichier XML de persistance par projet. D'autres configurations peuvent exister dans un projet JPA, mais le traitement de validation et des valeurs par défaut risque de ne pas être correct lorsque plusieurs unités de persistance sont utilisées. Découvrez plus d'informations ici .

0
Jay113