这是连接的代码
package com.sms.util;import java.sql.*;
import javax.naming.*;
import javax.sql.DataSource;//连接类
public class ConnectionUtil {
private Connection connection;
public ConnectionUtil() { Context ctx = null;
try {
ctx = new InitialContext();
DataSource ds = (DataSource) ctx.lookup("java:comp/env/jdbc/sms");
try {
connection = ds.getConnection();
}
catch (SQLException ex1) {
ex1.printStackTrace();
}
}
catch (NamingException ex) {
ex.printStackTrace();
}
} public Connection getConnection() {
return connection;
} public void close() {
try {
//判断如果数据库连接没有关的情况下
//就关闭之
//if (!connection.isClosed()) {
connection.close();
//}
}
catch (SQLException e) {
connection = null;
e.printStackTrace();
}
connection = null;
}
}
在servlet里得到连接 用后关闭
public void doPost(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException {
ConnectionUtil connection = new ConnectionUtil();
//Connection conn = connection.getConnection();
PreparedStatement pstmt;
PreparedStatement pstmt1;
try {
pstmt = connection.getConnection().prepareStatement(
"select C_PASS_POINT from JIFEN_LOGONUSER where C_USERNUM=?");
catch (SQLException ex1) {
ex1.printStackTrace();
}
finally {
connection.close();
}
httpSession.setAttribute("sendContent", content);
response.sendRedirect("sendResponse.jsp");
}
但是过一段时间就会报java.sql.SQLException: DBCP could not obtain an idle db connection, pool exhausted 错
好像是并没有关闭的连接 但是我已经调用了close的方法了 连接并没有返还连接池
package com.sms.util;import java.sql.*;
import javax.naming.*;
import javax.sql.DataSource;//连接类
public class ConnectionUtil {
private Connection connection;
public ConnectionUtil() { Context ctx = null;
try {
ctx = new InitialContext();
DataSource ds = (DataSource) ctx.lookup("java:comp/env/jdbc/sms");
try {
connection = ds.getConnection();
}
catch (SQLException ex1) {
ex1.printStackTrace();
}
}
catch (NamingException ex) {
ex.printStackTrace();
}
} public Connection getConnection() {
return connection;
} public void close() {
try {
//判断如果数据库连接没有关的情况下
//就关闭之
//if (!connection.isClosed()) {
connection.close();
//}
}
catch (SQLException e) {
connection = null;
e.printStackTrace();
}
connection = null;
}
}
在servlet里得到连接 用后关闭
public void doPost(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException {
ConnectionUtil connection = new ConnectionUtil();
//Connection conn = connection.getConnection();
PreparedStatement pstmt;
PreparedStatement pstmt1;
try {
pstmt = connection.getConnection().prepareStatement(
"select C_PASS_POINT from JIFEN_LOGONUSER where C_USERNUM=?");
catch (SQLException ex1) {
ex1.printStackTrace();
}
finally {
connection.close();
}
httpSession.setAttribute("sendContent", content);
response.sendRedirect("sendResponse.jsp");
}
但是过一段时间就会报java.sql.SQLException: DBCP could not obtain an idle db connection, pool exhausted 错
好像是并没有关闭的连接 但是我已经调用了close的方法了 连接并没有返还连接池
比如:关于连接池的回收问题,一端时间后就会报出个pool bound的异常,而且默认的连接池数目偏小,好象就几个;
这时可以自己手动修改下数值,并可加上
<parameter>
<name>removeAbandoned</name>
<!-- Abandoned DB connections are removed and recycled -->
<value>true</value>
</parameter>
则会自动回收连接资源
就可以了。具体的 连接池 自动设放 多余的connection
否则用连接池没有意义了
我加上了没有效果 上面的标签好像在tomcat4.x下好用 5.x感觉没有效果