请教关于数据库的连接的问题,有点难度哦!(请教过公司几个高手都没有解决) 用户登陆时与数据库用户管理表取得连接,用户信息取得之后,关闭连接(rs.close;stmt.close,conn.close)都已经执行,然而进入数据库发现该连接却并没有断开.说明:第一次登陆时,不存在这个问题,发生第二次以后请教各位高手,其原因为何? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 yxhzj(余华[学习J2EE中]) ( ) 信誉:100 2006-06-12 10:07:00 得分: 0 一大堆的话,没一句代码,神仙也救不了你 支持,呵呵,应该是你代码逻辑的问题。 你可能用了连接池,如果是这样,conn.close不是真正关闭连接,而是把连接返回到连接池 1.获得连接Connection conn = this.getConnection(pInfo); ResultEntity[] ents = new ResultEntity().executeQuery(sbSql.toString(),conn);2.executeQuery()方法public ResultEntity[] executeQuery(String sql, Connection conn) throws Exception{ boolean disConnection = false; Statement statement = null; ResultSet rs = null; try{ // Logger.getInstance().info("▼▼▼▼▼ SQL STATEMENT ▼▼▼▼▼");// Logger.getInstance().info("■ SQL:" + sql);// Logger.getInstance().info("▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲"); if( conn == null ){ disConnection = true; conn = DataBaseManager.getConnection(); } statement = conn.createStatement(); rs = DataBaseManager.getResultSet(sql,conn,statement); if(rs == null){ return null; } /* 項目情報の取得 */ ResultSetMetaData rsMeta = rs.getMetaData(); /* 列数を格納 */ columncount = rsMeta.getColumnCount(); /* 各TreeMapを初期化 */ this.colNameType = new HashMap(); this.colIndexType = new HashMap(); this.colIndexName = new HashMap(); for(int i=1; i<columncount+1; i++){ this.colNameType.put(rsMeta.getColumnName(i).toUpperCase(), new Integer(rsMeta.getColumnType(i))); this.colIndexType.put(new Integer(i), new Integer(rsMeta.getColumnType(i))); this.colIndexName.put(new Integer(i), rsMeta.getColumnName(i).toUpperCase()); } recordcount = 0; Vector vec = new Vector(); while(rs.next()){ ResultEntity ent = new ResultEntity(); ent.rowInfo = new HashMap(); recordcount++; ent.setColIndexName(this.colIndexName); ent.setColIndexType(this.colIndexType); ent.setColNameType(this.colNameType); for(int i=1; i<columncount+1; i++){ ent.rowInfo.put(rsMeta.getColumnName(i).toUpperCase(), rs.getObject(i)); } vec.add(ent); } /* Vectorに格納した結果をResultEntity[]で返す。 */ ResultEntity[] ents = new ResultEntity[vec.size()]; vec.toArray(ents); // 2006.02.07削除@kanazumi// Logger.getInstance().info("レコード取得件数:" + Integer.toString(ents.length)); return ents; }catch(Exception e){ conn.rollback(); Logger.getInstance().error("▼▼▼▼▼ SQL STATEMENT ERROR ▼▼▼▼▼"); Logger.getInstance().error(sql); Logger.getInstance().error("▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲"); throw e; } finally { if (rs != null){ rs.close(); rs = null; } try{ if( statement != null ){ statement.close(); statement = null; } }catch(Exception e){ } finally { if( disConnection ){ conn.close(); conn = null; } } } }3.获得连接的方法 public static Connection getConnection() throws Exception{ try{ Connection conn = null; if( ds != null ){ conn = ds.getConnection(); return conn; } DataBaseManager.setPropertyData(); String JDNIname = new String(); String WAS_CLASS = new String(); try{ JDNIname = PropertyClass.getProperty("JDNIname"); WAS_CLASS = PropertyClass.getProperty("WAS_CLASS"); Logger.getInstance().info("JDNIname = " + JDNIname); Logger.getInstance().info("WAS_CLASS = " + WAS_CLASS); }catch(Exception e){ Logger.getInstance().error("▼▼▼▼▼ JDNIname、WAS_CLASSの取得エラー ▼▼▼▼▼"); Logger.getInstance().error(e.toString()); Logger.getInstance().error("▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲"); throw e; } Properties parms; Context ctx; for(int intLoop=5;intLoop>0;intLoop--){ try{ parms = new Properties(); parms.setProperty( Context.INITIAL_CONTEXT_FACTORY, WAS_CLASS ); ctx = new InitialContext( parms ); ds = ( DataSource )ctx.lookup( "java:comp/env/" + JDNIname ); conn = ds.getConnection(); return conn; }catch(Exception e){ Logger.getInstance().error("▼▼▼▼▼ データベース接続エラー (その2)▼▼▼▼▼"); Logger.getInstance().error(e.toString()); Logger.getInstance().error("▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲"); throw e; } } return null; }catch(StaleConnectionException e){ Logger.getInstance().error("StaleConnectionExceptionが発生しました。"); return getConnection(); } }以上是相干代码,请帮忙分析分析 用newxy试试,特点是不用写代码实现绝大部分工作。newxy技术范例:http://www.newxy.net/zh_cn/samples/index.jspnewxy技术主页:http://www.newxy.net 很明显,你使用了连接池。使用连接池的目的就是为了防止连接被频繁的创建、关闭,而将所有连接统一管理,这样做本身就能减轻服务器的压力。你如果感觉连接过多的话,可以在连接池设置上设置它的max connection。不明白你既然使用了连接池为什么还有费力问怎样关闭连接。连接池的目的就是为了不让你自己创建、关闭连接 ( DataSource )ctx.lookup( "java:comp/env/" + JDNIname );conn = ds.getConnection();说明你是使用的连接池 因为在数据库中有个系统同时使用了该数据表,所以DBA 希望能够在用户登陆信息确定后,就断开连接,以节约数据库的资源.使用了连接池的技术后是不是就不能够有针对性的对具体的连接实施操作? SORRY, 是希望关闭对于其中一个数据表的连接,而不是对数据库整个的连接. 是希望关闭对于其中一个数据表的连接,而不是对数据库整个的连接.----------------------------------------------------------- 那是不可能的,连接就是连的数据库,con对应的是数据库。 你可以把初始连结数改小一点,那样平时占用的资源就会少一点。 已经在finally作了断开的动作,代码如下,可是没有效果. } finally { if (rs != null){ rs.close(); rs = null; } try{ if( statement != null ){ statement.close(); statement = null; } }catch(Exception e){ } finally { if( disConnection ){ conn.close(); conn = null; } } } ----------------------------------------------------------- 那是不可能的,连接就是连的数据库,con对应的是数据库。 你可以把初始连结数改小一点,那样平时占用的资源就会少一点。-----------------------------------------------------------如果con对应的是数据库的话,那么,ResultSet,和Statement 对应的是不是就是具体的数据表了? 借光:路过的各位,帮忙看看这几个帖子,看怎么解决;)谢谢啦!关于客户端页面从数据库中读取大量图片的问题http://community.csdn.net/Expert/TopicView3.asp?id=4816106乱码还原问题!http://community.csdn.net/Expert/TopicView3.asp?id=4813786关于javabean反射代理的问题http://community.csdn.net/Expert/TopicView3.asp?id=4813935大家帮忙顶顶! 谁能给个javadoc中用到的标准doclet的源码下载地址? 如何判断页面是否存在某特定词,并如何将特定词所在行获取? 这个错误是什么? Connsection reset可能是什么原因引起的 b/s系统是否可以控制外设? 求解释,求大神解释 如何一次性的永久设置tomcat的环境? 如何在网页中检测浏览器的JRE的版本?用javascript可以吗? 我的tomcat-users内容如下,为什么用户tomcat进不去? 查询时间的函数 关于下载的问题!!!!!!!!!!!! 请教大家一个问题??
一大堆的话,没一句代码,神仙也救不了你
支持,呵呵,应该是你代码逻辑的问题。
Connection conn = this.getConnection(pInfo);
ResultEntity[] ents = new ResultEntity().executeQuery(sbSql.toString(),conn);
2.executeQuery()方法
public ResultEntity[] executeQuery(String sql, Connection conn) throws Exception{
boolean disConnection = false;
Statement statement = null;
ResultSet rs = null;
try{
// Logger.getInstance().info("▼▼▼▼▼ SQL STATEMENT ▼▼▼▼▼");
// Logger.getInstance().info("■ SQL:" + sql);
// Logger.getInstance().info("▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲"); if( conn == null ){
disConnection = true;
conn = DataBaseManager.getConnection();
}
statement = conn.createStatement();
rs = DataBaseManager.getResultSet(sql,conn,statement);
if(rs == null){
return null;
}
/* 項目情報の取得 */
ResultSetMetaData rsMeta = rs.getMetaData();
/* 列数を格納 */
columncount = rsMeta.getColumnCount();
/* 各TreeMapを初期化 */
this.colNameType = new HashMap();
this.colIndexType = new HashMap();
this.colIndexName = new HashMap();
for(int i=1; i<columncount+1; i++){
this.colNameType.put(rsMeta.getColumnName(i).toUpperCase(), new Integer(rsMeta.getColumnType(i)));
this.colIndexType.put(new Integer(i), new Integer(rsMeta.getColumnType(i)));
this.colIndexName.put(new Integer(i), rsMeta.getColumnName(i).toUpperCase());
}
recordcount = 0;
Vector vec = new Vector();
while(rs.next()){
ResultEntity ent = new ResultEntity();
ent.rowInfo = new HashMap();
recordcount++;
ent.setColIndexName(this.colIndexName);
ent.setColIndexType(this.colIndexType);
ent.setColNameType(this.colNameType);
for(int i=1; i<columncount+1; i++){
ent.rowInfo.put(rsMeta.getColumnName(i).toUpperCase(), rs.getObject(i));
}
vec.add(ent);
}
/* Vectorに格納した結果をResultEntity[]で返す。 */
ResultEntity[] ents = new ResultEntity[vec.size()];
vec.toArray(ents);
// 2006.02.07削除@kanazumi
// Logger.getInstance().info("レコード取得件数:" + Integer.toString(ents.length));
return ents;
}catch(Exception e){
conn.rollback();
Logger.getInstance().error("▼▼▼▼▼ SQL STATEMENT ERROR ▼▼▼▼▼");
Logger.getInstance().error(sql);
Logger.getInstance().error("▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲");
throw e;
} finally {
if (rs != null){
rs.close();
rs = null;
}
try{
if( statement != null ){
statement.close();
statement = null;
}
}catch(Exception e){
} finally {
if( disConnection ){
conn.close();
conn = null;
}
}
}
}3.获得连接的方法
public static Connection getConnection() throws Exception{
try{
Connection conn = null;
if( ds != null ){
conn = ds.getConnection();
return conn;
}
DataBaseManager.setPropertyData();
String JDNIname = new String();
String WAS_CLASS = new String();
try{
JDNIname = PropertyClass.getProperty("JDNIname");
WAS_CLASS = PropertyClass.getProperty("WAS_CLASS");
Logger.getInstance().info("JDNIname = " + JDNIname);
Logger.getInstance().info("WAS_CLASS = " + WAS_CLASS);
}catch(Exception e){
Logger.getInstance().error("▼▼▼▼▼ JDNIname、WAS_CLASSの取得エラー ▼▼▼▼▼");
Logger.getInstance().error(e.toString());
Logger.getInstance().error("▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲");
throw e;
}
Properties parms;
Context ctx;
for(int intLoop=5;intLoop>0;intLoop--){
try{
parms = new Properties();
parms.setProperty( Context.INITIAL_CONTEXT_FACTORY, WAS_CLASS );
ctx = new InitialContext( parms );
ds = ( DataSource )ctx.lookup( "java:comp/env/" + JDNIname );
conn = ds.getConnection();
return conn;
}catch(Exception e){
Logger.getInstance().error("▼▼▼▼▼ データベース接続エラー (その2)▼▼▼▼▼");
Logger.getInstance().error(e.toString());
Logger.getInstance().error("▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲");
throw e;
}
}
return null;
}catch(StaleConnectionException e){
Logger.getInstance().error("StaleConnectionExceptionが発生しました。");
return getConnection();
}
}以上是相干代码,请帮忙分析分析
newxy技术范例:http://www.newxy.net/zh_cn/samples/index.jsp
newxy技术主页:http://www.newxy.net
不明白你既然使用了连接池为什么还有费力问怎样关闭连接。连接池的目的就是为了不让你自己创建、关闭连接
conn = ds.getConnection();说明你是使用的连接池
连接,以节约数据库的资源.
使用了连接池的技术后是不是就不能够有针对性的对具体的连接实施操作?
-----------------------------------------------------------
那是不可能的,连接就是连的数据库,con对应的是数据库。
你可以把初始连结数改小一点,那样平时占用的资源就会少一点。
} finally {
if (rs != null){
rs.close();
rs = null;
}
try{
if( statement != null ){
statement.close();
statement = null;
}
}catch(Exception e){
} finally {
if( disConnection ){
conn.close();
conn = null;
}
}
}
那是不可能的,连接就是连的数据库,con对应的是数据库。
你可以把初始连结数改小一点,那样平时占用的资源就会少一点。-----------------------------------------------------------
如果con对应的是数据库的话,那么,ResultSet,和Statement 对应的是不是就是具体的数据表了?
谢谢啦!关于客户端页面从数据库中读取大量图片的问题
http://community.csdn.net/Expert/TopicView3.asp?id=4816106乱码还原问题!
http://community.csdn.net/Expert/TopicView3.asp?id=4813786关于javabean反射代理的问题
http://community.csdn.net/Expert/TopicView3.asp?id=4813935大家帮忙顶顶!