使用apache dbcp连接池
2009-11-18 14:24
281 查看
最近整理了一下项目的数据库连接问题,使用apache dbcp连接池:
1、配置JNDI数据源:
在META-INF下新建context.xml,记得是META-INF下,内容
<?xml version="1.0" encoding="UTF-8"?> <Context> <Resource name="jdbc/MySqlPooledDS" auth="Container" type="javax.sql.DataSource" driverClassName="com.mysql.jdbc.Driver" url="jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=utf-8" username="root" password="root" /> </Context>
2、配置连接池
public static DataSource setupDataSource() throws Exception{ // First, we'll need a ObjectPool that serves as the actual pool of connections. GenericObjectPool pool = new GenericObjectPool(null, 100); pool.setMaxIdle(16); pool.setMaxWait(5000); pool.setMinIdle(8); javax.naming.InitialContext ctx = new javax.naming.InitialContext(); Context context = (Context) ctx.lookup("java:comp/env"); DataSource ds = (DataSource) context.lookup("jdbc/MySqlPooledDS"); // Next, we'll create a ConnectionFactory that the pool will use to create Connections. ConnectionFactory connectionFactory = new DataSourceConnectionFactory(ds); // Now we'll create the PoolableConnectionFactory, // which wraps the "real" Connections created by the ConnectionFactory with // the classes that implement the pooling functionality. PoolableConnectionFactory poolableConnectionFactory = new PoolableConnectionFactory(connectionFactory, pool, null, null, false, true); //Finally, we create the PoolingDriver itself, passing in the object pool we created. DataSource dataSource = new PoolingDataSource(pool); return dataSource; }
3、利用创建好的连接池取得连接
DataSource dataSource = setupDataSource(); Connection conn = dataSource.getConnection();
阅读更多
相关文章推荐
- 使用Apache DBCP连接池重构DBUtility
- JDBC实例--工具类升级,使用Apache DBCP连接池重构DBUtility,让连接数据库更有效,更安全
- mysql数据库连接池使用(一)dbcp方式的配置
- 使用c3p0与DBCP连接池,造成的MySql 8小时问题解决方案
- java dbcp连接池的使用
- 使用c3p0与DBCP连接池,造成的MySql 8小时问题解决方式
- 使用dbcp 连接池
- apache连接池使用
- DBCP连接池的使用
- DBCP连接池使用问题
- 使用DBCP创建连接池
- Java 使用 DBCP mysql 连接池 做数据库操作
- Java 连接池(使用DBCP与C3P0)
- java连接池参数配置 Apache-DBCP
- 用apache的dbcp来建立独立的数据库连接池(db connection pool)
- 连接池DBCP的使用(JAVA源码)
- tomcat 连接池泄露的问题:org.apache.commons.dbcp.SQLNestedException: Cannot get a connection, pool exhausted, cause:java.util.NoSuchEleme
- Spring整合Hibernate 使用dbcp连接池
- 使用c3p0与DBCP连接池,造成的MySql 8小时问题解决方案