我的程序结构大体如下:public class A{
ConnManager manager;
Connection conn;
PreparedStatement stmt;
ResultSet rs;
public A(){
conn_manager = new ConnManager();
conn = manager.getConnection("jndi");
}
public Vector getA1(){
String sql = "";
try {
stmt = conn.prepareStatement(sql);
rs = stmt.executeQuery(sql);
while (rs.next()) {
......
}
} catch (Exception e) {
System.out.println(e.toString());
} finally {
try {
rs.close();
stmt.close();
} catch (Exception e) {
}
}
}
public void close() {
try{
if(conn != null){
conn.close();
}
} catch (Exception e) {}
}
}然后我在另外一个程序里调用如下:public class B{
public void execute(){
A a = new A();
a.getA1();
a.close();
}
}
按上述方法有可能会有数据库连接不能释放的吗?
ConnManager manager;
Connection conn;
PreparedStatement stmt;
ResultSet rs;
public A(){
conn_manager = new ConnManager();
conn = manager.getConnection("jndi");
}
public Vector getA1(){
String sql = "";
try {
stmt = conn.prepareStatement(sql);
rs = stmt.executeQuery(sql);
while (rs.next()) {
......
}
} catch (Exception e) {
System.out.println(e.toString());
} finally {
try {
rs.close();
stmt.close();
} catch (Exception e) {
}
}
}
public void close() {
try{
if(conn != null){
conn.close();
}
} catch (Exception e) {}
}
}然后我在另外一个程序里调用如下:public class B{
public void execute(){
A a = new A();
a.getA1();
a.close();
}
}
按上述方法有可能会有数据库连接不能释放的吗?
finally {
try {
rs.close();
stmt.close();
} catch (Exception e) {
}
}
改成finally {
try {
try{
rs.close();
}catch(Exception a){
}
stmt.close();
} catch (Exception e) {
}
}
不过感觉这么用很变扭
rs.close();
}catch(Exception a){
}的话
rs.close()出异常的时候stmt.close()也不会被调用了