Hibernate and connection pooling

Hibernate support third-party connection pool, the official recommendation of the connection pool is C3P0, Proxool, and DBCP. When configuring connection pool to note three points:

1, Apche of DBCP in the Hibernate2 is supported, but is no longer recommended in Hibernate3, the official explanation is that this connection pool flawed. If for some reason you need to Hibernate3 to use DBCP, recommended JNDI approach.

2, by default (that is not configured connection pool case), Hibernate will use the built-in connection pooling. But the poor performance of the connection pool, and there are many BUG, so the government has also just a proposal to use only in development environment.

3, Hibernate2 and Hibernate3 namespace change. For example, configure the C3P0 when provider_class have Hibernate2 environments net.sf.hibernate.connection.C3P0ConnectionProvider, in Hibernate3 environments org.hibernate.connection.C3P0ConnectionProvider.

The following are several common circumstances Hibernate connection pool configuration is to connect MySQl as an example.

<! - JDBC Driver - "
<property name="connection.driver_class"> org.gjt.mm.mysql.Driver </ property>
<property name="connection.url"> jdbc: mysql: / / localhost: 3306/struts? useUnicode = true
& CharacterEncoding = GBK </ property>
"! - Database user name -"
<property name="connection.username"> root </ property>
"! - Database password -"
<property name="connection.password"> 8888 </ property>

Section above configuration, in the c3p0 and dbcp in, are necessary because the hibernate configuration based on the above to generate connections, re-dbcp to c3p0 or management. However, proxool can not, you can let proxool to generate their own connections, which in the following repeat that.

Online google a bit and found that there are basically three kinds of solutions can be implemented:
1, using hibernate native connection pool;
2, using c3po connection pool package functions;
3, using the dbcp connection pooling feature package;
4, using Proxool package connection pooling function;

The first program, Hibernate default connection pool, that is, myeclipse generated hibernate.cfg.xml add an attribute:
<property name="connection.pool_size"> 20 </ property>

The second program is: C3P0

Just add the hibernate.cfg.xml
<property name="c3p0.min_size"> 2 </ property>
<property name="c3p0.max_size"> 10 </ property>
<property name="c3p0.timeout"> 1800 </ property>
<property name="c3p0.acquireRetryAttempts"> 4 </ property>
<property name="c3p0.acquireIncrement"> 1 </ property>
<property name="c3p0.idleConnectionTestPeriod"> 36000 </ property>
<property name="c3p0.initialPoolSize"> 2 </ property>
<property name="c3p0.maxPoolSize"> 10 </ property>
<property name="c3p0.maxIdleTime"> 1200 </ property>
<property name="c3p0.maxStatements"> 30 </ property>
<property name="c3p0.minPoolSize"> 2 </ property>
<property name="connection.provider_class"> org.hibernate.connection.C3P0ConnectionProvider </ property>
There are in classespath added c3p0-0.8.4.5.jar

The third in the program are: dbcp

Adding in the hibernate.cfg.xml

<property name="dbcp.maxActive"> 100 </ property>
<property name="dbcp.whenExhaustedAction"> 1 </ property>
<property name="dbcp.maxWait"> 60000 </ property>
<property name="dbcp.maxIdle"> 10 </ property>

<property name="dbcp.ps.maxActive"> 100 </ property>
<property name="dbcp.ps.whenExhaustedAction"> 1 </ property>
<property name="dbcp.ps.maxWait"> 60000 </ property>
<property name="dbcp.ps.maxIdle"> 10 </ property>

There are in classespath adding commons-pool-1.2.jar and commons-dbcp-1.2.1.jar.

The fourth in the program are: Proxool

The establishment of a Proxool.xml file:
<? xml version = "1.0" encoding = "UTF-8"?>
<! - The proxool configuration can be embedded within your own application's.
Anything outside the "proxool" tag is ignored. ->
<something-else-entirely>
<proxool>
<alias>
mysql
</ alias>
<driver-url>
jdbc: mysql: / / localhost: 3306/jackdemo
</ driver-url>
<driver-class>
com.mysql.jdbc.Driver
</ driver-class>
<driver-properties>
<property name="user" value="root" />
<property name="password" value="jack" />
</ driver-properties>
<house-keeping-sleep-time>
60000
</ house-keeping-sleep-time>
<proxool.simultaneous-build-throttle>
100
</ proxool.simultaneous-build-throttle>
<prototype-count>
2
</ prototype-count>
<maximum-connection-count>
100
</ maximum-connection-count>
<minimum-connection-count>
10
</ minimum-connection-count>
</ proxool>
</ something-else-entirely>

Now look at the configuration of meaning:

<alias> to configure connection pool alias;
<driver-url> and writing JDBC connect to the database when the same URL.
<driver-class> and writing JDBC connect to the database the same as when the Driver.
<driver-properies> that connect to the database when the user name and password.
<house-keeping-sleep-time> proxool automatically detect connection state at all time intervals (ms), detected idle connection is immediately recovered, the destruction of overtime
<prototype-count> at a minimum the number of idle connections
<maximum-connection-count> maximum number of connections.
<minimum-connection-count> the smallest number of connections.

The following is the Hibernate configuration file for connection pool configuration:

<? xml version = '1 .0 'encoding =' UTF-8 '?>
<! DOCTYPE hibernate-configuration PUBLIC
"- / / Hibernate / Hibernate Configuration DTD 3.0 / / EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<hibernate-configuration>
<session-factory>

<! - Proxool connection pool load of classes - "
<property name="hibernate.connection.provider_class">
org.hibernate.connection.ProxoolConnectionProvider
</ property>

"! - Connection pool alias, that is, when you configure the connection pool from an alias -"
<property name="hibernate.proxool.pool_alias">
mysql
</ property>

"! - Connection pool file addresses -"
<property name="hibernate.proxool.xml">
config / proxool / proxool.xml
</ property>

"! - Whether run-time generated SQL output to the log for debugging -"
<property name="show_sql"> true </ property>

<mapping resource="com/jack/ssh/demo/bo/City.hbm.xml" />
<mapping resource="com/jack/ssh/demo/bo/Province.hbm.xml" />
</ session-factory>
</ hibernate-configuration>

Five . JNDI connection pool. Data source has been configured by the application services (such as the Web server), Hibernate needs to be done only through this data source JNDI name lookup. Application server connection pool outside appears as a data source JNDI binding, it is an instance of the class javax.jdbc.Datasource. As long as a Hibernate configuration file, such as:

hibernate.connection.datasource = java: / comp / env / jdbc / portal / / JNDI name of

hibernate.transaction.factory_class = org.hibernate.transaction.JTATransactionFactory

hibernate.transaction.manager_loopup_class = org.hibernate.transaction.JBossTransactionManagerLookup

hibernate.dialect = org.hibernate.dialect.MySQLDialect

分类:Java 时间:2008-11-18 人气:423
分享到:
blog comments powered by Disqus

相关文章

  • hibernate (three) connection pool configuration mysql 2009-05-15

    I spent three nights of the time, the hibernate connection pool with the three commonly used configuration of the study, in the hope that everyone has to help. The following are three types of connections to connect MySQl as an example. <!-- JDBC Dri

  • Tomcat6.x local database connection pool with mysql configuration 2010-10-22

    These days to do the project, the teacher let me responsible for the database connection pool, before the connection pool configuration is tomcat6.x, is global, but the teacher asked us to be partial, so that future changes to facilitate easy. Search

  • A similar DBCP data source (connection pool) and implementation (on) 2010-04-30

    During this time, is learning Hibernate, in the learning process, go back and go over JDBC, considerable gains. In fact, long time ago (sophomore), in contact with JDBC, and just felt very simple, and probably time for the software development was qu

  • [Transfer] DBCP connection pool: Principles and basic configuration 2011-02-23

    Turn on About six months ago, tried to get my colleagues solved a Commons-dbcp connection pool problem. Problems was more strange, but in fact is not particularly complex problems, understand the principles of DBCP, most of the problem is solved. In

  • jdbc connection pool easy package 2009-04-03

    The main methods of implementation are: The use of a container (LinkedList), initialization time set number of connections to generate Connection objects on the container Each subsequent time to obtain connections from the container to obtain, but th

  • Tomcat connection pool configured in MySql 2010-06-06

    As directly edit the server.xml configuration connection pool is prone to error, I guess tomcat version of the problem, not a version may have slightly different configuration, so I recommend using the data source in http://localhost/admin/ configura

  • jdbc ---- connection pool and data source 2010-09-23

    What is the data source ? Is used to create connections, and DriverManager The difference is that the data source (DataSource) His advantage is : Create the connection speed, by DataSource Parcels created after the Connection is , He's close method h

  • The mysql java connection pool 2010-12-03

    Can download the latest version from the official website http:// proxool. sourceforge.net Let Proxool connection pool support automatic re-connection project uses the connection pool is Proxool, have a problem running them if the connection pool, a

  • JDBC connection pool 2010-08-17

    Introduction In recent years, with the Internet / Intranet networking technology and the rapid development and the rapid worldwide spread, the computer Applications from traditional desktop applications to Web applications. Based on B / S (Browser /

  • JDBC database connection pool Statment 2010-09-08

    1. Foreword   database applications, in many software systems are often used, is indispensable for the development of supporting large-scale systems. But if not well managed database resources (such as: failure to make timely recovery of the databa

iOS 开发

Android 开发

Python 开发

JAVA 开发

开发语言

PHP 开发

Ruby 开发

搜索

前端开发

数据库

开发工具

开放平台

Javascript 开发

.NET 开发

云计算

服务器

Copyright (C) codeweblog.com, All Rights Reserved.

CodeWeblog.com 版权所有 黔ICP备15002463号-1

processed in 0.571 (s). 12 q(s)