一个lucene的问题,请帮忙
sahero
2008-08-26
由于工作需要,要用lucene进行开发,本人写了一个方法对lucene的index进行定时追加.该程序在main方法运行,没有任何问题.我用spring提供的定时器写了一个应用,定时运行该程序.问题出现了,发现每次运行完该程序时候,系统性能就会变得很差,甚至会出现OUT MEM.程序大概如下:
try { writer = new IndexWriter("f:\\index",new IK_CAnalyzer(),false); while (rs.next()){ Document doc = new Document(); int acode = rs.getInt("acode"); String title = rs.getString("title"); reader = new StringReader(title); doc.add(new Field("content",reader)); doc.add(new Field("acode",String.valueOf(acode),Field.Store.YES,Field.Index.NO)); writer.addDocument(doc); Thread.sleep(100); } } } } catch(Exception e) { e.printStackTrace(); }finally{ try { reader.close(); writer.close(); rs.close(); stmt.close(); conn.close(); } catch (Exception e) { e.printStackTrace(); } } } 系统环境是jdk1.5 tomcat5.0 window lucene2.3.2 本人之前试过对索引单条增加,但也出现了严重的性能问题.估计是我的这个程序未写好,望指导.谢 |
|
xietingyan
2008-08-26
不会,帮你顶,有没可能是在优化索引啊
|
|
fys124974704
2008-08-27
就从这些代码看不出问题所在,除了没有进行索引优化之外,
while (rs.next()){ Document doc = new Document(); 最好写成 Document doc = null; while (rs.next()){ doc = new Document(); 其它的我也看不出啥问题了! |
|
zhouning13
2008-08-27
1.
reader = new StringReader(title); 是在while里面的,而回收reader的reader.close();却放在了finally里面,如果你的while执行了100遍,就生成了100个 reader,而你只回收了最后一个reader,那么当然会出现内存泄漏,最后就out of memory。 2. 如果reader.close();出现了异常, 程序就跳到了finally里面的catch 中, writer.close(); rs.close(); stmt.close(); conn.close(); 这四句话就都没有执行。 如果在writer.close();出现了异常, rs.close(); stmt.close(); conn.close(); 这三句话就都没有执行。 最好还是每个都加句try....catch吧,最然很难看,但是安全。 |
|
sahero
2008-08-27
好的,谢谢哥们,我先试下~~
|
|
sahero
2008-08-27
郁闷,还是有问题,每当使用lucene的索引去搜索的时候,就出现out mem了.如果设置,不去追加索引,就没有问题...
|
|
sahero
2008-08-27
汗一个~!我原来是在两个项目上运行,同时应用同一个INDEX,我现在把这两个项目合在一起..可以了...
|
|
jianghuidong
2008-12-17
问一下,你是对数据库操作的么?
|
|
k7710
2008-12-17
建议分多批更新writer
|
|
jianghuidong
2008-12-18
楼主,在什么地方显示追加的?没看懂。
|