我实现了一个在 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 文件中)以及是否需要其他任何东西。