Topic: resin下数据库lock为什么不能释放?

  Print this page

1.resin下数据库lock为什么不能释放? Copy to clipboard
Posted by: woodworm
Posted on: 2003-08-05 13:46

小弟在resin下作jsp。经常用着用着就速度特别慢,不知道什么原因。不过我发现数据库在访问后被lock了。但是有时即使关掉resin也不能释放。

请问谁知道什么原因?
下面是我得数据库连接得代码:特别是用executeQuery1,带参数得连接数据库后,根本不会释放.请问各位得情况如何?
public ResultSet executeQuery(String sql){
rs=null;
try{
conn=DriverManager.getConnection(sConnStr);
Statement stmt=conn.createStatement();
rs=stmt.executeQuery(sql);
}catch(SQLException ex){
System.err.println("aq.executeQuery: "+ex.getMessage());
}

return rs;
}
public ResultSet executeQuery1(String sql){
rs=null;
try{
conn=DriverManager.getConnection(sConnStr);
Statement stmt=conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
rs=stmt.executeQuery(sql);
}catch(SQLException ex){
System.err.println("aq.executeQuery: "+ex.getMessage());
}

return rs;
}
public void clearConn(){
if (conn!=null) {
try {
if (conn != null)
conn.close();
conn=null;
} catch (SQLException e) {
e.printStackTrace();
}

}
}

2.Re:resin下数据库lock为什么不能释放? [Re: woodworm] Copy to clipboard
Posted by: 256456
Posted on: 2003-08-06 09:50

因为你的每个查询方法中都调用了Connection
所以在方法中都要调用 Connection.close();

try{
.......
}catch(SQLException se){
}finally{
try{
conn.close();
}catch(SQLException ee){}
}

3.Re:resin下数据库lock为什么不能释放? [Re: woodworm] Copy to clipboard
Posted by: woodworm
Posted on: 2003-08-07 23:07

请问,如果在finally里面加conn.close(),那么resultset是不是也会被关闭。
我是在jsp页面的最后,当rs都关闭后调用bean.clearconn()来调用close()的。但是不起作用。

4.Re:resin下数据库lock为什么不能释放? [Re: woodworm] Copy to clipboard
Posted by: fredfred
Posted on: 2003-08-18 08:26

my solution:

Write a totally reusable JDBC wrapper Class just for data reading, it is self-managed, you do not need to worry about any connection problem(but you need to download rowset putting into your classpath):

package com.bestway.framework;

import javax.sql.*;
import java.sql.*;
import java.io.*;
//import java.util.*;
import sun.jdbc.rowset.CachedRowSet;

/**
* @author Fred Wang
* @version 0.1
* @since 2003-2004
*
* This class is used to select data from DB. User gives sql satement and
* gets a resultset. It manages the connection automatically.
*/
public class DBReader implements Serializable {

/**
* DataSource.
*/
private transient DataSource ds=null;

//constructor.
public DBReader(DataSource ds) {
this.ds=ds;
}

public CachedRowSet getCachedRowSet(String sql) throws SQLException{

CachedRowSet crs=new CachedRowSet();
Connection cnn=ds.getConnection();
crs.setCommand(sql);
crs.execute(cnn);
cnn.close();
return crs;
}

/**
* get the ds.
*@return DataSourse
*/
public DataSource getDataSource(){
return ds;
}

/**
* set DataSource.
* @param ds
*/
public void setDataSource(DataSource ds){
this.ds=ds;
}
}

I create an instance and put it into session whenever a new session created.in the following pocess, you just use it like this:

String sql="select * from courses where isPublished='T' and categoryId="+categoryId;
dbReader.getCachedRowSet(sql);

that's all.

can I get one point?


   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923