2つのWeblogicデータソースを使用しています。私のXML構成には、2つの永続性ユニット、2つのentityManagerFactories、および2つのtransactionManagerがあります。ファイルは次のとおりです。
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> {}
複数のトランザクションマネージャを処理するための正しいアノテーションに関する提案はありますか?他に何か不足していますか?他のスタックオーバーフローの例では、リポジトリ、モデル、およびコントローラーを使用しているため、役に立たなかったDAOを使用していますJavaファイル。また、多くの例で参照されている休止状態も使用していません。 。あなたの洞察に感謝します。
次のエラーが表示されます:
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)
以下を使用できます。
@Transactional("transactionManagerName")
Org.springframework.transaction.annotation.Transactional(@Transactional)アノテーション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 "";
あなたの場合、それは:
<tx:annotation-driven transaction-manager="transactionManager" />
<tx:annotation-driven transaction-manager="transactionManagerLegacy" />
それはあなたの豆のIDです。
複数のデータソースが必要な場合は、これを読むことをお勧めします: http://www.baeldung.com/spring-data-jpa-multiple-databases
現在Daliは、プロジェクトごとに1つの永続性ユニットと1つの永続性XMLファイルのみをサポートしています。 JPAプロジェクトには他の構成も存在できますが、複数の永続性ユニットが使用されている場合、検証とデフォルトの処理が正しく行われない場合があります。詳細はこちら こちら 。