无法使用tomcat在spring中配置jndi数据库

时间:2017-04-25 07:39:42

标签: spring jndi

这是我的server.xml,我收到了 javax.naming.NameNotFoundException:名称UserDatabase未绑定在此Context中。如果我删除了Realm,那么我getjavax.naming.NameNotFoundException:在此上下文中找不到datsource。这有什么不对?

<?xml version="1.0" encoding="UTF-8"?>
<Server port="8005" shutdown="SHUTDOWN">
<Listener SSLEngine="on"
    className="org.apache.catalina.core.AprLifecycleListener" />
<Listener className="org.apache.catalina.core.JasperListener" />
<Listener className="org.apache.catalina.core.JreMemoryLeakPreventionListener" />
<Listener className="org.apache.catalina.mbeans.ServerLifecycleListener" />
<Listener className="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener" />
<GlobalNamingResources>
    <Resource auth="Container" description="Pas testing UCP Pool in Tomcat"
        driverClassName="oracle.jdbc.OracleDriver" maxIdle="10" maxWait="-1"
        name="myDatabase" password="abc123" pathname="conf/context.xml"
        readOnly="true" type="javax.sql.DataSource"
        url="myurl" user="admin"
        vreadOnly="true" xActive="20" />
</GlobalNamingResources>
<Service name="Catalina">
    <Connector connectionTimeout="20000" port="8080" protocol="HTTP/1.1"
        redirectPort="8443" />
    <!-- Define an AJP 1.3 Connector on port 8009 -->
    <Connector port="8009" protocol="AJP/1.3" redirectPort="8443" />

    <Engine defaultHost="localhost" name="Catalina">
        <Realm className="org.apache.catalina.realm.UserDatabaseRealm"
            resourceName="UserDatabase" />

        <Host appBase="webapps" autoDeploy="true" name="localhost"
            unpackWARs="true" xmlNamespaceAware="false" xmlValidation="false">
        </Host>
    </Engine>
</Service>
</Server>

Context.xml

<?xml version="1.0" encoding="UTF-8"?>
<Context>
<!-- Default set of monitored resources -->
<ResourceLink auth="Container" global="myDatabase"
    name="myDatabase" type="javax.sql.DataSource" />
<WatchedResource>WEB-INF/web.xml</WatchedResource>

</Context>

Spring bean xml

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd"> 
<bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
        <property name="jndiName" value="myDatabase"/>
</bean>

web.xml我添加了下面的行

<resource-ref>
    <description>DB Connection</description>
    <res-ref-name>CitilinxJdbcDatasource</res-ref-name>
    <res-type>javax.sql.DataSource</res-type>
    <res-auth>Container</res-auth>
</resource-ref>

1 个答案:

答案 0 :(得分:1)

这对我有用:

name = "jdbc/myDatabase"

在Tomcat的<Resource>

java:/comp/env/jdbc/myDatabase

在Spring的JndiObjectFactoryBean.jndiName