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-

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
  • del.icio.us
  • StumbleUpon
  • Digg
  • TwitThis
  • Mixx
  • Technorati
  • Facebook
  • NewsVine
  • Reddit
  • Google
  • LinkedIn
  • YahooMyWeb

Related Posts of Hibernate and connection pooling

  • The EJB3 Persistence

    EJB3 persistence with Hibernate is very similar to the mechanism: Environment: Server: JBOSS5.0 Database: MySQL5.0 1. Set up a data source First of all, in jboss-5.0.0.GA \ server \ default \ deploy, the establishment of a database used to connect the dat

  • can not be represented as java.sql.Timestamp

    Development of procedures for the use of hibernate when, some time there is no need to fill in the fields, but after the hibernate query time reported "Java.sql.SQLException: Value'0000-00-00 'can not be represented as java.sql.Timestamp ...

  • spring struts2.0 hibernate environmental structures .. despair carried out more than one hour only with good.

    http://www.qqread.com/java/2008/06/f413762.html Look here. . Note added myeclipse support spring when necessary add the commons-dbcp database connection pool package. And to add hibernate support. . Finally add struts2 support. . Oh the lazy point. . . fu

  • Spring2.0 + hibernate3.1 + log4j + mysql demo

    applicationContext.xml Non-attachment jar package, necessary friends can send an email to todd.liangt @ gmail.com

  • Struts2 + hibernate + spring problem user log in

    dao layer services layer action jsp <tr> <td align="center"> <b> user name: </ b> </ td> <td> <s: textfield name = "czyNumber" cssClass = "textstyle" theme = "simple" size = &q

  • Hibernate secondary cache

    Hibernate cache: 2-bit cache, also known as process-level cache or SessionFactory level cache, secondary cache can be shared by all of the session Cache configuration and the use of: Will echcache.xml (the document code in hibernate package directory ...

blog comments powered by Disqus
Recent Entries
Tag Cloud
Random Entries