0

我实现了一个在 Tomcat 7 下的 Axis2 (Jax-WS) 上运行的 Java Web 服务。为了从 postgreSQL 获取数据,这个 Web 服务使用了 mybatis 框架。如果我在 mybatis-config.xml 中手动设置连接参数,一切正常,但我想使用 JNDI 数据源直接通过服务器上下文获取它们。

在 myBatis-config.xml 中,我放置了以下标签(在环境标签内):

<dataSource type="JNDI">
    <property name="data_source" value="java:comp/env/jdbc/myWebService" />
</dataSource>

在互联网上搜索我也了解到我必须创建一个带有以下标签的 Tomcat(或 Axis2?)上下文:

<Resource name="jdbc/myWebService" auth="Container" type="javax.sql.DataSource" driverClassName="org.postgresql.Driver" url="jdbc:postgresql://localhost:5432/myDB" username="user" password="password" maxActive="20" maxIdle="10" maxWait="-1"/>

<resource-ref>
    <description>ConnectionToPostgreSQL</description>
    <res-ref-name>jdbc/myWebService</res-ref-name>
    <res-type>javax.sql.DataSource</res-type>
    <res-auth>Container</res-auth>
    <res-sharing-scope>Shareable</res-sharing-scope>
</resource-ref>

但我不知道我必须将它们放在哪里(在哪个 xml 文件中)以及是否需要其他任何东西。

4

1 回答 1

0

Resource定义可以在文件中定义[your webapp name]/META-INF/context.xml,例如:

<?xml version='1.0' encoding='utf-8'?>
<Context path="/[your webapp context]" privileged="true">
    <Resource name="jdbc/myWebService" auth="Container" type="javax.sql.DataSource" driverClassName="org.postgresql.Driver" url="jdbc:postgresql://localhost:5432/myDB" username="user" password="password" maxActive="20" maxIdle="10" maxWait="-1"/>
</Context>

还有其他选项 - 例如在server.xml或 shared中定义它context。请参阅JNDI 资源 HOW-TO

进入resource-ref你的 [your webapp name]/WEB-INF/web.xml.

于 2013-08-07T02:10:25.480 回答