论坛首页 Java企业应用论坛

大数据量下的toplink JPA分页问题。

浏览 2116 次
该帖已经被评为隐藏帖
作者 正文
   发表时间:2009-08-31  
jpa使用toplink实现。数据表有200多万的记录。 一运行下面的分页查询就出现这个错误:
Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
at com.mysql.jdbc.MysqlIO.reuseAndReadPacket(MysqlIO.java:2931)
at com.mysql.jdbc.MysqlIO.reuseAndReadPacket(MysqlIO.java:2871)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3414)

代码:

public class PagingTest {
EntityManagerFactory emf = null;  
    private javax.persistence.EntityManager em;
/**
* @param args
*/
public static void main(String[] args) {
PagingTest pTest= new PagingTest();
pTest.testPaging();

}
public  void testPaging(){
emf = EntityManagerHelper.getEmf();
em= emf.createEntityManager();
Query rowCountQuery = em.createQuery("SELECT COUNT(c) FROM Customer c"); // NOI18N
int size =((Long) rowCountQuery.getSingleResult()).intValue();
System.out.println(size);
final Query getRowsQuery = em.createQuery("select c from Customer c");
    System.out.println("numer of requests to the database " + counter++);
        Query query = getRowsQuery.setMaxResults(20).setFirstResult(1000000);//这里如果设置为setFirstResult(100000); 就可以运行。
      
        //add the cache
        List <Customer> resultList = query.getResultList();
        System.out.println("=====query done " );
        for(Customer cust:resultList){
        System.out.println("cust:"+cust.getCustomerId()+","+cust.getCustomerName());
        }
}

    private javax.persistence.EntityManager entityManager1;
    private javax.persistence.Query getRowsQuery;
    private javax.persistence.Query rowCountQuery;
  
    private List <Customer> getList() {
        getRowsQuery = java.beans.Beans.isDesignTime() ? null : entityManager1.createQuery("SELECT c FROM Customer c"); // NOI18N
        rowCountQuery = java.beans.Beans.isDesignTime() ? null : entityManager1.createQuery("SELECT COUNT(c) FROM Customer c"); // NOI18N

    List <Customer> toReturn = new ResultListJPA <Customer>(rowCountQuery, getRowsQuery);
        return toReturn;
    }
  
    private int counter=0;
  
}

请教高手这是什么原因?
论坛首页 Java企业应用版

跳转论坛:
Global site tag (gtag.js) - Google Analytics