Hibernate and connection pooling

分类:Java 2008-11-18 来源:CodeWeblog.com 人气:422

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-

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. ->
</ alias>
jdbc: mysql: / / localhost: 3306/jackdemo
</ driver-url>
</ driver-class>
<property name="user" value="root" />
<property name="password" value="jack" />
</ driver-properties>
</ house-keeping-sleep-time>
</ proxool.simultaneous-build-throttle>
</ prototype-count>
</ maximum-connection-count>
</ 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"


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

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

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 Drive

  • 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 quite s

  • [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 this

  • 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 this ha

  • 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

    什么是数据源? 就是用来创建连接的,和DriverManager一样,只不过数据源(DataSource) 他的优势是:创建连接的速度快,通过DataSource创建的Connection是被包裹后的,他的close方法已经被修改,他close是放到池里面去,而数据源内部会有一个连接池,而我们的程序之和数据源打交道,不会直接访问到连接池,因为的池是包内有好,而我们的程序一般和开源组织提供的类肯定不是在同一个包下,所以我们访问不到。 public class MyDataSource{ priva

  • 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 netwo

  • 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 / Serve

  • 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 开发



PHP 开发

Ruby 开发






Javascript 开发

.NET 开发



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

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

processed in 0.193 (s). 14 q(s)