c3p0.max_statements = 0 GooGooStatementCache Problem with checked-in Statement,

c3p0.max_statements不设置等于0时报如下错误

INFO 2010-07-23 16:29:38,490 [com.mchange.v2.c3p0.stmt.GooGooStatementCache] - Problem with checked-in Statement, discarding.
java.lang.NullPointerException
##
<!-- 最大连接数 -->
<property name="hibernate.c3p0.max_size">20</property>
<!-- 最小连接数 -->
<property name="hibernate.c3p0.min_size">5</property>
<!-- 获得连接的超时时间,如果超过这个时间,会抛出异常,单位毫秒 -->
<property name="hibernate.c3p0.timeout">120</property>
<!-- 最大的PreparedStatement的数量 -->
<property name="hibernate.c3p0.max_statements">0</property>
<!-- 每隔120秒检查连接池里的空闲连接 ,单位是秒-->
<property name="hibernate.c3p0.idle_test_period">120</property>
<!-- 当连接池里面的连接用完的时候,C3P0一下获取的新的连接数 -->
<property name="hibernate.c3p0.acquire_increment">2</property>
<!-- 每次都验证连接是否可用 -->
<property name="hibernate.c3p0.validate">true</property>
==========================================

“SEVERE: The last packet successfully received from the server was144382 seconds ago.The last packet sent successfully to the server was 144382 seconds ago, which is longer than the server configured value of 'wait_timeout'. You should consider either expiring and/or testing connection validity before use in your application, increasing the server configured values for client timeouts, or using the Connector/J connection property 'autoReconnect=true' to avoid this problem.”
你的log里说的很清楚了。原因是Mysql服务器默认的“wait_timeout”是8小时,如果一个connection空闲超过8个小时,Mysql将自动断开该 connection,而C3P0并不知道该connection已经失效,如果这时请求connection,将会造成上面的异常。你每次使用前判断connection是否有效就会避免这种异常。

解决的方法有3种:

增加wait_timeout的时间。
减少Connection pools中connection的lifetime。
测试Connection pools中connection的有效性。
当然最好的办法是同时综合使用上述3种方法,下面举个例子,假设wait_timeout为默认的8小时

C3P0增加以下配置信息:

//获取connnection时测试是否有效
testConnectionOnCheckin = true
//自动测试的table名称

automaticTestTable=C3P0TestTable

//set to something much less than wait_timeout, prevents connections from going stale
idleConnectionTestPeriod = 18000
//set to something slightly less than wait_timeout, preventing 'stale' connections from being handed out
maxIdleTime = 25000
//if you can take the performance 'hit', set to "true"
testConnectionOnCheckout = true

===================================

1、org.hibernate.LazyInitializationException: could not initialize proxy - the owning Session was closed.

问题:hibernate3默认的lazy为true,使用代理模式proxy属性允许延迟加载类的持久化实例。调用session.load()方法,Hibernate开始会返回CGLIB代理,除主键外的其他值均为null。当代理的某个方法被实际调用的时候, 真实的持久化对象才会被装载,但必须在同一个session中。如session.close()前一直未调用方法,close()后再调用,报上述错误。

解决:a.hbm.xml中 class项,加上lazy=false

b.使用session.get()方法,将不延迟,直接取出对象实例。

2、java.sql.SQLException: [Microsoft][SQLServer 2000 Driver for JDBC]Can't start a cloned connection while in manual transaction mode.

问题:ms SQLserver在设置为autoCommit=false,SelectMethod=direct时,处理多个statement将报该错

解决:在url加上设置SelectMethod=Cursor

3、java.lang.NullPointerException: Problem with checked-in Statement, discarding.

问题:oracle9i前的jdbc Driver有bug,多个statement亦会出错

解决:在http://www.oracle.com/technology/software/tech/java/sqlj_jdbc/index.html上下载最新的ojdbc14.jar,版本为10.2.0.1.0
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Here are some suggested optimizations for the given code: 1. Use switch case instead of if-else: In the current code, multiple if-else statements are being used to check for different conditions. This can be optimized by using switch case instead, which can improve readability and maintainability of the code. 2. Use early return to reduce nested if-else: In the current code, multiple if-else statements are nested inside a while loop, which can make the code difficult to read and understand. By using early return, we can reduce the level of nesting and make the code more readable. 3. Avoid redundant code: In the current code, there are multiple instances where the same block of code is being repeated. This can be optimized by refactoring the code and avoiding the duplication of code. Here is the optimized code: ``` static void fill_file_chunk(const char *path, int *number, int level) { DIR *dir = opendir(path); if(dir == NULL) { return; } ++level; if(level > 3) { closedir(dir); return; } struct dirent *entry; while((entry = readdir(dir)) != NULL) { switch(entry->d_type) { case DT_DIR: if(level == 1 || level == 2) { if(check_dir_name(entry->d_name) == 0) { continue; } char buffer[MAX_PATH]; sprintf(buffer, "%s/%s", path, entry->d_name); fill_file_chunk(buffer, number, level); } break; case DT_REG: if(level == 3) { if(check_file_name(entry->d_name) == 0) { continue; } if(*number == max_file_chunk) { file_chunks = (struct FileChunk *)realloc(file_chunks, (max_file_chunk + FILE_CHUNK_INCREAMENT) * sizeof(struct FileChunk)); if(file_chunks == NULL) { break; } max_file_chunk += FILE_CHUNK_INCREAMENT; } char buffer[MAX_PATH]; sprintf(buffer, "%s/%s", path, entry->d_name); int i = *number; strcpy(file_chunks[i].file_name, entry->d_name); sprintf(file_chunks[i].file_path, buffer); file_chunks[i].file_size = get_file_size(buffer); *number = i + 1; } break; default: break; } } closedir(dir); } ``` This optimized code uses switch case instead of if-else statements, reduces the level of nesting by using early return, and avoids redundant code by refactoring the code.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值