请问在类似如下的程序里,是否需要在最后关闭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);
}
}