久久精品人人爽,华人av在线,亚洲性视频网站,欧美专区一二三

Hibernate中怎么配置dbcp連接池

179次閱讀
沒有評論

共計 7105 個字符,預(yù)計需要花費 18 分鐘才能閱讀完成。

這篇文章主要介紹“Hibernate 中怎么配置 dbcp 連接池”的相關(guān)知識,丸趣 TV 小編通過實際案例向大家展示操作過程,操作方法簡單快捷,實用性強(qiáng),希望這篇“Hibernate 中怎么配置 dbcp 連接池”文章能幫助大家解決問題。

1. 獲取 DBCP jar

要將 DBCP 與 Hibernate 集成,您需要 commons-dbcp.jar 和 commons-pool-1.5.4.jar。

文件:pom.xml

project ... 
 repositories 
 repository 
 id JBoss repository /id 
 url http://repository.jboss.org/nexus/content/groups/public/ /url 
 /repository 
 /repositories 
 dependencies 
 dependency 
 groupId org.hibernate /groupId 
 artifactId hibernate-core /artifactId 
 version 3.6.3.Final /version 
 /dependency 
 dependency 
 groupId commons-dbcp /groupId 
 artifactId commons-dbcp /artifactId 
 version 1.4 /version 
 /dependency 
 /dependencies /project

2.DBCPConnectionProvider

要將 DBCP 與 Hibernate 集成,您需要創(chuàng)建一個“DBCPConnectionProvider”類。

文件:DBCPConnectionProvider.java

package com.mkyong.util;import java.io.PrintWriter;import java.io.StringWriter;import java.sql.Connection;import java.sql.SQLException;import java.util.Iterator;import java.util.Map;import java.util.Properties;import org.apache.commons.dbcp.BasicDataSource;import org.apache.commons.dbcp.BasicDataSourceFactory;import org.hibernate.HibernateException;import org.hibernate.cfg.Environment;import org.hibernate.connection.ConnectionProvider;import org.hibernate.connection.ConnectionProviderFactory;import org.slf4j.Logger;import org.slf4j.LoggerFactory;public class DBCPConnectionProvider implements ConnectionProvider { private static final Logger log = LoggerFactory
 .getLogger(DBCPConnectionProvider.class); private static final String PREFIX =  hibernate.dbcp. private BasicDataSource ds; // Old Environment property for backward-compatibility (property removed in
 // Hibernate3)
 private static final String DBCP_PS_MAXACTIVE =  hibernate.dbcp.ps.maxActive // Property doesn t exists in Hibernate2
 private static final String AUTOCOMMIT =  hibernate.connection.autocommit public void configure(Properties props) throws HibernateException { try {
 log.debug( Configure DBCPConnectionProvider // DBCP properties used to create the BasicDataSource
 Properties dbcpProperties = new Properties(); // DriverClass   url
 String jdbcDriverClass = props.getProperty(Environment.DRIVER);
 String jdbcUrl = props.getProperty(Environment.URL);
 dbcpProperties.put(driverClassName , jdbcDriverClass);
 dbcpProperties.put(url , jdbcUrl); // Username / password
 String username = props.getProperty(Environment.USER);
 String password = props.getProperty(Environment.PASS);
 dbcpProperties.put(username , username);
 dbcpProperties.put(password , password); // Isolation level
 String isolationLevel = props.getProperty(Environment.ISOLATION); if ((isolationLevel != null)
  (isolationLevel.trim().length()   0)) {
 dbcpProperties.put( defaultTransactionIsolation ,
 isolationLevel);
 } // Turn off autocommit (unless autocommit property is set)
 String autocommit = props.getProperty(AUTOCOMMIT); if ((autocommit != null)   (autocommit.trim().length()   0)) {dbcpProperties.put( defaultAutoCommit , autocommit);
 } else {
 dbcpProperties.put( defaultAutoCommit ,
 String.valueOf(Boolean.FALSE));
 } // Pool size
 String poolSize = props.getProperty(Environment.POOL_SIZE); if ((poolSize != null)   (poolSize.trim().length()   0)
  (Integer.parseInt(poolSize)   0)) {dbcpProperties.put( maxActive , poolSize);
 } // Copy all  driver  properties into  connectionProperties 
 Properties driverProps = ConnectionProviderFactory
 .getConnectionProperties(props); if (driverProps.size()   0) {StringBuffer connectionProperties = new StringBuffer(); for (Iterator iter = driverProps.entrySet().iterator(); iter
 .hasNext();) {Map.Entry entry = (Map.Entry) iter.next();
 String key = (String) entry.getKey();
 String value = (String) entry.getValue();
 connectionProperties.append(key).append(=).append(value); if (iter.hasNext()) {
 connectionProperties.append( 
 dbcpProperties.put( connectionProperties ,
 connectionProperties.toString());
 } // Copy all DBCP properties removing the prefix
 for (Iterator iter = props.entrySet().iterator(); iter.hasNext();) {Map.Entry entry = (Map.Entry) iter.next();
 String key = (String) entry.getKey(); if (key.startsWith(PREFIX)) {String property = key.substring(PREFIX.length());
 String value = (String) entry.getValue();
 dbcpProperties.put(property, value);
 } // Backward-compatibility
 if (props.getProperty(DBCP_PS_MAXACTIVE) != null) {
 dbcpProperties.put( poolPreparedStatements ,
 String.valueOf(Boolean.TRUE));
 dbcpProperties.put( maxOpenPreparedStatements ,
 props.getProperty(DBCP_PS_MAXACTIVE));
 } // Some debug info
 if (log.isDebugEnabled()) {StringWriter sw = new StringWriter();
 dbcpProperties.list(new PrintWriter(sw, true));
 log.debug(sw.toString());
 } // Let the factory create the pool
 ds = (BasicDataSource) BasicDataSourceFactory
 .createDataSource(dbcpProperties); // The BasicDataSource has lazy initialization
 // borrowing a connection will start the DataSource
 // and make sure it is configured correctly.
 Connection conn = ds.getConnection();
 conn.close(); // Log pool statistics before continuing.
 logStatistics();} catch (Exception e) {
 String message =  Could not create a DBCP pool 
 log.error(message, e); if (ds != null) { try {ds.close();
 } catch (Exception e2) { // ignore
 ds = null;
 } throw new HibernateException(message, e);
 log.debug(Configure DBCPConnectionProvider complete} public Connection getConnection() throws SQLException {
 Connection conn = null; try {conn = ds.getConnection();
 } finally {logStatistics();
 } return conn;
 } public void closeConnection(Connection conn) throws SQLException { try {conn.close();
 } finally {logStatistics();
 } public void close() throws HibernateException {
 log.debug( Close DBCPConnectionProvider 
 logStatistics(); try { if (ds != null) {ds.close();
 ds = null;
 } else {log.warn( Cannot close DBCP pool (not initialized) 
 } catch (Exception e) {throw new HibernateException( Could not close DBCP pool , e);
 log.debug(Close DBCPConnectionProvider complete} protected void logStatistics() { if (log.isInfoEnabled()) {log.info( active:   + ds.getNumActive() +   (max:  
 + ds.getMaxActive() + )   +  idle:   + ds.getNumIdle()
 +  (max:   + ds.getMaxIdle() +  ) 
 } public boolean supportsAggressiveRelease() { return false;}

3. 在 hibernate.cfg.xml 中配置 DBCP

現(xiàn)在,鏈接您的“DBCPConnectionProvider”并在“hibernate.cfg.xml”中定義 DBCP 屬性,例如:

文件:hibernate.cfg.xml

 ?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
  property name= hibernate.connection.driver_class oracle.jdbc.driver.OracleDriver /property
  property name= hibernate.connection.url jdbc:oracle:thin:@localhost:1521:MKYONG /property
  property name= hibernate.connection.username mkyong /property
  property name= hibernate.connection.password password /property
  property name= hibernate.dialect org.hibernate.dialect.Oracle10gDialect /property
  property name= hibernate.default_schema MKYONG /property
  property name= show_sql true /property  
  property name= hibernate.connection.provider_class
com.mkyong.util.DBCPConnectionProvider  /property
  property name= hibernate.dbcp.initialSize 8 /property
  property name= hibernate.dbcp.maxActive 20 /property
  property name= hibernate.dbcp.maxIdle 20 /property
  property name= hibernate.dbcp.minIdle 0 /property  
  mapping  >4. 運(yùn)行,輸出

完成,運(yùn)行它并查看以下輸出:

關(guān)于“Hibernate 中怎么配置 dbcp 連接池”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識,可以關(guān)注丸趣 TV 行業(yè)資訊頻道,丸趣 TV 小編每天都會為大家更新不同的知識點。

正文完
 
丸趣
版權(quán)聲明:本站原創(chuàng)文章,由 丸趣 2023-07-17發(fā)表,共計7105字。
轉(zhuǎn)載說明:除特殊說明外本站除技術(shù)相關(guān)以外文章皆由網(wǎng)絡(luò)搜集發(fā)布,轉(zhuǎn)載請注明出處。
評論(沒有評論)
主站蜘蛛池模板: 东兴市| 青铜峡市| 香格里拉县| 忻城县| 常熟市| 茂名市| 雷波县| 渑池县| 聊城市| 灌阳县| 虞城县| 环江| 泾川县| 绵竹市| 凉城县| 邵阳县| 天镇县| 南陵县| 麻城市| 白水县| 泰州市| 全椒县| 随州市| 洞口县| 施秉县| 安龙县| 金川县| 梓潼县| 化州市| 永修县| 通化县| 昌黎县| 中宁县| 河源市| 松滋市| 承德县| 刚察县| 晋江市| 巴楚县| 长宁区| 贡嘎县|