Java framework --- hibernate connection pool, java --- hibernate
Hibernate supports third-party connection pools. the officially recommended connection pools are C3P0, Proxool, and DBCP. Note the following three points when configuring the connection pool:
1. Apche's DBCP is supported in Hibernate2, but it is no longer recommended in Hibernate3. The official explanation is that this connection pool has defects. If you need to use DBCP in Hibernate3 for some reason, we recommend that you use the JNDI method.
2. By default (that is, if the connection pool is not configured), Hibernate uses the built-in connection pool. However, this connection pool has poor performance and many bugs (I had been plagued by eight-hour connection suspension issues in the Mysql environment). Therefore, the official recommendation is only for use in the development environment.
III. The namespaces of Hibernate2 and Hibernate3 have changed. For example, when C3P0 is configured, provider_class uses net. sf. hibernate. connection. C3P0ConnectionProvider in Hibernate2 and org. hibernate. connection. C3P0ConnectionProvider in Hibernate3.
Below are several common connection pool configurations in the Hibernate environment:
1. Hibernate default connection pool
<? Xml version = '1. 0' encoding = 'utf-8'?> <! DOCTYPE hibernate-configuration PUBLIC "-// Hibernate/Hibernate Configuration DTD/EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
2. C3P0 connection Configuration
<? Xml version = '1. 0' encoding = 'utf-8'?> <! DOCTYPE hibernate-configuration PUBLIC "-// Hibernate/Hibernate Configuration DTD/EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
3. proxool connection pool
(1) first write the proxool configuration file, file name: proxool. xml (usually with hibernate. cfg. in this example, the MYSQL database is configured and the database name is schoolproject.
<? 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 of the connection pool --> <alias> DBPool </alias> <! -- Proxool can only manage connections generated by itself --> <driver-url> jdbc: mysql: // localhost: 3306/schoolproject? UseUnicode = true & amp; characterEncoding = UTF8 </driver-url> <! -JDBC driver --> <driver-class> com. mysql. jdbc. driver </driver-class> <driver-properties> <property name = "user" value = "root"/> <property name = "password" value = ""/> </driver-properties> <! -- The proxool automatically detects the time interval (in milliseconds) of each connection status, and immediately returns the result when detecting idle connections, destroy timeout -->
(2) configure the hibernate. cfg. xml file
<? Xml version = '1. 0' encoding = 'utf-8'?> <! DOCTYPE hibernate-configuration PUBLIC "-// Hibernate/Hibernate Configuration DTD/EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
- Hibernate. connection. provider_class defines the Hibernate connection loading class. Here, the Proxool connection pool uses this class. Different connection pools have different loading classes. You can refer to the Hibernate documentation to obtain relevant information.
- Hibernate. proxool. pool_alias here is the alias of the connection pool we mentioned above.
- Hibernate. proxool. xml declares the location of the configuration file for the connection pool to Hibernate. You can use relative or absolute paths. When using relative paths, be sure to be within the Path range! Otherwise, an exception is thrown.
- Dialect is the dialect for declaring SQL statements
- Show_ SQL defines whether to display the SQL language generated by Hibernate. It is usually set to true in the debugging phase and then changed to false after completion, which is conducive to debugging.
- <Mapping> resource file ing