Spring、JDBCTemplate、および c3p0 を使用して、データベースにアクセスできる Web アプリケーションを開発しています。
サーバーがフリーズすることがよくありますが、それはビジー状態のデータベース接続の数が原因であると確信しています。jconsole を使用してアプリケーションの動作を監視すると、ComboPooledDataSource の maxPoolSize に達し、サーバーがページをロードしなくなっていることがわかります。
便利なコードは次のとおりです。
データソース定義:
<Resource auth="Container" description="GDLWeb DB Connection"
driverClass="org.postgresql.Driver"
maxPoolSize="16"
minPoolSize="1"
acquireIncrement="1"
maxIdleTime="60"
maxStatements="0"
idleConnectionTestPeriod="1800"
acquireRetryAttempts="30"
breakAfterAcquireFailure="true"
name="jdbc/gdlweb"
user="gdlweb"
password=""
factory="org.apache.naming.factory.BeanFactory"
type="com.mchange.v2.c3p0.ComboPooledDataSource"
jdbcUrl="jdbc:postgresql://localhost:5432/postgres"
/>
典型的なアクセス方法 (DAO クラス内):
protected T getPersistentObject(
final String tableName,
final List<WhereClause> whereParams,
final RowMapper<T> rowMapper) {
try {
log.debug(this, "get " + tableName + " " + whereParams);
return (T) getTemplate().queryForObject(
generateSelectStar(tableName, whereParams),
extractValueMap(whereParams),
rowMapper);
} catch (final EmptyResultDataAccessException e) {
log.warning(this, "No " + tableName + " found with " + whereParams + " in the DB!");
return null;
}
}
maxPoolSize を 100 に増やそうとしました。これは、postgresql サーバーで定義されている maxConnections です。このようにして、postgresql サーバーがクラッシュする直前に、現在開いているビジー状態の接続が 43 あることがわかりました。
私はおそらく JDBCTemplate を間違った方法で使用していますが、どこにあるのかわかりません。
ありがとう。