- 浏览: 42283 次
- 性别:
- 来自: Edmonton
文章分类
最新评论
转载 http://wiki.apache.org/commons/DBCP/Hibernate
2010-01-27 Version 5 2005-01-27 Version1
About Hibernate & DBCP
Hibernate 2 includes a limited DBCP ConnectionProvider . Not all features of DBCP were supported and with Hibernate3 this limited implementation was deprecated.
There is an implementation on this page. The original author said:
- The implementation below supports all DBCP features and is a drop in replacement for the default version.
However this is untrue. It fails to support:
-
Almost all hibernate.dbcp.ps.* properties
-
hibernate.dbcp.whenExhaustedAction
- ... maybe more?
so personally I feel rather dubious about using it.
This provider can be used on Hibernate 2 & 3 (with some import statements adjustments).
DBCPConnectionProvider
/*
* Copyright 2004 The Apache Software Foundation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.hibernate.connection;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Iterator;
import java.util.Properties;
import org.apache.commons.dbcp.BasicDataSource;
import org.apache.commons.dbcp.BasicDataSourceFactory;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.HibernateException;
import org.hibernate.cfg.Environment;
/**
* <p>A connection provider that uses an Apache commons DBCP connection pool.</p>
*
* <p>To use this connection provider set:<br>
* <code>hibernate.connection.provider_class org.hibernate.connection.DBCPConnectionProvider</code></p>
*
* <pre>Supported Hibernate properties:
* hibernate.connection.driver_class
* hibernate.connection.url
* hibernate.connection.username
* hibernate.connection.password
* hibernate.connection.isolation
* hibernate.connection.autocommit
* hibernate.connection.pool_size
* hibernate.connection (JDBC driver properties)</pre>
* <br>
* All DBCP properties are also supported by using the hibernate.dbcp prefix.
* A complete list can be found on the DBCP configuration page:
* <a href="http://jakarta.apache.org/commons/dbcp/configuration.html">http://jakarta.apache.org/commons/dbcp/configuration.html</a>.
* <br>
* <pre>Example:
* hibernate.connection.provider_class org.hibernate.connection.DBCPConnectionProvider
* hibernate.connection.driver_class org.hsqldb.jdbcDriver
* hibernate.connection.username sa
* hibernate.connection.password
* hibernate.connection.url jdbc:hsqldb:test
* hibernate.connection.pool_size 20
* hibernate.dbcp.initialSize 10
* hibernate.dbcp.maxWait 3000
* hibernate.dbcp.validationQuery select 1 from dual</pre>
*
* <p>More information about configuring/using DBCP can be found on the
* <a href="http://jakarta.apache.org/commons/dbcp/">DBCP website</a>.
* There you will also find the DBCP wiki, mailing lists, issue tracking
* and other support facilities</p>
*
* @see org.hibernate.connection.ConnectionProvider
* @author Dirk Verbeeck
*/
public class DBCPConnectionProvider implements ConnectionProvider {
private static final Log log = LogFactory.getLog(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) iter.next();
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()) {
log.debug("Creating a DBCP BasicDataSource with the following DBCP factory properties:");
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.fatal(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() + ")");
}
}
}
发表评论
-
[L符号是从哪里来的?
2013-07-09 07:13 0参见: http://stackoverflow.com/q ... -
如何处理Hibernate的 fails to correctly determine parameter type异常
2011-11-29 04:40 1117在Hibernate的HQL 中想使用 ... -
Debugging JSPs in Eclipse using OC4J
2010-05-07 06:22 1531To set break points and debug J ... -
找回Tomcat6中Administration Web Application
2010-03-31 03:18 2922找回Tomcat6中Administration Web Ap ...
相关推荐
DBCP 资料,配置的解释,属性,以及一些BUG,在项目中都出现过 比如:web程序启动后会注册JDBC驱动,关闭不了,tomcat7.(版本在什么之上会自动检测,强行关闭导致报错)
spring和hibernate需要的jar,直接导入项目中就可以,解决Class 'org.springframework.orm.hibernate3.LocalSessionFactoryBean' not found和BasicDataSource not found错误
博文链接:https://pantao.iteye.com/blog/142280
DBCP是Apache提供的一款开源免费的数据库连接池!Hibernate3.0之后不再对DBCP提供支持!因为Hibernate声明DBCP有致命的缺欠!DBCP因为Hibernate的这一毁谤很是生气,并且说自己没有缺欠。
- Hibernate:DBCP可以作为Hibernate的数据源,提供连接池服务。在Hibernate的配置文件中,可以通过指定DBCP的数据源类来启用连接池,提高数据操作的效率。 - Spring:Spring框架提供了对各种数据源的支持,包括...
这三个技术在实际开发中经常结合使用,DBCP提供高效稳定的数据库连接,Hibernate简化数据库操作,而JSON则作为数据交换的中间格式,实现了数据的透明传输。通过熟练掌握这些工具和技术,开发者能够构建出更加高效、...
1. 添加依赖:在项目中引入Spring、Hibernate、JPA和DBCP2的相关库文件,例如lib-spring 4.0.6、Hibernate 4.3.5和JPA的依赖。 2. 配置数据源:在Spring的配置文件中,使用DBCP2的数据源bean,设置数据库连接参数如...
为了提高性能,通常会使用数据库连接池,如C3P0、HikariCP或Apache DBCP。连接池管理数据库连接的创建和回收,减少创建和关闭连接的开销。 ### Hibernate与MySQL连接示例 ```xml <hibernate-configuration> ......
本主题将深入探讨如何整合Hibernate ORM框架与Spring框架,并利用Apache DBCP(BasicDataSource)连接池来高效地管理数据库连接。让我们逐一解析这些知识点。 首先,Hibernate是一个流行的Java持久化框架,它简化了...
6. **Hibernate不推荐使用DBCP**:Hibernate开发团队不建议在Hibernate中直接使用DBCP连接池,可能是因为其他连接池如C3P0或HikariCP等提供了更好的性能和稳定性。 【标签】中的知识点: 7. **JNDI资源**:在...
Hibernate作为一款强大的对象关系映射(ORM)框架,它提供了与多种连接池的集成,如C3P0、DBCP、HikariCP等。本篇将详细介绍如何在Hibernate中配置这些连接池,并探讨其工作原理和优势。 **一、C3P0连接池** C3P0...
<property name="hibernate.proxoolproxool.hibernate.dbcp.timeBetweenEvictionRunsMillis">3000 <property name="hibernate.proxool.hibernate.proxool.driverClassName">com.mysql.jdbc.Driver</property> ...
5. **hibernate**:Hibernate是一个强大的对象关系映射(ORM)框架,虽然它有自己的连接池管理(C3P0或HikariCP),但也可以与commons-dbcp集成,通过配置来使用DBCP作为数据连接池。 **标签解析:** - **commons-...
DBCP,全称为Apache Commons DBCP,是Apache软件基金会提供的一个开源数据库连接池组件。...在实际开发中,结合其他数据库操作框架,如MyBatis或Hibernate,DBCP能更好地发挥其优势,实现高效的数据访问。
为了使用 DBCP 连接池,我们需要在 Hibernate 配置文件(hibernate.cfg.xml)中添加以下配置: ``` <property name="hibernate.dbcp.maxActive">20 <property name="hibernate.dbcp.maxIdle">10 <property name="...
在Java Web开发中,SSH(Struts2、Spring、Hibernate)框架是一个常用的企业级应用框架组合,它提供了模型-视图-控制器(MVC)架构模式,并整合了强大的持久层框架Hibernate和依赖注入框架Spring。在搭建SSH框架时,...
这两个库经常与SSH(Spring、Struts和Hibernate)框架一起使用,是Java开发中的常用工具。 "commons-pool"是Apache Commons的一个子项目,主要提供对象池服务。对象池设计模式允许开发者高效地管理资源,通过复用...
在Spring与Hibernate整合时,DBCP作为数据源,Commons Pool作为连接池的底层实现,共同确保了数据库连接的高效管理和复用。Spring通过其IoC(Inversion of Control)容器管理这两个库的配置,并提供了一个方便的`org...
在给定的描述中提到,Hibernate ORM框架就曾使用DBCP作为其默认的连接池实现。 DBCP连接池的工作原理是预先创建一定数量的数据库连接,并将这些连接保存在一个池中。当应用程序需要与数据库交互时,不再直接创建新...
这是一个整合了多个核心技术的Java Web开发资源包,涵盖了Spring 3、Hibernate 4、Struts 2、DBCP、MySQL、JSON、Ehcache以及DOM4J等组件。以下将详细解析这些技术及其在Web开发中的应用。 1. **Spring框架**:...