我正在通过 JNDI 资源使用 tomcat 连接池。
在context.xml
:
<Resource name="jdbc/mydb" auth="Container" type="javax.sql.DataSource"
username="root" password="root" driverClassName="com.mysql.jdbc.Driver"
maxActive="1000" maxIdle="100" maxWait="10000"
url="jdbc:mysql://localhost:3306/mydatabase"
factory="org.apache.tomcat.jdbc.pool.DataSourceFactory" />
在web.xml
:
<resource-ref>
<description>DB Connection</description>
<res-ref-name>jdbc/mydb</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
从我需要数据库连接的 java 类中,我进行以下查找:
Context initContext = new InitialContext();
DataSource ds = (DataSource)initContext.lookup("java:/comp/env/jdbc/mydb");
我的第一个疑问是 DataSource 类型。使用javax.sql.DataSource
or是一样的org.apache.tomcat.jdbc.pool.DataSource
吗?
此外,有时我会收到“连接太多”错误。我已经阅读了许多关于此的 stackoverflow 问题/答案,但我没有成功理解问题可能出在哪里。
我遵循了 tomcat 文档,并正确关闭了结果集、语句和连接。
编辑
我的tomcat版本是7.0.26。所以应该有一个错误(见informatik01用户建议的链接)