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 ...
随机推荐
- <转载>AWS 基础知识
什么是AWS? Amazon Web Services (AWS) , 其实就是 亚马逊提供的专业云计算服务.其提供服务包括:亚马逊弹性计算网云(Amazon EC2).亚马逊简单储存服务(Amazo ...
- spark historyserver 页面反应很慢 jvm堆调参
我们的spark historyserver 最近页面打开很慢 jstat -gcutil pid 1000 发现full gc 相当严重 查看堆大小,发现默认堆1G,打算修改到4G jps -lvm ...
- 【3-30】document获取、事件、标记样式
一.获取标记对象 1.id选择:document.getelementbyid("id名")---根据id找单个元素 2.class选择:document.getelementsb ...
- 3. group_concat与oracle中wm_concat用法一样
例子如下: select group_concat(rp.ROLE_ID) from eic_right_role_operator rp where rp.OPERATOR_ID = #id#
- 表格线边框重复css解决方法
1.td 的边框和table 的边框重叠 .table { border-left:1px solid #dedede; border-top:1px solid #dedede;} .td { bo ...
- Ubuntu 下安装 Swoole
环境:Ubuntu16.04 apt-get update apa-get install apache2 php php-pear php-dev mysql-server gcc apache2 ...
- 【JEECG技术文档】JEECG在线聊天插件功能集成文档
原文地址:http://jeecg.iteye.com/blog/2320670 JEECG在线聊天插件功能集成文档 前提: 采用jeecg_3.6.3版本以上(Maven工程) 插件项目: 在线聊天 ...
- elk-Logstash
ELK是三个工具的集合,Elasticsearch + Logstash + Kibana,这三个工具组合形成了一套实用.易用的监控架构,很多公司利用它来搭建可视化的海量日志分析平台. 2. Logs ...
- vue:在路由跳转中使用拦截器
1:首先在路由对象中的某一个具体的路由对象加这样一个属性 meta: { requireAuth:true } 2:然后在main.js中添加这段代码 router.beforeEach((to, ...
- 跨域(二)——WebSocket
严格地说,WebSocket技术不属于HTML5,这个技术是对HTTP无状态连接的一种革新,本质就是一种持久性socket连接,在浏览器客户端通过javascript进行初始化连接后,就可以监听相关的 ...