c3p0的几种使用方式(原文地址: https://my.oschina.net/liangtee/blog/101047)
package com.c3p0.test; import java.sql.Connection;
import java.sql.SQLException;
import java.beans.PropertyVetoException;
import com.mchange.v2.c3p0.ComboPooledDataSource; public class DBPool {
private static DBPool dbPool;
private ComboPooledDataSource dataSource; static {
dbPool = new DBPool();
} public DBPool() {
try {
dataSource = new ComboPooledDataSource();
dataSource.setUser("id");
dataSource.setPassword("pw");
dataSource.setJdbcUrl(
"jdbc:mysql://127.0.0.1:3306/test?autoReconnect=true&useUnicode=true&characterEncoding=GB2312");
dataSource.setDriverClass("com.mysql.jdbc.Driver");
dataSource.setInitialPoolSize(2);
dataSource.setMinPoolSize(1);
dataSource.setMaxPoolSize(10);
dataSource.setMaxStatements(50);
dataSource.setMaxIdleTime(60);
} catch (PropertyVetoException e) {
throw new RuntimeException(e);
}
} public final static DBPool getInstance() {
return dbPool;
} public final Connection getConnection() {
try {
return dataSource.getConnection();
} catch (SQLException e) {
throw new RuntimeException("无法从数据源获取连接", e);
}
} public static void main(String[] args) throws SQLException {
Connection con = null;
try {
con = DBPool.getInstance().getConnection();
} catch (Exception e) {
} finally {
if (con != null)
con.close();
}
} }
以上是直接在combopooleddatasource里面直接设置属性值
<?xml version="1.0" encoding="UTF-8"?>
<c3p0-config>
<default-config>
<property name="user">test</property>
<property name="password">test</property>
<property name="jdbcUrl">jdbc:oracle:thin:@localhost:1521:orcl</property>
<property name="driverClass">oracle.jdbc.OracleDriver</property>
<property name="automaticTestTable">con_test</property>
<property name="checkoutTimeout">30000</property>
<property name="idleConnectionTestPeriod">30</property>
<property name="initialPoolSize">10</property>
<property name="maxIdleTime">30</property>
<property name="maxPoolSize">100</property>
<property name="minPoolSize">10</property>
<property name="maxStatements">200</property> <user-overrides user="test-user">
<property name="maxPoolSize">10</property>
<property name="minPoolSize">1</property>
<property name="maxStatements">0</property>
</user-overrides>
</default-config> <!-- This app is massive! -->
<named-config name="intergalactoApp">
<property name="acquireIncrement">50</property>
<property name="initialPoolSize">100</property>
<property name="minPoolSize">50</property>
<property name="maxPoolSize">1000</property> <!-- intergalactoApp adopts a different approach to configuring statement
caching -->
<property name="maxStatements">0</property>
<property name="maxStatementsPerConnection">5</property> <!-- he's important, but there's only one of him -->
<user-overrides user="master-of-the-universe">
<property name="acquireIncrement">1</property>
<property name="initialPoolSize">1</property>
<property name="minPoolSize">1</property>
<property name="maxPoolSize">5</property>
<property name="maxStatementsPerConnection">50</property>
</user-overrides>
</named-config> <named-config name="userApp">
<property name="driverClass">com.mysql.jdbc.Driver</property>
<property name="jdbcUrl">jdbc:mysql://localhost:3306/test</property>
<property name="user">root</property>
<property name="password">123456</property>
<property name="acquireIncrement">5</property>
<property name="initialPoolSize">10</property>
<property name="minPoolSize">10</property>
<property name="maxPoolSize">20</property>
<property name="maxStatements">0</property>
<property name="maxStatementsPerConnection">5</property>
<!-- he's important, but there's only one of him -->
<user-overrides user="master-of-the-universe">
<property name="acquireIncrement">1</property>
<property name="initialPoolSize">1</property>
<property name="minPoolSize">1</property>
<property name="maxPoolSize">5</property>
<property name="maxStatementsPerConnection">50</property>
</user-overrides>
</named-config>
</c3p0-config>
package com.c3p0.test; import java.sql.Connection;
import java.sql.SQLException;
import javax.sql.DataSource; import com.mchange.v2.c3p0.ComboPooledDataSource; public class DbConnection {
private static DataSource dataSource;
static {
dataSource = new ComboPooledDataSource("userApp");
} public static Connection getConnectioon() throws SQLException {
return dataSource.getConnection();
}
}
以上是使用配置文件的形式
package com.c3p0.test; import java.sql.Connection;
import java.sql.SQLException;
import java.util.Properties; import com.mchange.v2.c3p0.ComboPooledDataSource;
import com.mchange.v2.c3p0.DataSources; public final class ConnectionManager {
private static ConnectionManager instance; public ComboPooledDataSource ds;
private static String c3p0Properties = "c3p0.properties"; private ConnectionManager() throws Exception {
Properties p = new Properties();
p.load(this.getClass().getResourceAsStream(c3p0Properties));
ds = new ComboPooledDataSource();
ds.setUser(p.getProperty("user"));
ds.setPassword(p.getProperty("password"));
ds.setJdbcUrl(p.getProperty("jdbcUrl"));
ds.setDriverClass(p.getProperty("driverClass"));
ds.setInitialPoolSize(Integer.parseInt(p.getProperty("initialPoolSize")));
ds.setMinPoolSize(Integer.parseInt(p.getProperty("minPoolSize")));
ds.setMaxPoolSize(Integer.parseInt(p.getProperty("maxPoolSize")));
ds.setMaxStatements(Integer.parseInt(p.getProperty("maxStatements")));
ds.setMaxIdleTime(Integer.parseInt(p.getProperty("maxIdleTime")));
} public static final ConnectionManager getInstance() {
if (instance == null) {
try {
instance = new ConnectionManager();
} catch (Exception e) {
e.printStackTrace();
}
}
return instance;
} public synchronized final Connection getConnection() {
try {
return ds.getConnection();
} catch (SQLException e) {
e.printStackTrace();
}
return null;
} protected void finalize() throws Throwable {
DataSources.destroy(ds); // 关闭datasource
super.finalize();
}
}
以上使用properties属性文件设置属性
c3p0的几种使用方式(原文地址: https://my.oschina.net/liangtee/blog/101047)的更多相关文章
- 【c3p0】 C3P0的三种配置方式以及基本配置项详解
数据库连接池C3P0框架是个非常优异的开源jar,高性能的管理着数据源,这里只讨论程序本身负责数据源,不讨论容器管理. ---------------------------------------- ...
- spring 2种下载方式 下载地址 download 地址
spring 在官网只提供 maven 的下载方式,把zip方式的不再提供,两种方法下载: 1.想找回以前版本的spring zip包,如果知道版本号,那么直接在google里输入 ” spring ...
- JGit与远程仓库链接使用的两种验证方式(ssh和https)
JGit是使用JAVA的API来操控Git仓库的库,由Eclipse公司维护.他提供的API分成两个层次,底层命令和高层命令.底层API是直接作用于低级的仓库对象,高层的API是一个面向普通用户级别功 ...
- Windows Server 2008 R2 /2012 修改密码策略(摘抄 原文地址 https://www.cnblogs.com/mili3/p/7799347.html)
今天建了域环境,在添加新用户的时候,发现用简单的密码时域安全策略提示密码复杂度不够,于是我就想在域安全策略里面把密码复杂度降低一点. 问题: 在“管理工具 >> 本地安全策略 > ...
- nmap使用命令(转载)原文地址https://www.jianshu.com/p/4030c99fcaee
- 转 VMware虚拟机三种联网方式(图文详细解说)
原文地址https://blog.csdn.net/lucienduan/article/details/38233147 VMware三种网络模式联网 首先说一下VMware的几个虚拟设备 安装了V ...
- 魔改swagger:knife4j的另外一种打开方式
之前公司使用了swagger作为文档管理工具,原生的swagger-ui非常丑,之后就用了开源项目 萧明 / knife4j 的swagger组件进行了swagger渲染,改造之后界面漂亮多了,操作也 ...
- Ajax的get和post两种请求方式区别
Ajax的get和post两种请求方式区别 (摘录):http://ip-10000.blog.sohu.com/114437748.html 解get和post的区别. 1. get是把参数数据队列 ...
- iOS活动倒计时的两种实现方式
代码地址如下:http://www.demodashi.com/demo/11076.html 在做些活动界面或者限时验证码时, 经常会使用一些倒计时突出展现. 现提供两种方案: 一.使用NSTime ...
随机推荐
- 【学习】Python解决汉诺塔问题
参考文章:http://www.cnblogs.com/dmego/p/5965835.html 一句话:学程序不是目的,理解就好:写代码也不是必然,省事最好:拿也好,查也好,解决问题就好! ...
- Mybatis学习4——核心文件sqlMapperConfig.xml属性
1.外部文件jdbc.properties jdbc.driver=com.mysql.jdbc.Driver jdbc.url=jdbc:mysql://localhost:3306/mybatis ...
- postgresql----COPY之表与文件之间的拷贝
postgresql提供了COPY命令用于表与文件(和标准输出,标准输入)之间的相互拷贝,copy to由表至文件,copy from由文件至表. 示例1.将整张表拷贝至标准输出 test=# cop ...
- 白鹭引擎 - 矢量绘图 ( graphics )
class Main extends egret.DisplayObjectContainer { /** * Main 类构造器, 初始化的时候自动执行, ( 子类的构造函数必须调用父类的构造函数 ...
- [ 记录 ] Vue 对象数组中一项数据改变,页面不更新
问题描述:将data中数据列表渲染到页面,循环生成 el-switch,点击页面中 el-switch 后数组中某项值改变,但是页面不更新 数据格式如下 export default{ data(){ ...
- PHP轻量级框架 Slim 使用(一)
安装参照文档:https://wizardforcel.gitbooks.io/slim3-doc/content/1.html 项目目录 其中主要业务操作在app目录中完成,可根据需求划分 我这里分 ...
- redis内部数据结构和外部数据结构揭秘
Redis有哪些数据结构? 字符串String.字典Hash.列表List.集合Set.有序集合SortedSet. 很多人面试时都遇到过这种场景吧? 其实除了上面的几种常见数据结构,还需要加上数据结 ...
- java实现生成二维码
package com.cn.test; import java.awt.Graphics2D; import java.awt.geom.AffineTransform; import java.a ...
- shell编程小技巧(命令篇)
本文主要介绍shell编程中一些好用的命令或者一些常见命令但比较少用却又好用的参数,目的是希望可以提高编码效率. df命令 常用命令 df / df -k / df -m / df -H / df - ...
- delphi Drag and Drop sample 鼠标拖放操作实例
Drag and Drop is a common operation that makes the interface user friendly: a user can drag/drop inf ...