想把问题说清楚,可能要用到网址,希望版主不要删除!
我一个新站,里面的内容不多,访问量也不大,但运行一天就内存溢出不知道是怎么回事,特来求助csdn想高手们帮下忙!
一般只访问了网站的主页和http://www.house08.cn右侧的少许文章。我猜可能是以下代码有问题,但也不知道是哪句,特贴出代码,请大家帮解决下。
自定义标签:ShowArticle.javapackage com.fwcz.taglib;
import java.util.List;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.TagSupport;import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;import com.fwcz.model.Article;
import com.fwcz.service.ArticleDaoImpl;
import com.fwcz.service.ArticleService;
import com.fwcz.util.PageHelp;
public class ShowArticle extends TagSupport {
private int categoryId;
private int page;
private static Log log = LogFactory.getLog(ShowArticle.class);
private ArticleDaoImpl articleDaoImpl;
private static final long serialVersionUID = 1L;
public int doStartTag()throws JspException{
return EVAL_BODY_INCLUDE;
}

public ArticleDaoImpl getArticleDaoImpl() {
return articleDaoImpl;
} public void setArticleDaoImpl(ArticleDaoImpl articleDaoImpl) {
this.articleDaoImpl = articleDaoImpl;
} public int doEndTag()throws JspException{
int defaultSize=25;
System.out.println("PaginationTag中的page为"+page);
String url="/admin/archiveManager.jsp?page="+page+"&id="+categoryId;
int totals=ArticleService.getCount(categoryId);
BeanFactory factory=new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
ArticleDaoImpl articleDaoImpl =(ArticleDaoImpl)factory.getBean("articleDaoImpl");
List<Article> articles=(List<Article>) articleDaoImpl.pagination(categoryId, page,defaultSize);
pageContext.setAttribute("articles", articles);

String navigate=PageHelp.navigateYhaoo(totals, page, url, 25);
pageContext.setAttribute("navigate", navigate);
pageContext.setAttribute("page",page);//当前页
return EVAL_PAGE;
}
public int getCategoryId() {
return categoryId;
}

public void setCategoryId(int categoryId) {
this.categoryId = categoryId;
} public int getPage() {
return page;
} public void setPage(int page) {
this.page = page;
}
}数据库关闭:DButil.java着重看下 close()方法
package com.fwcz.util;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
public class DButil {
private static Log log = LogFactory.getLog(DButil.class);
public static void close(ResultSet rs,Connection con,Statement stmt){
try {
rs.close();
stmt.close();
con.close();
} catch (SQLException e) {
e.printStackTrace();
}
}

public static void close(Connection con,PreparedStatement pstm){
try {
pstm.close();
con.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
数据库操作类:ArticleDaoImpl.java着重看下pagination(),和public Object query(Serializable id)方法
package com.fwcz.service;import java.io.Serializable;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;import org.springframework.orm.ibatis.support.SqlMapClientDaoSupport;import com.fwcz.dao.Dao;
import com.fwcz.model.Article;
import com.fwcz.util.DBcon;
import com.fwcz.util.DButil;
import com.fwcz.util.FileUtils;
import com.fwcz.util.Globals;public class ArticleDaoImpl extends SqlMapClientDaoSupport implements Dao { @Override
public void create(Object t) {
//增加文章
getSqlMapClientTemplate().insert("createArticle", t);
} @Override
public void delete(Object t) {
// TODO Auto-generated method stub }
public List<Article> list(int cId) {
String sql="select * from Article where Article_Category_Id="+cId;
return null;
}
@Override
//读文章详细信息
public Object query(Serializable id) {
Article article=new Article();
String sql="select * from Article where article_Id="+id;
Connection con=DBcon.getConnection();
Statement st=null;
ResultSet rs=null;
try {
st = con.createStatement();
rs=st.executeQuery(sql);
while(rs.next()){
article.setArticleId(rs.getInt("article_Id"));
article.setTitle(rs.getString("title"));
article.setArticleCategoryId(rs.getInt("Article_Category_Id"));
article.setAuthor(rs.getString("author"));
article.setRelateTxt(rs.getString("relate_Txt"));
article.setContent(FileUtils.readFile(Globals.path+article.getRelateTxt()));//读取内容
article.setReleaseDate(rs.getString("release_Date"));
article.setDescript(rs.getString("descript"));
article.setKeyWord(rs.getString("keyWord"));
article.setVisitTota(rs.getInt("Visit_Tota"));
article.setOrigin(rs.getString("origin"));
}

} catch (SQLException e) {
e.printStackTrace();
}finally{
DButil.close(rs, con, st);
}
return article;
}
//分页
public List<Article> pagination(int CategoryId,int page,int defaultSize) {
String sql="select * from Article inner join Article_Category on Article.Article_Category_Id=Article_Category.Article_Category_Id where Article.Article_Category_Id="+CategoryId+" order by Release_Date DESC";
List<Article> articles=new ArrayList<Article>();
Connection con=DBcon.getConnection();
Statement st;
try {
st = con.createStatement();
ResultSet rs=st.executeQuery(sql);
while(rs.next()){
Article article=new Article();
article.setArticleId(rs.getInt("article_Id"));
article.setTitle(rs.getString("title"));
article.setArticleCategoryId(rs.getInt("Article_Category_Id"));
article.setAuthor(rs.getString("author"));
articles.add(article);
}
DButil.close(rs, con, st);
} catch (SQLException e) {
e.printStackTrace();
}finally{

}
return articles;
}
}

解决方案 »

  1.   

    //分页public List<Article> pagination(int CategoryId,int page,int defaultSize) {
            String sql="select * from Article inner join Article_Category on Article.Article_Category_Id=Article_Category.Article_Category_Id where Article.Article_Category_Id="+CategoryId+" order by Release_Date DESC";
            List<Article> articles=new ArrayList<Article>();
            Connection con=DBcon.getConnection();
            Statement st;
            try {
                st = con.createStatement();
                ResultSet rs=st.executeQuery(sql);
                while(rs.next()){
                    Article article=new Article();
                    article.setArticleId(rs.getInt("article_Id"));
                    article.setTitle(rs.getString("title"));
                    article.setArticleCategoryId(rs.getInt("Article_Category_Id"));
                    article.setAuthor(rs.getString("author"));
                        articles.add(article);
                }
                DButil.close(rs, con, st);
            } catch (SQLException e) {
                e.printStackTrace();
            }finally{
                
            }
            return articles;
        }
    }
    吧   DButil.close(rs, con, st);关闭语句放在finally{
                
            }
    里面去,试试
      

  2.   

    嗯,楼上说的是 
            } catch (SQLException e) {
                e.printStackTrace();
            }finally{
                  DButil.close(rs, con, st);        }
            return articles;
      

  3.   

     try {
                rs.close();
                stmt.close();
                con.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
    一般会分开放的try{
      if(rs != null)
      rs.close();
      }
    }catch...try{
      if(stmt!= null)
      stmt.close();
      }
    }catch...
    try{
      if(con != null)
      con.close();
      }
    }catch...
      

  4.   

    我还有两点:1) 如果doEndTag()中的ClassPathXmlApplicationContext可以不用每次创建的话,最好别每次创建。
    2) ArticleDaoImpl中的pagination实现不合理,每次应该只new本页Article。修改后的pagination如下(如果没有理解错你的意思的话):public List<Article> pagination(int CategoryId,int page,int defaultSize) {
        String sql="select * from Article inner join Article_Category on Article.Article_Category_Id=Article_Category.Article_Category_Id where Article.Article_Category_Id="+CategoryId+" order by Release_Date DESC";
        List<Article> articles=new ArrayList<Article>();
        Connection con=DBcon.getConnection();
        Statement st;
        try {
            st = con.createStatement();
            ResultSet rs=st.executeQuery(sql);
            int skippedRecords = ( page - 1 )* defaultSize;
            while( rs.next() && skippedRecords > 0 ) {
             skippedRecords++;
    }

    int recordsToFetch = defaultSize;

    while( rs.next() && recordsToFetch > 0 ) {
    recordsToFetch--;
    Article article=new Article();
                article.setArticleId(rs.getInt("article_Id"));
                article.setTitle(rs.getString("title"));
                article.setArticleCategoryId(rs.getInt("Article_Category_Id"));
                article.setAuthor(rs.getString("author"));
                articles.add(article);
    }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally{
            DButil.close(rs, con, st);
        }
        return articles;
    }
    }
      

  5.   

    pagination的实现有点错误,改正后的pagination如下:public List<Article> pagination(int CategoryId,int page,int defaultSize) {
        String sql="select * from Article inner join Article_Category on Article.Article_Category_Id=Article_Category.Article_Category_Id where Article.Article_Category_Id="+CategoryId+" order by Release_Date DESC";
        List<Article> articles=new ArrayList<Article>();
        Connection con=DBcon.getConnection();
        Statement st;
        try {
            st = con.createStatement();
            ResultSet rs=st.executeQuery(sql);
            int skippedRecords = ( page - 1 )* defaultSize;
            while( rs.next() && skippedRecords > 0 ) {
             skippedRecords--;
    }

    int recordsToFetch = defaultSize;

    while( rs.next() && recordsToFetch > 0 ) {
    recordsToFetch--;
    Article article=new Article();
                article.setArticleId(rs.getInt("article_Id"));
                article.setTitle(rs.getString("title"));
                article.setArticleCategoryId(rs.getInt("Article_Category_Id"));
                article.setAuthor(rs.getString("author"));
                articles.add(article);
    }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally{
            DButil.close(rs, con, st);
        }
        return articles;
    }
    }
      

  6.   

    最好是用工具分析Java core,一般内存溢出造成的服务当机会生成javacore
      

  7.   

    spring的context用api调用一次(new),就要刷新一次,建议用spring容器注入好的bean或action来做数据库操作,而不是jsp标签。
      

  8.   

    tonyyl 
    详述下怎么用spring容器注入好的bean或action来做数据库操作
      

  9.   

    能把你的JSP页面代码发布出来看看吗?可能就知道怎么回事了。
      

  10.   

    如果楼主的程序是用mvc做的,所有数据库操作请在mvc的model里做,例如struts就在action里做,由于楼主的
    dao定义在spring的容器里,最好的处理方式就是把mvc框架与spring集成(参见struts与spring集成的相关信息),然后将mvc的model也定义在spring的容器里。这样model与dao的依赖关系就在spring容器里配好,就不需要用new Xml...context()这种方式加载spring容器了,效率会提升不少。
      

  11.   

    tomcat内存你配置了多少?不会是默认的吧
      

  12.   

    内存溢出,一般要查 static 全局变量,或者 singleton 单例变量,是否不停地往上面两种对象中放东西。
      

  13.   

    楼上的,一语惊醒梦中人,我连接都是用的static,请指示!
    我贴下我连接 的代码,请大家看看!package com.fwcz.util;
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.SQLException;
    import org.apache.log4j.Logger;
    public class DBcon {
    Logger log=Logger.getLogger(DBcon.class);
    /**
     * 函数名:getConnection 作 用:连接数据库 参 数:null 返回值:Connection。连接成功返回 连接对象,否则返回 null
     */
    public static Connection getConnection() {
    Logger log = Logger.getLogger(DBcon.class);
    try {
    Connection con = DriverManager.getConnection("proxool.fwczPool");
    return con;
    } catch (SQLException e) {
    log.fatal("getConnection:" + e.getMessage());
    }
    return null;
    }
    }
      

  14.   

    有几种溢出的、jvm参数先调好。用jprofiler分析
      

  15.   

     st = con.createStatement();
                rs=st.executeQuery(sql);感觉第一个代码可能会导致内存泄露,建议所有的这种地方,改成用prepareStatement
      

  16.   

    你写的网站很容易被注入,不要去拼sql语句
      

  17.   

    在ShowArticle.java 类你不能使用了下面这句调用spring容器。 
    BeanFactory factory=new ClassPathXmlApplicationContext("classpath:applicationContext.xml"); 
    这样一个请求就是一次实例化一次。我们以前的问题一样。你应该使用
    ApplicationContext context = WebApplicationContextUtils.getRequiredWebApplicationContext(application);
    应该从你web容器中取出spring已经实例化好的spring容器。而不是重新实例化。 
      

  18.   

    写的太快改了,BeanFactory factory = WebApplicationContextUtils.getRequiredWebApplicationContext(application); 
      

  19.   

    laorer  你不会就是天乙社区的开发者吧