Tomcat提供了灵活且简单的JNDI及Datasource设置方式。
这里,记录我在学习应用过程中的问题。
1. 首先要在你的自己的应用程序中WEB-INF/web.xml 中添加一个Resource-ref, 指定Datasource 的 JNDI。
<resource-ref>
<description>DB Connection</description>
<res-ref-name>jdbc/storedb</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
2. 在Tomcat的 config/context.xml中添加Resource。 (本例中使用的是MySql作为 DB)
<!-- maxActive: Maximum number of dB connections in pool. Make sure you
configure your mysqld max_connections large enough to handle
all of your db connections. Set to -1 for no limit.
-->
<!-- maxIdle: Maximum number of idle dB connections to retain in pool.
Set to -1 for no limit. See also the DBCP documentation on this
and the minEvictableIdleTimeMillis configuration parameter.
-->
<!-- maxWait: Maximum time to wait for a dB connection to become available
in ms, in this example 10 seconds. An Exception is thrown if
this timeout is exceeded. Set to -1 to wait indefinitely.
-->
<!-- username and password: MySQL dB username and password for dB connections -->
<!-- driverClassName: Class name for the old mm.mysql JDBC driver is
org.gjt.mm.mysql.Driver - we recommend using Connector/J though.
Class name for the official MySQL Connector/J driver is com.mysql.jdbc.Driver.
-->
<!-- url: The JDBC connection url for connecting to your MySQL dB.
-->
<Resource name="jdbc/storedb" auth="Container" type="javax.sql.DataSource"
maxActive="100" maxIdle="30" maxWait="10000"
username="root" password="passw0rd" driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost:3306/storedb"/>