一个新站,租用的虚拟空间,里面的内容不多,访问量也不大独立ip每天150吧,数据库操作在service包里面的,用了jdbc和ibatis只有增加时用到ibatis,大多都是用的jdbc,jdbc访问数据库后我都关了的,ibatis是用spring管理数据库的,所以没申明关闭。但运行一天就内存溢出不知道是怎么回事,特来求助csdn想高手们帮下忙! 
一般只访问了网站的主页和http://www.house08.cn右侧的少许文章。以下代码是我一个文章显示的流程,但也不知道是哪句,特贴出代码,请大家帮解决下。 
自定义标签:ShowArticle.java 
package 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()方法 Java codepackage 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);
            }
            
        } catch (SQLException e) {
            e.printStackTrace();
        }finally{
            DButil.close(rs, con, st);
        }
        return articles;
    }
}

解决方案 »

  1.   

    applicationContext.xml配置文件<?xml version="1.0" encoding="UTF-8"?>
    <beans
    xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">

    <bean id="dataSource"
    class="org.apache.commons.dbcp.BasicDataSource">
    <property name="driverClassName">
    <value>com.microsoft.jdbc.sqlserver.SQLServerDriver</value>
    </property>
    <property name="url">
    <value>jdbc:microsoft:sqlserver://127.0.0.1:1433;DatabaseName=fwcz</value>
    </property>
    <property name="username">
    <value>sa</value>
    </property>
    <property name="password">
    <value>sa</value>
    </property>
    </bean>

    <bean id="sqlMapClient"
    class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
    <property name="configLocation">
    <value>classpath:ibatis-sql-map-config.xml</value>
    </property>
    </bean>
    <!-- start service -->
    <bean id="blogDaoImpl" class="com.fwcz.blog.service.BlogDaoImpl">
    <property name="dataSource">
    <ref local="dataSource"/>
    </property>
    <property name="sqlMapClient">
    <ref local="sqlMapClient"/>
    </property>
    </bean>
    <bean id="provinceCapitalImpl" class="com.fwcz.service.ProvinceCapitalImpl">
    <property name="dataSource">
    <ref local="dataSource"/>
    </property>
    <property name="sqlMapClient">
    <ref local="sqlMapClient"/>
    </property>
    </bean>
    <bean id="houseInfoDaoImpl" class="com.fwcz.service.HouseInfoDaoImpl">
    <property name="dataSource">
    <ref local="dataSource"/>
    </property>
    <property name="sqlMapClient">
    <ref local="sqlMapClient"/>
    </property>
    </bean> <bean id="areaInfoDaoImpl" class="com.fwcz.service.AreaInfoDaoImpl">
    <property name="dataSource">
    <ref local="dataSource"/>
    </property>
    <property name="sqlMapClient">
    <ref local="sqlMapClient"/>
    </property>
    </bean> <bean id="secondHandDaoImpl" class="com.fwcz.service.SecondHandDaoImpl">
    <property name="dataSource">
    <ref local="dataSource"/>
    </property>
    <property name="sqlMapClient">
    <ref local="sqlMapClient"/>
    </property>
    </bean> <bean id="houseTypeDaoImpl" class="com.fwcz.service.HouseTypeDaoImpl">
    <property name="dataSource">
    <ref local="dataSource"/>
    </property>
    <property name="sqlMapClient">
    <ref local="sqlMapClient"/>
    </property>
    </bean>

    <bean id="articleDaoImpl" class="com.fwcz.service.ArticleDaoImpl">
    <property name="dataSource">
    <ref local="dataSource"/>
    </property>
    <property name="sqlMapClient">
    <ref local="sqlMapClient"/>
    </property>
    </bean>

    <bean id="memberDaoImpl" class="com.fwcz.service.MemberDaoImpl">
    <property name="dataSource">
    <ref local="dataSource"/>
    </property>
    <property name="sqlMapClient">
    <ref local="sqlMapClient"/>
    </property>
    </bean>

    <bean id="categoryDaoImpl" class="com.fwcz.service.CategoryDaoImpl">
    <property name="dataSource">
    <ref local="dataSource"/>
    </property>
    <property name="sqlMapClient">
    <ref local="sqlMapClient"/>
    </property>
    </bean>
    <!-- end service -->



    <!-- start struts -->
    <bean name="/houseInfo" class="com.fwcz.action.HouseInfoAction" scope="prototype">
    <property name="houseInfoDaoImpl">
    <ref local="houseInfoDaoImpl"/>
    </property>
    </bean> <bean name="/article" class="com.fwcz.action.ArticleAction" scope="prototype">
    <property name="articleDaoImpl">
    <ref local="articleDaoImpl"/>
    </property>
    </bean> <bean name="/secondHand" class="com.fwcz.action.SecondHandAction" scope="prototype">
    <property name="secondHandDaoImpl">
    <ref local="secondHandDaoImpl"/>
    </property>
    </bean> <bean name="/member" class="com.fwcz.action.MemberAction" scope="prototype">
    <property name="memberDaoImpl">
    <ref local="memberDaoImpl"/>
    </property>
    </bean>
    <!-- end struts -->
    </beans>
      

  2.   

    pagination方法,并没有完全实现分页算法。
    楼主的SQL只是将一张表的所有内容查询出来,而非只有所需页面的内容。
    也就是说,返回List的size大小,应当小于等于defaultSize参数。
    但,看似貌似不是。
    这样的话,当表中数据非常多时,内存会爆掉,而且,交互的数据量也大。
      

  3.   

    你这个到底是 iBatis 还是 JDBC 啊?
      

  4.   

    代码很乱,写得也很糟糕,需要改进的地方很多,很多!比如:public static void close(ResultSet rs,Connection con,Statement stmt){
        try {
            rs.close();
            stmt.close();
            con.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }1,rs 为 null 会出现什么情况?
    2,如果 rs 关闭失败了,那么 stmt, con 都将无法关闭!
      

  5.   

    我想说的不多,,,以前我也用jdbc写过项目,,,也是出现这个问题,,,
    解决办法,,,
    我用了数据源之后就没事了,,,,不能说没事了,,是在,3个月后的出现的这个问题,,,一句话,,,jdbc没有数据源的性能好用,,,
    去你的spring配置中改为数据源,,,试试
      

  6.   

    //分页
        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);
                }
                
            } catch (SQLException e) {
                e.printStackTrace();
            }finally{
                DButil.close(rs, con, st);
            }
            return articles;
        }这段代码有问题,
    你的分页函数,返回值List应该是当前页面的信息,但是,SQL语句查出的不是当前页面信息,而是所有的信息。
    当数据库表内记录非常多时,会产生内存泄漏