Oracle   发布时间:2022-05-17  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了Oracle JDBC prefetch: how to avoid running out of RAM大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

Using Oracle java JDBC (ojdbc6 11.2.0.4),loading a query with many rows takes forever (high latency environment. This is Apparently the default prefetch In Oracle JDBC is default size "10" which requires a round trip time once per 10 rows. I am attempTing to set an aggressive prefetch size to avoID this.

PreparedStatement stmt = conn.prepareStatement("SELEct * from tablename"); statement.setFetchSize(10000); ResultSet rs = statement.executequery(); 

This can work,but instead I get an out of memory exception. I had presumed that setFetchSize would tell it to buffer "that many rows" as they come in,using as much RAM as each row requires. If I run with 50 threads,even with 16G of -XMX space,it runs out of memory. Feels almost like a leak:

Exception in thread "@H_338_17@main" java.lang.outOfMemoryError: Java heap space at java.lang.reflect.Array.newArray(Native @H_338_17@method) at java.lang.reflect.Array.newInstance(Array.java:70) at Oracle.jdbc.driver.bufferCache.get(BufferCache.java:226) at Oracle.jdbc.driver.PhysicalConnection.getCharBuffer(PhysicalConnection.java:7422) at Oracle.jdbc.driver.OracleStatement.prepareAccessors(OracleStatement.java:983) at Oracle.jdbc.driver.T4CTTIDcb.receiveCommon(T4CTTIDcb.java:273) at Oracle.jdbc.driver.T4CTTIDcb.receive(T4CTTIDcb.java:144) at Oracle.jdbc.driver.T4C8Oall.readDCB(T4C8Oall.java:771) at Oracle.jdbc.driver.T4CTTIfun.receive(T4CTTIfun.java:346) at Oracle.jdbc.driver.T4CTTIfun.doRPC(T4CTTIfun.java:186) at Oracle.jdbc.driver.T4C8Oall.doOALL(T4C8Oall.java:521) at Oracle.jdbc.driver.T4CPreparedStatement.doOall8(T4CPreparedStatement.java:205) at Oracle.jdbc.driver.T4CPreparedStatement.executeForDescribe(T4CPreparedStatement.java:861) at Oracle.jdbc.driver.OracleStatement.executeMaybedescribe(OracleStatement.java:1145) at Oracle.jdbc.driver.OracleStatement.doExecuteWithTimeout(OracleStatement.java:1267) at Oracle.jdbc.driver.OraclePreparedStatement.executeInternal(OraclePreparedStatement.java:3449) at Oracle.jdbc.driver.OraclePreparedStatement.executequery(OraclePreparedStatement.java:3493) at Oracle.jdbc.driver.OraclePreparedStatementWrapper.executequery(OraclePreparedStatementWrapper.java:1491) .... 

what can I do to still get prefetch but not run out of RAM? what is going on?

The closest related item on SO is this: http://stackoverflow.com/a/14317881/32453

解决方法

Basically,Oracle‘s default strategy for later ojdbc jars is to "pre allocate" an array per "prefetch" row that accomodates for the largest size conceivably possible to return from that query. So in my case I had some VARCHAR2(4000) in there,so 50 threads * 3 columns of varchar2‘s * 4000 was adding up to more than gigabytes of RAM [yikes]. There does not appear to be an option to say "don‘t pre allocate that array,just use the size needed." Ojdbc even keeps these preallocated buffers around between preparedstatements so it can reuse them. Definitely a memory hog.

The fix was to determine the maximum actual column size,then replace the query with (assuming 50 is the max sizE) SELEct substr(column_name,50) as well as profile and only use as high of setFetchSize as actually made significant speed 
 ments.

Other things you can do: decrease the number of prefetch rows,increase Xmx parameter,only SELEct the columns you need.

Once we were able to use at least prefetch 400 [make sure to profile to see what numbers are good for you,with high latency we saw 
 ments up to prefetch size 3-4K] on all querIEs,perfoRMANce 
 d dramatically.

I suppose if you wanted to be really aggressive against sparse "really long" rows you might be able to re-query when you run into these [rare] rows.

Details ad nauseum here

最新还有个解决方法,使用ojdbc8,已经是按需分配模式。

大佬总结

以上是大佬教程为你收集整理的Oracle JDBC prefetch: how to avoid running out of RAM全部内容,希望文章能够帮你解决Oracle JDBC prefetch: how to avoid running out of RAM所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签: