how to overcome out of memory exception
Hi all,
My Application is the one which is accessed by so many people via intranet...
I have used connection Pooling concept to esatblish connection to Databse.
i have created a datsource via tomcat admin page and set it as a attribute in ServletContext through a listener class.
Then fetching that datasource getServletContext().getAttribute method.
Using that datasource fetched I am opening connection in starting of try block in every page and closing the connection in Finally block.
I am using Tomcat with MySql .
I am getting Out ofMemory error often, after which i need to restart the tomcat server to get rid of this error and make my application work normally......
Can any body help me and suggest me how to overcome this error...?
<%@ page import="java.io.*,java.sql.*,javax.sql.*,javax.servlet.jsp.JspPage,java.net.*,java.util.*,com.pts.database.DBCon" %>
<%@ page import="com.pts.beans.ProjMeetFormat,com.pts.beans.BProjStatus" errorPage="../../InterError.jsp"%>
<jsp:useBean id="projs" class="com.pts.beans.BProjStatus" scope="page" />
<%!
DataSource datasource=null;
public void jspInit()
{
try
{
System.out.println("get attribute of DBCPool: "+getServletContext().getAttribute("DBCPool"));
datasource =(DataSource) getServletContext().getAttribute("DBCPool");
System.out.println("datasource inside ViewStatsReport.jsp new : " + datasource);
}
catch(Exception e)
{
System.out.println("exception in getting datasource in ViewStatsReport.jsp" + e.getMessage());
}
}
%>
<%
Connection con=null;
Statement stmt1=null;
ResultSet rs=null;
ResultSet rs1=null;
ResultSet rs2=null;
ResultSet rs3=null;
ResultSet rs4=null;
try
{
con=datasource.getConnection();
System.out.println("after static connection is new" + con);
stmt1=con.createStatement();
String sql1,sql,sql2,sql3,proc="",stat="",ad="",prosid="";
int count1=0,count2=0,total=0,i,chapnum=0,j;
String[] cids= new String[100];
String duedateDup="",duedate="",projType="",projName="",ap=null;boolean meet=false,heading=true;
// code for checking TE/TL
String uname=(String)session.getAttribute("uname");
System.out.println("inside select client is " + uname);
String check_TL="";
String trim_projid="";
String check_TE="";
int TL_indexof=0;
int TL_strlength=0;
boolean check_TL_flag=false;
String strPrID = request.getParameter("projID");
TL_indexof=strPrID.indexOf("P");
TL_strlength=strPrID.length();
trim_projid=strPrID.substring(TL_indexof,TL_strlength);
rs=stmt1.executeQuery("select u.userid from user_det u,proj_det p,proj_handler ph where p.projid='"+trim_projid+"' and u.username='"+uname+"' and u.userid=ph.userid and p.projid=ph.projid ");
if(rs.next())
{
check_TL_flag=true;
}
else{
rs=stmt1.executeQuery("select * from user_det where username='"+uname+"' and designame in('Team Manager','Production','Management')");
if(rs.next())
{
check_TL_flag=true;
}
}
System.out.println("check_TL_flag"+check_TL_flag);
if(!check_TL_flag)
{
RequestDispatcher rd = request.getRequestDispatcher("ViewStatusReportothers.jsp");
rd.forward(request,response);
}
// end of TE/TL code
Hashtable chapNames = new Hashtable();
Hashtable chapIDS = new Hashtable();
Hashtable prosNames = new Hashtable();
if(strPrID.startsWith("R6"))
{
RequestDispatcher rd = request.getRequestDispatcher("FinalReport.jsp");
rd.forward(request,response);
}
String cidqry="";
rs = stmt1.executeQuery("select chapID,chapName from chapstat where projID='"+strPrID+"' order by chapid");
for(i=0; rs.next(); i++)
{
cids=rs.getString("chapID");
chapIDS.put(""+i,""+rs.getString("chapID"));
chapNames.put(""+i,rs.getString("chapName"));
}
chapnum=i;
sql = "select projname,projType,duedateDup,duedate,activeprocess from proj_det where projID='"+strPrID+"'";
rs = stmt1.executeQuery(sql);
while(rs.next())
{
projName=rs.getString("projName");
projType = rs.getString("projType");
duedateDup = rs.getString("duedateDup");
duedate = rs.getString("duedate");
ap=rs.getString("activeprocess");
}
sql1="select distinct p.prosid,pd.process from projstat p,process_det pd where projID='"+strPrID+"' && status != 'NA' && p.prosid=pd.prosid && p.prosid not in ('PR19','PR20','PR21') order by pd.porder";
rs2=stmt1.executeQuery(sql1);
and etc.........
This is a piece of code in the page which is visited most often by the endusers. To be frank, in all the jsp pages, I am using Hashtables like this to store results of query . After storing in Hashtables, i am using the vvalues present in Hashtable for any calculations or display. First part of my code in any jsp will be like fetching from database and storing in Hashtables. In the remaining part of my code. I will be fetching stored values in hashtable and using it for display and calculation.
can u suggest any remedy now for my problem...?