Spring MVC + 2 JNDI 数据源

Spring MVC + 2 JNDI Datasources

目前正在处理 Spring MVC 项目。它利用 Apache Tomcat 从我的 SQL 数据库中获取数据。

目前,我可以从单个数据源中获取数据,但我的下一个目标是从两个数据源中获取信息。

这是文件结构的图片

包含数据源信息的context.xml文件

<Context useHttpOnly="true">

<Realm
className="com.sentry.tomcat.realm.AuthenticationServiceRealm"
applicationName="CLUB"
environmentName="Dev"
logicalDomainNames="Intranet,ServiceAccounts"
authenticationProfileName="SentryLogin" 
/>

<Valve
className="org.apache.catalina.valves.AccessLogValve"
directory="${server.log.base}"
fileDateFormat="yyyy-MM-dd"
pattern="%t ${server.hostName}  ${server.domainName}    ${tomcat.instancename}  Dev %a  %{X-Forwarded-For}i %m  %U  %q  %p  %u  %v  %A  %H  %{User-Agent}i  %S  %I  %{Referer}i %s  %b  %D  tomcat_v2"
prefix="SEL_access_log_tomcat_v2."
suffix=".log"
/>

<Resource
logAbandoned="true" 
removeAbandonedOnBorrow="true" 
removeAbandonedTimeout="60000"
testOnBorrow="true"
validationQuery="SELECT 1"
maxWaitMillis="10000"
maxIdle="100"
maxTotal="100"
url="jdbc:sqlserver://*server name*;databaseName=*db name*;integratedSecurity=true;sendStringParametersAsUnicode=false"
driverClassName="com.microsoft.sqlserver.jdbc.SQLServerDriver"
type="javax.sql.DataSource"
auth="Container"
name="jdbc/CLUB"
/>

 <Resource
logAbandoned="true" 
removeAbandonedOnBorrow="true" 
removeAbandonedTimeout="60000"
testOnBorrow="true"
validationQuery="SELECT 1"
maxWaitMillis="10000"
maxIdle="100"
maxTotal="100"
url="jdbc:sqlserver://*server name*;databaseName=*db name*;integratedSecurity=true;sendStringParametersAsUnicode=false"
driverClassName="com.microsoft.sqlserver.jdbc.SQLServerDriver"
type="javax.sql.DataSource"
auth="Container"
name="jdbc/PC"
/>

导入两个不同资源的应用程序-context.xml文件。

<?xml version="1.0" encoding="UTF-8"?>
<import resource="CLUB-datasource-context.xml"/> 
<import resource="PC-datasource-context.xml"/> 

<context:component-scan base-package="com.sentry.club.service" />

导入的两个上下文xml文件 俱乐部

<?xml version="1.0" encoding="UTF-8"?>
<!--  JNDI Data Source --> 
<bean id="jndiDataSource" class="org.springframework.jndi.JndiObjectFactoryBean" lazy-init="true">
    <property name="jndiName" value="java:/comp/env/jdbc/CLUB"/>
</bean> 

<bean id="hibernateJpaVendorAdapter" class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" />

<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    <property name="dataSource" ref="jndiDataSource" />
    <property name="jpaVendorAdapter" ref="hibernateJpaVendorAdapter" />
    <property name="jpaProperties">
        <props>         
            <prop key="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</prop>
            <prop key="hibernate.cache.use_second_level_cache">false</prop>
            <prop key="hibernate.connection.release_mode">after_transaction</prop>
            <prop key="hibernate.show_sql">true</prop>      
            <prop key="hibernate.format_sql">true</prop>
            <!-- Get root categories was causing an LazyInitializationException.  Not sure why this didn't happen pre JPA.
                This property puts a bandaid over that error but we really should find a way to provide the actual entity manager -->
            <prop key="hibernate.enable_lazy_load_no_trans">true</prop>
        </props>
    </property>
    <!-- domain packages to scan are defined here -->   
    <property name="packagesToScan" value="com.sentry.club.phraseology.domain" />
    <property name="sharedCacheMode" value="ENABLE_SELECTIVE" />
    <property name="validationMode" value="NONE" />
</bean>

<bean id="transactionManager"
    class="org.springframework.orm.jpa.JpaTransactionManager">
    <property name="entityManagerFactory" ref="entityManagerFactory" />
    <qualifier value="jpa" />
</bean>

<!-- repository package defined here -->    
<jpa:repositories base-package="com.sentry.club.phraseology.repository"/>

电脑

<?xml version="1.0" encoding="UTF-8"?>

<!--  JNDI Data Source --> 
<bean id="jndiDataSource" class="org.springframework.jndi.JndiObjectFactoryBean" lazy-init="true">
    <property name="jndiName" value="java:/comp/env/jdbc/PC"/>
</bean> 

<bean id="hibernateJpaVendorAdapter" class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" />

<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    <property name="dataSource" ref="jndiDataSource" />
    <property name="jpaVendorAdapter" ref="hibernateJpaVendorAdapter" />
    <property name="jpaProperties">
        <props>         
            <prop key="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</prop>
            <prop key="hibernate.cache.use_second_level_cache">false</prop>
            <prop key="hibernate.connection.release_mode">after_transaction</prop>
            <prop key="hibernate.show_sql">true</prop>      
            <prop key="hibernate.format_sql">true</prop>
            <!-- Get root categories was causing an LazyInitializationException.  Not sure why this didn't happen pre JPA.
                This property puts a bandaid over that error but we really should find a way to provide the actual entity manager -->
            <prop key="hibernate.enable_lazy_load_no_trans">true</prop>
        </props>
    </property>
    <!-- domain packages to scan are defined here -->   
    <property name="packagesToScan" value="com.sentry.club.classcode.domain" />
    <property name="sharedCacheMode" value="ENABLE_SELECTIVE" />
    <property name="validationMode" value="NONE" />
</bean>

<bean id="transactionManager"
    class="org.springframework.orm.jpa.JpaTransactionManager">
    <property name="entityManagerFactory" ref="entityManagerFactory" />
    <qualifier value="jpa" />
</bean>

<!-- repository package defined here -->    
<jpa:repositories base-package="com.sentry.club.classcode.repository"/>

我收到这个错误

2019-06-20T13:27:28,018 ERROR [] org.apache.catalina.core.ContainerBase.[Catalina].[localhost].[/club]:4798 Exception sending context initialized event to listener instance of class [org.springframework.web.context.ContextLoaderListener]
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'basisRepository': Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Not an managed type: class com.sentry.club.classcode.domain.Basis
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1631) ~[spring-beans-4.3.21.RELEASE.jar:4.3.21.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:553) ~[spring-beans-4.3.21.RELEASE.jar:4.3.21.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:481) ~[spring-beans-4.3.21.RELEASE.jar:4.3.21.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.getObject(AbstractBeanFactory.java:312) ~[spring-beans-4.3.21.RELEASE.jar:4.3.21.RELEASE]
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) ~[spring-beans-4.3.21.RELEASE.jar:4.3.21.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:308) ~[spring-beans-4.3.21.RELEASE.jar:4.3.21.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197) ~[spring-beans-4.3.21.RELEASE.jar:4.3.21.RELEASE]
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:742) ~[spring-beans-4.3.21.RELEASE.jar:4.3.21.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:867) ~[spring-context-4.3.21.RELEASE.jar:4.3.21.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:543) ~[spring-context-4.3.21.RELEASE.jar:4.3.21.RELEASE]
    at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:443) ~[spring-web-4.3.21.RELEASE.jar:4.3.21.RELEASE]
    at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:325) ~[spring-web-4.3.21.RELEASE.jar:4.3.21.RELEASE]
    at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:107) ~[spring-web-4.3.21.RELEASE.jar:4.3.21.RELEASE]
    at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4792) [catalina.jar:8.5.32]
    at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5256) [catalina.jar:8.5.32]
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150) [catalina.jar:8.5.32]
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1421) [catalina.jar:8.5.32]
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1411) [catalina.jar:8.5.32]
    at java.util.concurrent.FutureTask.run(FutureTask.java:266) [?:1.8.0_161]
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) [?:1.8.0_161]
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) [?:1.8.0_161]
    at java.lang.Thread.run(Thread.java:748) [?:1.8.0_161]
Caused by: java.lang.IllegalArgumentException: Not an managed type: class com.sentry.club.classcode.domain.Basis
    at org.hibernate.jpa.internal.metamodel.MetamodelImpl.managedType(MetamodelImpl.java:219) ~[hibernate-entitymanager-4.3.10.Final.jar:4.3.10.Final]
    at org.springframework.data.jpa.repository.support.JpaMetamodelEntityInformation.<init>(JpaMetamodelEntityInformation.java:71) ~[spring-data-jpa-1.11.15.RELEASE.jar:?]
    at org.springframework.data.jpa.repository.support.JpaEntityInformationSupport.getEntityInformation(JpaEntityInformationSupport.java:68) ~[spring-data-jpa-1.11.15.RELEASE.jar:?]
    at org.springframework.data.jpa.repository.support.JpaRepositoryFactory.getEntityInformation(JpaRepositoryFactory.java:184) ~[spring-data-jpa-1.11.15.RELEASE.jar:?]
    at org.springframework.data.jpa.repository.support.JpaRepositoryFactory.getTargetRepository(JpaRepositoryFactory.java:117) ~[spring-data-jpa-1.11.15.RELEASE.jar:?]
    at org.springframework.data.jpa.repository.support.JpaRepositoryFactory.getTargetRepository(JpaRepositoryFactory.java:99) ~[spring-data-jpa-1.11.15.RELEASE.jar:?]
    at org.springframework.data.repository.core.support.RepositoryFactorySupport.getRepository(RepositoryFactorySupport.java:198) ~[spring-data-commons-1.13.15.RELEASE.jar:?]
    at org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.initAndReturn(RepositoryFactoryBeanSupport.java:277) ~[spring-data-commons-1.13.15.RELEASE.jar:?]
    at org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.afterPropertiesSet(RepositoryFactoryBeanSupport.java:263) ~[spring-data-commons-1.13.15.RELEASE.jar:?]
    at org.springframework.data.jpa.repository.support.JpaRepositoryFactoryBean.afterPropertiesSet(JpaRepositoryFactoryBean.java:101) ~[spring-data-jpa-1.11.15.RELEASE.jar:?]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1689) ~[spring-beans-4.3.21.RELEASE.jar:4.3.21.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1627) ~[spring-beans-4.3.21.RELEASE.jar:4.3.21.RELEASE]
    ... 21 more
2019-06-20T13:27:28,446 ERROR [] org.apache.catalina.core.StandardContext:5257 One or more listeners failed to start. Full details will be found in the appropriate container log file
2019-06-20T13:27:28,460 ERROR [] org.apache.catalina.core.StandardContext:5308 Context [/club] startup failed due to previous errors
2019-06-20T13:27:28,470 INFO  [] org.apache.catalina.core.ContainerBase.[Catalina].[localhost].[/club]:635 Closing Spring root WebApplicationContext
2019-06-20T13:27:28,507 INFO  [] org.apache.coyote.http11.Http11NioProtocol:588 Starting ProtocolHandler ["http-nio-127.0.0.1-80"]
2019-06-20T13:27:28,522 INFO  [] org.apache.coyote.http11.Http11NioProtocol:588 Starting ProtocolHandler ["https-jsse-nio-127.0.0.1-443"]
2019-06-20T13:27:28,529 INFO  [] org.apache.catalina.startup.Catalina:694 Server startup in 14701 ms

我能够解决问题。在每个 datasource-context.xml 文件中,您需要唯一地命名 beans(包括 entityManager、transactionManager 等)。此外,您需要使用 entity-manager-factory-ref= 属性在 jpa:repositories 元素中引用实体管理器。

    <?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jee="http://www.springframework.org/schema/jee"
    xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:jpa="http://www.springframework.org/schema/data/jpa"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
     http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-2.0.xsd
     http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
     http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc.xsd
     http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.0.xsd">

    <!--  JNDI Data Source --> 
    <bean id="PCDataSource" class="org.springframework.jndi.JndiObjectFactoryBean" lazy-init="true">
        <property name="jndiName" value="java:/comp/env/jdbc/PC"/>
    </bean> 

    <bean id="hibernateJpaVendorAdapter" class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" />

    <bean id="PCentityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        <property name="dataSource" ref="PCDataSource" />
        <property name="jpaVendorAdapter" ref="hibernateJpaVendorAdapter" />
        <property name="jpaProperties">
            <props>         
                <prop key="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</prop>
                <prop key="hibernate.cache.use_second_level_cache">false</prop>
                <prop key="hibernate.connection.release_mode">after_transaction</prop>
                <prop key="hibernate.show_sql">true</prop>      
                <prop key="hibernate.format_sql">true</prop>
                <!-- Get root categories was causing an LazyInitializationException.  Not sure why this didn't happen pre JPA.
                    This property puts a bandaid over that error but we really should find a way to provide the actual entity manager -->
                <prop key="hibernate.enable_lazy_load_no_trans">true</prop>
            </props>
        </property>
        <!-- domain packages to scan are defined here -->   
        <property name="packagesToScan" value="com.sentry.club.classcode.domain" />
        <property name="sharedCacheMode" value="ENABLE_SELECTIVE" />
        <property name="validationMode" value="NONE" />
        <!-- <context:component-scan base-package="com.sentry.club.classcode.domain"/> -->
    </bean>

    <bean id="transactionManager"
        class="org.springframework.orm.jpa.JpaTransactionManager">
        <property name="entityManagerFactory" ref="PCentityManagerFactory" />
        <qualifier value="jpa" />
    </bean>

    <!-- repository package defined here -->    
    <jpa:repositories base-package="com.sentry.club.classcode.repository" entity-manager-factory-ref="PCentityManagerFactory"/>

</beans>