我已将tomcat lib dir中的tomcat-dbcp.jar复制到jdk的jre \ lib \ ext目录 在我的应用程序的context.xml文件中编码如下:
<?xml version="1.0" encoding="UTF-8"?>
<Context antiJARLocking="true" path="/Conn_Pool">
<Resource name="jdbc/murach" auth="Container"
maxActive="100" maxIdle="30" maxWait="10000"
username="root" password="1234"
driverClassName=" com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost:3306/murach"
logAbandoned="true" removeAbandoned="true"
removeAbandonedTimeout="60" type="javax.sql.DataSource"
/>
</Context>
和类ConnectionPool.java如下:
package data;
import java.sql.Connection;
import java.sql.SQLException;
import javax.sql.*;
import javax.sql.DataSource;
import javax.naming.InitialContext;
public class ConnectionPool
{
private static ConnectionPool pool=null;
private static DataSource dataSource=null;
private ConnectionPool()
{
try
{
InitialContext ic = new InitialContext();
dataSource = (DataSource)ic.lookup("java:/comp/env/jdbc/murach");
}
catch(Exception e)
{
e.printStackTrace();
}
}
public static ConnectionPool getInstance()
{
if(pool==null)
{
pool= new ConnectionPool();
}
return pool;
}
public Connection getConnection()
{
try
{
return dataSource.getConnection();
}
catch(SQLException sqle)
{
sqle.printStackTrace();
return null;
}
}
public void freeConnection(Connection c)
{
try{
c.close();
}
catch(SQLException sqle){
sqle.printStackTrace();
}
}
}
但公共连接getConnection()无法正常工作 它没有返回dataSource.getConnection();在try块中。相反它 在catch块内