server.xml中配置如下<Context path="/ourhome" docBase="ourhome"
debug="5" reloadable="true" crossContext="true"> <Logger className="org.apache.catalina.logger.FileLogger"
prefix="localhost_ourhome_log." suffix=".txt"
timestamp="true"/> <Resource name="jdbc/AuctionDB"
auth="Container"
type="javax.sql.DataSource"/> <ResourceParams name="jdbc/AuctionDB">
<parameter>
<name>factory</name>
<value>org.apache.commons.dbcp.BasicDataSourceFactory</value>
</parameter> <!-- 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 0 for no limit.
-->
<parameter>
<name>maxActive</name>
<value>100</value>
</parameter> <!-- Maximum number of idle dB connections to retain in pool.
Set to 0 for no limit.
-->
<parameter>
<name>maxIdle</name>
<value>30</value>
</parameter> <!-- 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.
-->
<parameter>
<name>maxWait</name>
<value>10000</value>
</parameter> <!-- MySQL dB username and password for dB connections -->
<parameter>
<name>username</name>
<value>root</value>
</parameter>
<parameter>
<name>password</name>
<value></value>
</parameter> <!-- Class name for mm.mysql JDBC driver -->
<parameter>
<name>driverClassName</name>
<value>com.mysql.jdbc.Driver</value>
</parameter> <!-- The JDBC connection url for connecting to your MySQL dB.
The autoReconnect=true argument to the url makes sure that the
mm.mysql JDBC Driver will automatically reconnect if mysqld closed the
connection. mysqld by default closes idle connections after 8 hours.
-->
<parameter>
<name>url</name>
<value>jdbc:mysql://localhost/auction?autoReconnect=true</value>
</parameter>
</ResourceParams>
</Context>web.xml中配置如下
<resource-ref>
<description>DB Connection</description>
<res-ref-name>jdbc/AuctionDB</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-sharing-scope>Shareable</res-sharing-scope>
</resource-ref>程序中代码如下:
public static ConnectionPool getInstance() {
try {
if (mySelf == null) {
Context initCtx = new InitialContext();
DataSource ds = (DataSource)initCtx.lookup(JNDI_NAME);
mySelf = new ConnectionPool(ds);
}
return mySelf;
}catch(NamingException ex) {
ex.printStackTrace();
throw new RuntimeException("error.unexpected");
}
}public Connection getConnection(boolean autoCommit) throws SQLException {
Connection con = ds.getConnection();
con.setAutoCommit(autoCommit);
return con;
}
程序运行到con=ds.getConnection();抛出以上错误
debug="5" reloadable="true" crossContext="true"> <Logger className="org.apache.catalina.logger.FileLogger"
prefix="localhost_ourhome_log." suffix=".txt"
timestamp="true"/> <Resource name="jdbc/AuctionDB"
auth="Container"
type="javax.sql.DataSource"/> <ResourceParams name="jdbc/AuctionDB">
<parameter>
<name>factory</name>
<value>org.apache.commons.dbcp.BasicDataSourceFactory</value>
</parameter> <!-- 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 0 for no limit.
-->
<parameter>
<name>maxActive</name>
<value>100</value>
</parameter> <!-- Maximum number of idle dB connections to retain in pool.
Set to 0 for no limit.
-->
<parameter>
<name>maxIdle</name>
<value>30</value>
</parameter> <!-- 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.
-->
<parameter>
<name>maxWait</name>
<value>10000</value>
</parameter> <!-- MySQL dB username and password for dB connections -->
<parameter>
<name>username</name>
<value>root</value>
</parameter>
<parameter>
<name>password</name>
<value></value>
</parameter> <!-- Class name for mm.mysql JDBC driver -->
<parameter>
<name>driverClassName</name>
<value>com.mysql.jdbc.Driver</value>
</parameter> <!-- The JDBC connection url for connecting to your MySQL dB.
The autoReconnect=true argument to the url makes sure that the
mm.mysql JDBC Driver will automatically reconnect if mysqld closed the
connection. mysqld by default closes idle connections after 8 hours.
-->
<parameter>
<name>url</name>
<value>jdbc:mysql://localhost/auction?autoReconnect=true</value>
</parameter>
</ResourceParams>
</Context>web.xml中配置如下
<resource-ref>
<description>DB Connection</description>
<res-ref-name>jdbc/AuctionDB</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-sharing-scope>Shareable</res-sharing-scope>
</resource-ref>程序中代码如下:
public static ConnectionPool getInstance() {
try {
if (mySelf == null) {
Context initCtx = new InitialContext();
DataSource ds = (DataSource)initCtx.lookup(JNDI_NAME);
mySelf = new ConnectionPool(ds);
}
return mySelf;
}catch(NamingException ex) {
ex.printStackTrace();
throw new RuntimeException("error.unexpected");
}
}public Connection getConnection(boolean autoCommit) throws SQLException {
Connection con = ds.getConnection();
con.setAutoCommit(autoCommit);
return con;
}
程序运行到con=ds.getConnection();抛出以上错误
private static String JNDI_NAME = "java:comp/env/jdbc/AuctionDB";
一个上午还没搞定,郁闷up
解决方法如下,给你参考一下:
我的问题出在两个地方,
一个是driver的问题,我用的是oracle9i,所以要用classes111.jar,而不是class12.jar。
二是在servel.xml
建连接池,虚拟路径,我一直都是
<Context path="" docBase="ROOT" debug="0" >
所以总是报 Cannot load JDBC driver class 'null'
改成 <Context path="/myapp" docBase="\myapp.war" debug="0" >
就ok了。
有人说可以建全局的连接池,我还没有成功,所以就针对这个应用专用连接池了。你多试试看,我曾经一度放弃了建tomcat连接池,
在朋友帮助下,我成功了,相信你也可以做成的。