JiveJdon Community Forums
在线363人   首页   主题表   培训咨询   标签   精华   查搜   注册    登陆 RSS
首页 » 论坛 » 开源JdonFramework及其应用案例论坛
???en_US.forumThreadPrev.name??? 上一主题
  Go back to the topic 返回本主题   Go back to the topic listing返回主题列表
???en_US.forumThreadNext.name??? 下一主题
Go 总共有 0 回复 / 1
 发表新帖子   回复该主题贴
softxyz

悄悄话
发表文章: 13
注册时间: 2004年06月04日 09:04
Struts程序里应该如何有效释放资源? 2004年09月20日 10:11 到本帖网址 加入本帖到收藏夹 发送到手机 回复该主题
标签列表
请问在类似如下的程序里,是否需要在最后关闭dbo对象和其他如String、ResultSet对象以释放资源?
资源释放是在retrun(actionMapping.findForward("success"));之前,还是在finally中?
另外在if(!rs.next())中因为需要错误跳转,那么应该如何释放资源?


package com.zz.web.action;

import com.zz.web.actionform.*;
import com.zz.web.db.*;
import org.apache.struts.action.*;
import java.sql.*;
import javax.servlet.http.*;

public class userLogonAction extends Action {

public ActionForward execute(ActionMapping actionMapping, ActionForm actionForm,
HttpServletRequest request,
HttpServletResponse response) {
ActionErrors errors = new ActionErrors();
HttpSession session = request.getSession();

try
{
DBOperate dbo = new DBOperate();

String username = request.getParameter("username");
String userpass = request.getParameter(
"userpass");

String sqlstr=
"select * from gy_user where username=? and userpass=?";
dbo.prepareStatement(sqlstr);
dbo.setString(1,username);
dbo.setString(2,userpass);
ResultSet rs = dbo.executeQuery();
if(!rs.next())
{
dbo.close();
errors.add(
"error.username.required",
new ActionError(
"error.username.wrong"));
saveErrors(request, errors);
return actionMapping.findForward(
"error");
}
session.setAttribute(
"userid",rs.getString("userid"));
session.setAttribute(
"username",rs.getString("username"));
session.setAttribute(
"groupid",rs.getString("groupid"));
int userid = rs.getInt(
"userid");
String ip = request.getRemoteAddr();
dbo.executeUpdate(
"update gy_user set lastlogtime=sysdate,lastlogip='"+ip+"' where userid="+userid);
rs.close();
username=null;
userpass=null;
sqlstr=null;
return ( actionMapping.findForward (
"success" ) ) ;
}
catch (Exception e)
{
e.printStackTrace();
throw new RuntimeException(e);
}
finally{

}
}
}



其中dbo.close()的代码如下


public void close(){
try{
if(stmt!=null){
stmt.close();
stmt = null;
}
if(prepstmt!=null){
prepstmt.close();
prepstmt = null;
}
conn.close();
conn = null;
}catch(Exception e){
System.err.println(
"close err: " + e);
}
}
这个主题有 0 回复 / 1Go
???en_US.forumThreadPrev.name??? 上一主题
  Go back to the topic 返回本主题   Go back to the topic listing返回主题列表    返回页首返回页首
???en_US.forumThreadNext.name??? 下一主题
热点TAG: AOP cache 缓存 DDD EJB 集群 设计模式 Hibernate IOC JiveJdon OO RBAC Seam Spring Struts
正在读取,请等待...
google yahoo 新浪ViVi 365Key网摘 天极网摘 CSDN网摘 添加到百度搜藏 POCO网摘 博采网摘
查询本论坛内 回复超过的热门帖子
     回复该主题贴
标题
 
粗体 斜体 下划线 插入图片 插入代码 插入url链接 插入附件
内容
  发贴前查询 标签列表勿重复发表问题

RSS 手机阅读 add to google add to yahoo
解惑之道在J道 ,打造中国最具影响力的的企业软件社区
OpenSource JIVEJDON v3.0 Powered by JdonFramework Code © 2002-08 jdon.com
anti spam