HibernateでC3P0接続プールを構成する方法
Connection Pool
接続プールは、Javaアプリケーションがデータベースと対話するたびに接続を作成するのを防ぎ、接続を開いたり閉じたりするコストを最小限に抑えるため、パフォーマンスに優れています。
wiki connection poolの説明を参照してください
Hibernateには内部接続プールが付属していますが、実稼働での使用には適していません。 このチュートリアルでは、サードパーティの接続プール(C3P0)をHibernateと統合する方法を示します。
1. hibernate-c3p0.jarを取得します
c3p0をHibernateと統合するには、hibernate-c3p0.jarが必要です。JBossリポジトリから取得してください。
ファイル:pom.xml
JBoss repository http://repository.jboss.org/nexus/content/groups/public/ org.hibernate hibernate-core 3.6.3.Final org.hibernate hibernate-c3p0 3.6.3.Final
2. c3p0プロパティを構成する
c3p0を構成するには、次のように、c3p0構成の詳細を「hibernate.cfg.xml」に入れます。
ファイル:hibernate.cfg.xml
oracle.jdbc.driver.OracleDriver jdbc:oracle:thin:@localhost:1521:MKYONG example password org.hibernate.dialect.Oracle10gDialect MKYONG true 5 20 300 50 3000
-
hibernate.c3p0.min_size – Minimum number of JDBC connections in the pool. Hibernateのデフォルト:1
-
hibernate.c3p0.max_size – Maximum number of JDBC connections in the pool. Hibernateのデフォルト:100
-
hibernate.c3p0.timeout – When an idle connection is removed from the pool (in second). Hibernateのデフォルト:0、無期限。
-
hibernate.c3p0.max_statements – Number of prepared statements will be cached. パフォーマンスを向上させます。 Hibernateのデフォルト:0、キャッシングは無効です。
-
hibernate.c3p0.idle_test_period – idle time in seconds before a connection is automatically validated. Hibernateのデフォルト:0
Note
hibernate-c3p0の構成設定の詳細については、thisの記事をお読みください。
実行、出力
完了して実行すると、次の出力が表示されます。
接続初期化プロセス中に、接続プールに5つのデータベース接続が作成され、Webアプリケーションですぐに再利用できます。
ダウンロード–Hibernate-C3P0-Connection-Pool-Example.zip(8KB)