数据库连接池释放连接之问 数据库连接池并不是真正的关掉连接,当使用是,从一个池里取出空闲的连接,使用完毕然后归还到池子里,你的情况就是后面没做,使用完毕后没有归还到连接池,所以你去察看你执行完查询程序后是否调用归还语句,因为现在一般都是用代理机制把con.close代理到你的类里去了,所以可能你要手工con.close执行一下 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 public Vector allRoom() throws Exception { Connection conn = null; Vector allRoom = new Vector(); String selectSql = "select r.id,r.bianhao,r.nb,s.states,rpt.types,rpt.price from room r,states s,roomPT rpt where r.stateid=s.id and r.roomtypeid = rpt.id"; try { conn = pool.getConnection(); Statement stmt = conn.createStatement(); rs = stmt.executeQuery(selectSql); while (rs.next()) { Room r = new Room(); r.setId(rs.getInt("id")); r.setBianhao(rs.getString("bianhao")); r.setNb(rs.getString("nb")); r.setStateid(rs.getString("states")); r.setRoomtypeid(rs.getString("types")); r.setPrice(rs.getString("price")); allRoom.add(r); } } catch (Exception e) { e.printStackTrace(); } finally { BaseDB.closedb(conn); } return allRoom; }我写的每个方法都在finally块中把数据库连接给关闭了,可是为什么还是会这样啊。 BaseDB.closedb(conn);这里问题肯定有问题,你应该把你的BaseDB贴出来,因为你conn = pool.getConnection();这里使用的pool,而关闭BaseDB.closedb(conn);pool是BaseDB吗?信息太少了,连接池你自己写的吗? /* * package com.qhbg.db * class ConnectionPool * * 创建日期 2006-03-24 * * 安信电子有限公司 * * 开发者 王学 * */package com.qhbg.db;import java.sql.SQLException;import java.sql.Connection;import javax.sql.DataSource;public class ConnectionPool { private DataSource ds; private static ConnectionPool self; private ConnectionPool(DataSource ds) { this.ds = ds; } public static void init(DataSource ds) { self = new ConnectionPool(ds); } public static ConnectionPool getInstance() { if (self == null) { throw new IllegalStateException("Pool not init"); } return self; } public Connection getConnection() throws SQLException { return ds.getConnection(); }} /* * package com.qhbg.db * class DBInitServlet * * 创建日期 2006-03-24 * * 安信电子有限公司 * * 开发者 王学 * */package com.qhbg.db;import javax.servlet.http.HttpServlet;import javax.servlet.ServletConfig;import javax.servlet.ServletException;import java.sql.SQLException;import org.apache.struts.util.GenericDataSource;public class DBInitServlet extends HttpServlet { public void init(ServletConfig config) throws ServletException { super.init(config); try { GenericDataSource ds = new GenericDataSource(); ds.setDriverClass(getInitParameter("driverClass")); ds.setUrl(getInitParameter("jdbcURL")); ds.setMinCount(Integer.parseInt(getInitParameter("minCount"))); ds.setMaxCount(Integer.parseInt(getInitParameter("maxCount"))); ds.setAutoCommit(false); ds.setUser(getInitParameter("user")); ds.setPassword(getInitParameter("password")); ds.open(); ConnectionPool.init(ds); } catch (SQLException e) { e.printStackTrace(); System.out.println("error"); } }} package com.qhbg.db;import java.sql.Connection;public class BaseDB { /** * <p> * 关闭数据库连接 * */ public static void closedb(Connection conn) throws Exception { try { if (conn != null) conn.close(); } catch (Exception e) { e.printStackTrace(); } } /** * <p> * 数据库操作,事务回滚 * */ public static void rollbackdb(Connection conn) throws Exception { try { conn.rollback(); } catch (Exception e) { e.printStackTrace(); } }} ****不能连续发帖,偶用同事的再发帖****public class KehuDJDB implements KehuDJDAO { PreparedStatement ps = null; ResultSet rs = null; private ConnectionPool pool; public KehuDJDB() { pool = ConnectionPool.getInstance(); }在构造函数中取得pool然后方法如下::/** * <p> * 对数据库进行操作,显示某段时间内记帐或免费的人员 * * */ public Vector mouTyperuzhurenyuan(String riqi1,String riqi2,String i) throws Exception { Connection conn = null; Vector mouTyperuzhurenyuan = new Vector(); String selectSql = "select name,sex,age,sfzjh,address,gzdw,rzsj,lksj,huafei from kehuDJ where jztag = 2 " + "and rzsj >= '"+riqi1+"' and rzsj <= '"+riqi2+"' and bideType = "+i; try { conn = pool.getConnection(); Statement stmt = conn.createStatement(); rs = stmt.executeQuery(selectSql); while (rs.next()) { KehuDJ kj = new KehuDJ(); kj.setName(rs.getString("name")); kj.setSex(rs.getString("sex")); kj.setAge(rs.getInt("age")); kj.setSfzjh(rs.getInt("sfzjh")); kj.setAddress(rs.getString("address")); kj.setGzdw(rs.getString("gzdw")); kj.setRzsj(rs.getString("rzsj")); kj.setLksj(rs.getString("lksj")); kj.setJine(rs.getFloat("huafei")); mouTyperuzhurenyuan.add(kj); } } catch (Exception e) { e.printStackTrace(); } finally { BaseDB.closedb(conn); } return mouTyperuzhurenyuan; } 把BaseDB.closedb(conn);改成conn.close()吧,你这个不是连接池,去查下什么叫连接池 lz还是使用dbcp来管理连接池吧 在线求大神解答,hibernate里运行报错,本地执行sql正确 url中参数传递 文件读写乱码 属标放上去 出现菜单 像 淘宝网 关于视频格式转换的问题 英文版windows2000中的控制台中是默认字符集是 iso-8859 ,如何转为中文的字符集 紧急求助啊!有关与tomcat 5.0 oracle & MiddleGen的问题 毕业设计求救啊! 请问使用以下这些对象需要import哪些package?分别可以从哪download?(jdk1.3) WebService底层是采用什么机制呢 Hibernate,我疯了
Connection conn = null;
Vector allRoom = new Vector();
String selectSql = "select r.id,r.bianhao,r.nb,s.states,rpt.types,rpt.price from room r,states s,roomPT rpt where r.stateid=s.id and r.roomtypeid = rpt.id"; try { conn = pool.getConnection();
Statement stmt = conn.createStatement();
rs = stmt.executeQuery(selectSql); while (rs.next()) {
Room r = new Room();
r.setId(rs.getInt("id"));
r.setBianhao(rs.getString("bianhao"));
r.setNb(rs.getString("nb"));
r.setStateid(rs.getString("states"));
r.setRoomtypeid(rs.getString("types"));
r.setPrice(rs.getString("price")); allRoom.add(r);
} } catch (Exception e) {
e.printStackTrace();
} finally {
BaseDB.closedb(conn);
} return allRoom;
}我写的每个方法都在finally块中把数据库连接给关闭了,可是为什么还是会这样啊。
* package com.qhbg.db
* class ConnectionPool
*
* 创建日期 2006-03-24
*
* 安信电子有限公司
*
* 开发者 王学
*
*/
package com.qhbg.db;import java.sql.SQLException;
import java.sql.Connection;
import javax.sql.DataSource;public class ConnectionPool {
private DataSource ds; private static ConnectionPool self; private ConnectionPool(DataSource ds) {
this.ds = ds;
} public static void init(DataSource ds) {
self = new ConnectionPool(ds);
} public static ConnectionPool getInstance() {
if (self == null) {
throw new IllegalStateException("Pool not init");
}
return self;
} public Connection getConnection() throws SQLException {
return ds.getConnection();
}
}
* package com.qhbg.db
* class DBInitServlet
*
* 创建日期 2006-03-24
*
* 安信电子有限公司
*
* 开发者 王学
*
*/
package com.qhbg.db;import javax.servlet.http.HttpServlet;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;import java.sql.SQLException;
import org.apache.struts.util.GenericDataSource;
public class DBInitServlet extends HttpServlet {
public void init(ServletConfig config) throws ServletException {
super.init(config);
try {
GenericDataSource ds = new GenericDataSource();
ds.setDriverClass(getInitParameter("driverClass"));
ds.setUrl(getInitParameter("jdbcURL"));
ds.setMinCount(Integer.parseInt(getInitParameter("minCount")));
ds.setMaxCount(Integer.parseInt(getInitParameter("maxCount")));
ds.setAutoCommit(false);
ds.setUser(getInitParameter("user"));
ds.setPassword(getInitParameter("password"));
ds.open();
ConnectionPool.init(ds);
} catch (SQLException e) {
e.printStackTrace();
System.out.println("error");
}
}
}
/**
* <p>
* 关闭数据库连接
*
*/
public static void closedb(Connection conn) throws Exception {
try {
if (conn != null)
conn.close();
} catch (Exception e) {
e.printStackTrace();
}
} /**
* <p>
* 数据库操作,事务回滚
*
*/ public static void rollbackdb(Connection conn) throws Exception {
try {
conn.rollback();
} catch (Exception e) {
e.printStackTrace();
} }}
public class KehuDJDB implements KehuDJDAO {
PreparedStatement ps = null; ResultSet rs = null; private ConnectionPool pool; public KehuDJDB() {
pool = ConnectionPool.getInstance();
}
在构造函数中取得pool然后方法如下::/**
* <p>
* 对数据库进行操作,显示某段时间内记帐或免费的人员
*
*
*/
public Vector mouTyperuzhurenyuan(String riqi1,String riqi2,String i) throws Exception {
Connection conn = null;
Vector mouTyperuzhurenyuan = new Vector();
String selectSql = "select name,sex,age,sfzjh,address,gzdw,rzsj,lksj,huafei from kehuDJ where jztag = 2 "
+ "and rzsj >= '"+riqi1+"' and rzsj <= '"+riqi2+"' and bideType = "+i;
try {
conn = pool.getConnection();
Statement stmt = conn.createStatement();
rs = stmt.executeQuery(selectSql);
while (rs.next()) {
KehuDJ kj = new KehuDJ();
kj.setName(rs.getString("name"));
kj.setSex(rs.getString("sex"));
kj.setAge(rs.getInt("age"));
kj.setSfzjh(rs.getInt("sfzjh"));
kj.setAddress(rs.getString("address"));
kj.setGzdw(rs.getString("gzdw"));
kj.setRzsj(rs.getString("rzsj"));
kj.setLksj(rs.getString("lksj"));
kj.setJine(rs.getFloat("huafei")); mouTyperuzhurenyuan.add(kj); } } catch (Exception e) {
e.printStackTrace();
} finally {
BaseDB.closedb(conn);
} return mouTyperuzhurenyuan;
}