<!-- 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>;test</value>;
</parameter>;
<parameter>;
<name>;password</name>;
<value>;test</value>;
</parameter>;
<!-- Class name for mm.mysql JDBC driver -->;
<parameter>;
<name>;driverClassName</name>;
<value>;org.gjt.mm.mysql.Driver</value>;
</parameter>;
<!-- The JDBC connection url for connecting to your MySQL dB.-->;
<parameter>;
<name>;url</name>;
<value>;jdbc:mysql://localhost:3306/test</value>;
</parameter>;
</ResourceParams>;
F. 在web.xml中加入
<resource-ref>;
<description>;DB Connection</description>;
<res-ref-name>;jdbc/TestDB</res-ref-name>;
<res-type>;javax.sql.DataSource</res-type>;
<res-auth>;Container</res-auth>;
</resource-ref>;
这里的jdbc/TestDb要和C中Resource段的name匹配
G. 观察结果
首先确定数据库已经启动,接着启动Tomcat,如果Tomcat启动异常,可能的原因是数据库的JDBC库没有加载
最后打开浏览器,访问 http://localhost:8080/DBTest/ViewTable.jsp就可以看到结果作者: ii 时间: 2003-03-11 08:45 标题: JNDI(Java 命名和目录接口) Common Problems
Here are some common problems encountered with a web application which uses a database and tips for how to solve them.
Intermittent dB Connection Failures
Tomcat runs within a JVM. The JVM periodically performs garbage collection (GC) to remove java objects which are no longer being used. When the JVM performs GC execution of code within Tomcat freezes. If the maximum time configured for establishment of a dB connection is less than the amount of time garbage collection took you can get a db conneciton failure.
To collect data on how long garbage collection is taking add the -verbose:gc argument to your CATALINA_OPTS environment variable when starting Tomcat. When verbose gc is enabled your $CATALINA_BASE/logs/catalina.out log file will include data for every garbage collection including how long it took.
When your JVM is tuned correctly 99% of the time a GC will take less than one second. The remainder will only take a few seconds. Rarely, if ever should a GC take more than 10 seconds.
Make sure that the db connection timeout is set to 10-15 seconds. For the DBCP you set this using the parameter maxWait.
Random Connection Closed Exceptions
These can occur when one request gets a db connection from the connection pool and closes it twice. When using a connection pool, closing the connection just returns it to the pool for reuse by another request, it doesn't close the connection. And Tomcat uses multiple threads to handle concurrent requests. Here is an example of the sequence of events which could cause this error in Tomcat:
Request 1 running in Thread 1 gets a db connection.
Request 1 closes the db connection.
The JVM switches the running thread to Thread 2
Request 2 running in Thread 2 gets a db connection
(the same db connection just closed by Request 1).
The JVM switches the running thread back to Thread 1
Request 1 closes the db connection a second time in a finally block.
The JVM switches the running thread back to Thread 2
Request 2 Thread 2 tries to use the db connection but fails
because Request 1 closed it.
Here is an example of properly written code to use a db connection obtained from a connection pool:
Connection conn = null;
Statement stmt = null; // Or PreparedStatement if needed
ResultSet rs = null;
try {
conn = ... get connection from connection pool ...
stmt = conn.createStatement("select ...");
rs = stmt.executeQuery();
... iterate through the result set ...
rs.close();
rs = null;
stmt.close();
stmt = null;
conn.close(); // Return to connection pool
conn = null; // Make sure we don't close it twice
} catch (SQLException e) {
... deal with errors ...
} finally {
// Always make sure result sets and statements are closed,
// and the connection is returned to the pool
if (rs != null) {
try { rs.close(); } catch (SQLException e) { ; }
rs = null;
}
if (stmt != null) {
try { stmt.close(); } catch (SQLException e) { ; }
stmt = null;
}
if (conn != null) {
try { conn.close(); } catch (SQLException e) { ; }
conn = null;
}
}作者: ii 时间: 2003-03-11 08:46 标题: JNDI(Java 命名和目录接口) 0. Introduction
Versions of MySQL and the mm.mysql JDBC driver when have been reported to work:
MySQL 3.23.47, MySQL 3.23.47 using InnoDB, MySQL 4.0.1alpha
mm.mysql 2.0.14 (JDBC Driver)
Please let us know if you have tested the new MySQL mm.mysql 3.0 driver.
1. MySQL configuration
Ensure that you follow these instructions as variations can cause problems.
Create a new test user, a new database and a single test table. Your MySQL user must have a password assigned. The driver will fail if you try to connect with an empty password.
mysql>; GRANT ALL PRIVILEGES ON *.* TO javauser@localhost
->; IDENTIFIED BY 'javadude' WITH GRANT OPTION;
mysql>; create database javatest;
mysql>; use javatest;
mysql>; create table testdata (
->; id int not null auto_increment primary key,
->; foo varchar(25),
->; bar int);
Note: the above user should be removed once testing is complete!
Next insert some test data into the testdata table.
<!-- 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>;javauser</value>;
</parameter>;
<parameter>;
<name>;password</name>;
<value>;javadude</value>;
</parameter>;
<!-- Class name for mm.mysql JDBC driver -->;
<parameter>;
<name>;driverClassName</name>;
<value>;org.gjt.mm.mysql.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:3306/javatest?autoReconnect=true</value>;
</parameter>;
</ResourceParams>;
</Context>;
3. web.xml configuration
Now create a WEB-INF/web.xml for this test application.
<?xml version="1.0" encoding="ISO-8859-1"?>;
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd">;
<web-app>;
<description>;MySQL Test App</description>;
<resource-ref>;
<description>;DB Connection</description>;
<res-ref-name>;jdbc/TestDB</res-ref-name>;
<res-type>;javax.sql.DataSource</res-type>;
<res-auth>;Container</res-auth>;
</resource-ref>;
</web-app>;
4. Test code
Now create a simple test.jsp for use later.
<%
foo.DBTest tst = new foo.DBTest();
tst.init();
%>;
<h2>;Results</h2>;
Foo <%= tst.getFoo() %>;<br/>;
Bar <%= tst.getBar() %>;
</body>;
</html>;
And create a Java class to actually use your new Datasource and connection pool. Note: this code isn't anywhere near production ready - it's only supposed to be used as a simple test :-)
public String getFoo() { return foo; }
public int getBar() { return bar;}
}
Finally deploy your web app into $CATALINA_HOME/webapps either as a warfile called DBTest.war or into a sub-directory called DBTest
Once deployed, point a browser at http://localhost:8080/DBTest/test.jsp to view the fruits of your hard work.作者: ii 时间: 2003-03-11 09:19 标题: JNDI(Java 命名和目录接口) datasource:
http://jakarta.apache.org/tomcat/tomcat-4.1-doc/jndi-datasource-examples-howto.html