发帖    主题    评论    推荐    标签    作者    订阅    查搜    注册   登陆   关注
 
面向对象 设计模式 领域驱动设计 企业架构 框架 开发教程 微服务 大数据 扩展性 并发编程 事件驱动 分布式 SOA

hibernate中的outofmemory问题

2006-07-04 18:01
赞助商链接

以下是我用来测试的源代码,每页数量为7

java代码:

public class HibernateTest extends Thread {

static Log log = LogFactory.getLog("");

IArticleManager iArticle;

/**
* @param article
* @throws IllegalAccessException
* @throws InstantiationException
* @throws ClassNotFoundException
*/

public HibernateTest() {
super();
try {
iArticle = ArticleManagerFactory.createFactory()
.getArticleManager();
} catch (ClassNotFoundException e) {
log.warn(
"ClassNotFoundException", e);
} catch (InstantiationException e) {
log.warn(
"InstantiationException", e);
} catch (IllegalAccessException e) {
log.warn(
"IllegalAccessException", e);
}
}

public void run() {
long startDate = System.currentTimeMillis();
try {
//List list = iArticle.getAllArticle();
findFiveWork();
} catch (HibernateException e) {
log.warn(
"HibernateException", e);
} catch (ResourceException e) {
e.printStackTrace();
}
long endDate = System.currentTimeMillis();
log.warn(String.valueOf(endDate - startDate));
super.run();
}

public Vector findFiveWork() throws ResourceException, HibernateException {

SessionAdapter sa = new SessionAdapter();
sa.setSession(Helper.getSessions().openSession());
log.warn(sa.getSession());
Transaction tx = sa.getSession().beginTransaction();
try {
Vector vec = new Vector();
long lnum = ((Integer) sa
.getSession()
.iterate(
"select count(*) from ComputerWork as work where work.audited=1 order by work.uploadDate desc")
.next()).intValue();
Query query = sa
.getSession()
.createQuery(
"select from ComputerWork as work where work.audited=1 order by work.uploadDate desc");
Page page = new Page(query, lnum, 5);
// 返回第一页就足够了,因为显示单项作Æ返Ä页面Ö灰钚路⒈í的前5位。
page.goToPageN(1);
vec.add(page);

lnum = ((Integer) sa
.getSession()
.iterate(
"select count(*) from CaseWork as work where work.audited=1 order by work.uploadDate desc")
.next()).intValue();
query = sa
.getSession()
.createQuery(
"select from CaseWork as work where work.audited=1 order by work.uploadDate desc");
page = new Page(query, lnum, 5);
page.goToPageN(1);
vec.add(page);

lnum = ((Integer) sa
.getSession()
.iterate(
"select count(*) from TeacherHomeWork as work where work.audited=1 order by work.uploadDate desc")
.next()).intValue();
query = sa
.getSession()
.createQuery(
"select from TeacherHomeWork as work where work.audited=1 order by work.uploadDate desc");
page = new Page(query, lnum, 5);
page.goToPageN(1);
vec.add(page);
lnum = ((Integer) sa
.getSession()
.iterate(
"select count(*) from ResourceWork as work where work.audited=1 order by work.uploadDate desc")
.next()).intValue();
query = sa
.getSession()
.createQuery(
"select from ResourceWork as work where work.audited=1 order by work.uploadDate desc");
page = new Page(query, lnum, 5);
page.goToPageN(1);
vec.add(page);

lnum = ((Integer) sa
.getSession()
.iterate(
"select count(*) from SchoolPageWork as work where work.audited=1 order by work.uploadDate desc")
.next()).intValue();
query = sa
.getSession()
.createQuery(
"select from SchoolPageWork as work where work.audited=1 order by work.uploadDate desc");
page = new Page(query, lnum, 5);
page.goToPageN(1);
vec.add(page);
log.warn(tx+
" 第五次:"+String.valueOf(System.currentTimeMillis())+"内存"+Runtime.getRuntime().totalMemory());

tx.commit();
return vec;
} catch (HibernateException e) {
tx.rollback();
throw new ResourceException(e,
ResourceException.deleteCaseWorkByIdWithRightString);
} catch (Exception e) {
throw new ResourceException(e);
} finally {
sa.getSession().close();
log.warn(String.valueOf(System.currentTimeMillis())+
":seesion关闭后内存"+Runtime.getRuntime().totalMemory());
}
}

public static void main(String[] args) {

log.warn(
"虚拟机最大内存:"+Runtime.getRuntime().maxMemory());
log.warn(String.valueOf(System.currentTimeMillis())+
":开始前内存"+Runtime.getRuntime().totalMemory());

PropertyConfigurator.configure(HibernateTest.class
.getResource(
"/properties/log4j.properties"));

for (int i = 0; i < 1; i++) {
new HibernateTest().start();
}

}
}



/**
* 这是page的goToPage方法,进行了query.iterate查询,并且查询前后都打印出了当前内存
* 得到第n页的记录条目
* @param n
*/

public boolean goToPageN( int n ) throws HibernateException
{
if( n < 0 || n > this.pagesCount )
return false;
else
this.currentPageNo = n;

// 如果结果是最后一页
if( isLastPage() )
{
this.nextPageNo = 0;
this.isLast = 1;
}
else
{
this.nextPageNo = this.currentPageNo + 1;
this.isLast = 0;
}

// 如果结果是第一页
if( isFirstPage() )
{
this.isFirst = 1;
this.prevPageNo = 0;
}
else
{
this.isFirst = 0;
this.prevPageNo = this.currentPageNo - 1;
}
this.setQuery();
System.out.println(System.currentTimeMillis()+
"query前:内存"+Runtime.getRuntime().totalMemory());
Iterator it = this.query.iterate();
while(it.hasNext()) {
this.list.add(it.next());
}
System.out.println(System.currentTimeMillis()+
"query后:内存"+Runtime.getRuntime().totalMemory());
return true;
}






这是在测试里只对线程进行一次循环时的输出:
log4j:WARN No appenders could be found for logger ().
log4j:WARN Please initialize the log4j system properly.
[WARN ] 2006-07-04 13:37:11,000 method:com.raykey.HibernateTest.findFiveWork(HibernateTest.java:7
net.sf.hibernate.impl.SessionImpl@69ed4a
1151991431265query前:内存4702208
1151991432296query后:内存13787136
1151991432359query前:内存13787136
1151991433734query后:内存25522176
1151991433812query前:内存25522176
1151991433953query后:内存25522176
1151991434000query前:内存25522176
1151991434421query后:内存44150784
1151991434453query前:内存44150784
1151991434812query后:内存66650112
[WARN ] 2006-07-04 13:37:14,812 method:com.raykey.HibernateTest.findFiveWork(HibernateTest.java:146)
net.sf.hibernate.transaction.JDBCTransaction@1ced821 第五次:1151991434812内存66650112
[WARN ] 2006-07-04 13:37:14,828 method:com.raykey.HibernateTest.findFiveWork(HibernateTest.java:158)
1151991434828:seesion关闭后内存66650112
[WARN ] 2006-07-04 13:37:14,828 method:com.raykey.HibernateTest.run(HibernateTest.java:70)
18766
这是在测试里只对线程进行二次循环时的输出(for(int i=0;i<2;i++)):
log4j:WARN No appenders could be found for logger ().
log4j:WARN Please initialize the log4j system properly.
[WARN ] 2006-07-04 13:38:54,921 method:com.raykey.HibernateTest.findFiveWork(HibernateTest.java:7
net.sf.hibernate.impl.SessionImpl@59cbda
[WARN ] 2006-07-04 13:38:54,921 method:com.raykey.HibernateTest.findFiveWork(HibernateTest.java:7
net.sf.hibernate.impl.SessionImpl@f429d7
1151991535218query前:内存4718592
1151991536218query前:内存13824000
1151991536843query后:内存28475392
1151991537031query前:内存28475392
1151991537046query后:内存28475392
1151991537140query前:内存28475392
1151991538421query后:内存37421056
1151991538687query前:内存54800384
1151991538796query后:内存54800384
1151991539031query后:内存54800384
1151991539062query前:内存54800384
1151991539390query后:内存54800384
1151991539421query前:内存54800384
1151991539734query前:内存66650112
1151991540281query后:内存66650112
1151991540531query前:内存66650112
1151991540593query后:内存66650112
1151991541140query前:内存66650112
1151991542734query后:内存66650112
[WARN ] 2006-07-04 13:39:02,734 method:com.raykey.HibernateTest.findFiveWork(HibernateTest.java:146)
net.sf.hibernate.transaction.JDBCTransaction@1e7c609 第五次:1151991542734内存66650112
[WARN ] 2006-07-04 13:39:03,000 method:com.raykey.HibernateTest.findFiveWork(HibernateTest.java:158)
1151991543000:seesion关闭后内存66650112
[WARN ] 2006-07-04 13:39:03,000 method:com.raykey.HibernateTest.run(HibernateTest.java:70)
14344
[WARN ] 2006-07-04 13:39:03,078 method:
1151991543078:seesion关闭后内存66650112
java.lang.OutOfMemoryError


可以看到,并发两个进程内存就挂了,各位有什么办法吗?

我在hibernate是是这么配置缓存的:
hibernate.cache.provider_class net.sf.hibernate.cache.HashtableCacheProvider
hibernate.cache.use_query_cache true

2006-07-06 16:03

Hibernate批量查询非常耗费内存。

赞助商链接

赞助商链接

返回顶部

移动版 关于本站 使用帮助 联系管理员 最佳分辨率1366x768
OpenSource JIVEJDON Powered by JdonFramework Code © 2002-20 jdon.com