大橙子网站建设,新征程启航
为企业提供网站建设、域名注册、服务器等服务
这篇文章主要介绍“Hibernate中怎么配置dbcp连接池”的相关知识,小编通过实际案例向大家展示操作过程,操作方法简单快捷,实用性强,希望这篇“Hibernate中怎么配置dbcp连接池”文章能帮助大家解决问题。
创新互联专注于南州晴隆网站建设服务及定制,我们拥有丰富的企业做网站经验。 热诚为您提供南州晴隆营销型网站建设,南州晴隆网站制作、南州晴隆网页设计、南州晴隆网站官网定制、微信小程序开发服务,打造南州晴隆网络公司原创品牌,更为您提供南州晴隆网站排名全网营销落地服务。
要将 DBCP 与 Hibernate 集成,您需要commons-dbcp.jar和commons-pool-1.5.4.jar。
文件:pom.xml
JBoss repository http://repository.jboss.org/nexus/content/groups/public/ org.hibernate hibernate-core 3.6.3.Final commons-dbcp commons-dbcp 1.4
要将 DBCP 与 Hibernate 集成,您需要创建一个“ 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; } }
现在,链接您的“ DBCPConnectionProvider ”并在“ hibernate.cfg.xml ”中定义 DBCP 属性,例如:
文件:hibernate.cfg.xml
oracle.jdbc.driver.OracleDriver jdbc:oracle:thin:@localhost:1521:MKYONG mkyong password org.hibernate.dialect.Oracle10gDialect MKYONG true com.mkyong.util.DBCPConnectionProvider 8 20 20 0
完成,运行它并查看以下输出:
关于“Hibernate中怎么配置dbcp连接池”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识,可以关注创新互联行业资讯频道,小编每天都会为大家更新不同的知识点。