Ticket #136 (closed 总结: fixed)

Opened 13 years ago

Last modified 13 years ago

关于启用preparestatment cache的问题

Reported by: yuanhuoqing Owned by:
Priority: major Milestone:
Component: 报价库 Version: 报价库5.0
Keywords: preparestatment cache Cc:
Due Date: 18/02/2013

Description (last modified by yuanhuoqing) (diff)

之前产品库遇到个问题,oracle数据表表增加个字段后会报数据库异常:Cause: java.sql.SQLException: 违反协议,然后导致数据库查询很慢,时间久了点会导致数据库游标超限等异常。对应的应用代码可以不更新,只要应用重启后问题就可以解决,在应用重启的过程中会影响网友访问10来分钟。网上找到一个相同的案例: http://www.iteye.com/topic/900077 ,这个报错跟启用preparestatment cache有关。
经过在测试环境测试分析,发现是我们的数据源配置启用了preparestatment cache,配置如下:

<database jndi-name="jdbc/product">
      <driver user="product" password="product" url="jdbc:oracle:thin:@192.168.75.100:1521:testdb" type="oracle.jdbc.driver.OracleDriver"/>
	<prepared-statement-cache-size>8</prepared-statement-cache-size>
	<max-connections>20</max-connections>
	<max-idle-time>30s</max-idle-time>
</database>

参数<prepared-statement-cache-size>8</prepared-statement-cache-size>即是启用preparestatment cache,默认是不启用的,即值为0,去掉这行配置测试不会出现这个异常。

PreparedStatement是JDBC里面提供的对象,很多连接池都引入了PreparedStatementCache的概念。如Jboss连接池、C3P0,DBCP等。PreparedStatementCache即用于保存与数据库交互的prepareStatement对象。在cache里的ps对象,不需要重新走一次DBMS连接请求去创建。
PreparedStatementCache是跟着connection走的。一个connection就会有一个cache。比如一个cache允许缓存20条语句,20个connection就可能缓存400个。一般连接池可以支持这个的配置,能提高性能,但会占用较大内存,同时增加表字段时如果不重启应用会报异常:java.sql.SQLException: 违反协议,需要重启应用才能解决,所以一般不建议启用PreparedStatementCache。

Change History

comment:1 Changed 13 years ago by yuanhuoqing

  • Status changed from new to closed
  • Resolution set to fixed
  • Description modified (diff)

comment:2 Changed 13 years ago by yuanhuoqing

  • Description modified (diff)

comment:3 Changed 13 years ago by chenchongqi

"所以一般不建议启用PreparedStatementCache"

没必要下这样的结论,知道这个情况,具体要怎么用看各自应用的实际情况,在性能和维护性方面各自取舍

comment:4 Changed 13 years ago by chenchongqi

  • Milestone 2012报价库5.0 deleted
Note: See TracTickets for help on using tickets.