Java中使用C3P0连接池
先看官网给的范例:
import java.sql.*;
import javax.naming.*;
import javax.sql.DataSource;
import com.mchange.v2.c3p0.DataSources; /**
* This example shows how to acquire a c3p0 DataSource and
* bind it to a JNDI name service.
*/
public final class JndiBindDataSource
{
// be sure to load your database driver class, either via
// Class.forName() [as shown below] or externally (e.g. by
// using -Djdbc.drivers when starting your JVM).
static
{
try
{ Class.forName( "com.mysql.jdbc.Driver" ); }
catch (Exception e)
{ e.printStackTrace(); }
} public static void main(String[] argv)
{
try
{
// let a command line arg specify the name we will
// bind our DataSource to.
String jndiName = argv[0]; // acquire the DataSource using default pool params...
// this is the only c3p0 specific code here
DataSource unpooled = DataSources.unpooledDataSource("jdbc:mysql://127.0.0.1:3306/gpsdata",
"root",
"root");
DataSource pooled = DataSources.pooledDataSource( unpooled ); // Create an InitialContext, and bind the DataSource to it in
// the usual way.
//
// We are using the no-arg version of InitialContext's constructor,
// therefore, the jndi environment must be first set via a jndi.properties
// file, System properties, or by some other means.
InitialContext ctx = new InitialContext();
ctx.rebind( jndiName, pooled );
System.out.println("DataSource bound to nameservice under the name \"" +
jndiName + '\"');
}
catch (Exception e)
{ e.printStackTrace(); }
} static void attemptClose(ResultSet o)
{
try
{ if (o != null) o.close();}
catch (Exception e)
{ e.printStackTrace();}
} static void attemptClose(Statement o)
{
try
{ if (o != null) o.close();}
catch (Exception e)
{ e.printStackTrace();}
} static void attemptClose(Connection o)
{
try
{ if (o != null) o.close();}
catch (Exception e)
{ e.printStackTrace();}
} private JndiBindDataSource()
{}
}
1.建立 com.mchange.v2.c3p0.ComboPooledDataSource
这是一个JavaBean,在使用前应设置它的jdbcURL、user、password和driverClass。其他参数参考configuration properties
- ComboPooledDataSource cpds = new ComboPooledDataSource(); cpds.setDriverClass( "org.postgresql.Driver" );
- cpds.setJdbcUrl( "jdbc:postgresql://localhost/testdb" ); cpds.setUser("swaldman");
- cpds.setPassword("test-password");
- // 下面的设置是可选的,c3p0可以在默认条件下工作,也可以设置其他条件
- cpds.setMinPoolSize(5);
- cpds.setAcquireIncrement(5);
- cpds.setMaxPoolSize(20);
也可以使用命名Configuration
- ComboPooledDataSource cpds = new ComboPooledDataSource("intergalactoApp");
2.使用工厂类com.mchange.v2.c3p0.DataSources
com.mchange.v2.c3p0.DataSources 可以按照传统的JDBC驱动建立一个无连接池的DataSource,然后转化为连接池的DataSource。
- DataSource ds_unpooled = DataSources.unpooledDataSource("jdbc:postgresql://localhost/testdb", "swaldman", "test-password");
- DataSource ds_pooled = DataSources.pooledDataSource( ds_unpooled );
- // 此时DataSource已经可以使用,但应该显示的设置driver Class
- Class.forName("org.postgresql.Driver");
如果想设置其中的参数,可以将参数放入一个Map中
- DataSource ds_unpooled = DataSources.unpooledDataSource("jdbc:postgresql://localhost/testdb", "swaldman", "test-password");
- Map overrides = new HashMap();
- overrides.put("maxStatements", "200"); overrides.put("maxPoolSize", new Integer(50));
- //建立一个包括默认值和设置值的PooledDataSource
- ds_pooled = DataSources.pooledDataSource( ds_unpooled, overrides );
如果使用命名的Configuration,可以如下
- ds_pooled = DataSources.pooledDataSource( ds_unpooled, "intergalactoAppConfig", overrides );
销毁DataSource有两种方式
DataSource.destroy()方式
- DataSource ds_pooled = null;
- try {
- DataSource ds_unpooled = DataSources.unpooledDataSource("jdbc:postgresql://localhost/testdb", "swaldman", "test-password");
- ds_pooled = DataSources.pooledDataSource( ds_unpooled );
- // 下面正常使用DataSource...
- }
- finally {
- DataSources.destroy( ds_pooled );
- }
另一种是PooledDataSource 接口提供的close() 方法
- static void cleanup(DataSource ds) throws SQLException {
- // 确定是否为c3p0的PooledDataSource
- if ( ds instanceof PooledDataSource) {
- PooledDataSource pds = (PooledDataSource) ds;
- pds.close();
- } else
- System.err.println("Not a c3p0 PooledDataSource!");
- }
Java中使用C3P0连接池的更多相关文章
- Spring框架中 配置c3p0连接池 完成对数据库的访问
开发准备: 1.导入jar包: ioc基本jar jdbcTemplate基本jar c3p0基本jar 别忘了mysql数据库驱动jar 原始程序代码:不使用配置文件方式(IOC)生成访问数据库对象 ...
- JAVA中事物以及连接池
一.事物 什么是事物? 事务,一般是指要做的或所做的事情.在计算机术语中是指访问并可能更新数据库中各种数据项的一个程序执行单元.这些单元要么全都成功,要么全都不成功. 做一件事情,这个一件事情中有多个 ...
- java学习笔记—c3p0连接池与元数据分析(42)
第一步:导入c3p0包 第二步:在classpath目录下,创建一个c3p0-config.xml <?xml version="1.0" encoding="UT ...
- Spring框架中 配置c3p0连接池
开发准备: 1.导入jar包: ioc基本jar jdbcTemplate基本jar c3p0基本jar 别忘了mysql数据库驱动jar 原始程序代码:不使用配置文件方式(IOC)生成访问数据库对象 ...
- Hibernate的配置中,c3p0连接池相关配置
一.配置c3p0 1.导入 hibernate-c3po连接池包,Maven地址是:http://mvnrepository.com/artifact/org.hibernate/hibernate- ...
- C3P0连接池在hibernate和spring中的配置
首先为什么要使用连接池及为什么要选择C3P0连接池,这里就不多说了,目前C3P0连接池还是比较方便.比较稳定的连接池,能与spring.hibernate等开源框架进行整合. 一.hibernate中 ...
- Spring之c3p0连接池配置和使用
1.导入包:c3p0和mchange包 2.代码实现方式: package helloworld.pools; import com.mchange.v2.c3p0.ComboPooledDataSo ...
- C3P0连接池工具类实现步骤及方法
C3P0连接池的工具类 使用C3P0获得连接对象连接池有一个规范接口 javax.sal.DataSourse 接口定义了一个从连接池中获得连接的方法getConnection(); 步骤导入jar包 ...
- (七)Spring 配置 c3p0 连接池
目录 在 Spring 核心配置文件中配置 c3p0 连接池 配置 JdbcTemplate 对象 在 service 层注入 userDao 在 UserDao 里面注入 JdbcTemplate ...
随机推荐
- ormlite介绍一
概述 ORMlite是类似hibernate的对象映射框架,主要面向java语言,同时,是时下最流行的android面向数据库的的编程工具. 官方网站:http://ormlite.com ...
- XML之DTD(文档类型定义)
文档类型定义(DTD)可定义合法的XML文档构建模块.它使用一系列合法的元素来定义文档的结构. DTD 可被成行地声明于 XML 文档中,也可作为一个外部引用. 声明元素 在 DTD 中,XML 元素 ...
- iOS下JS与OC互相调用(二)--WKWebView 拦截URL
在上篇文章中讲述了使用UIWebView拦截URL的方式来处理JS与OC交互. 由于UIWebView比较耗内存,性能上不太好,而苹果在iOS 8中推出了WKWebView. 同样的用WKWebVie ...
- 聊聊Condition
本文可作为传智播客<张孝祥-Java多线程与并发库高级应用>的学习笔记. 上面我们说了Lock,那是对synchronized的一种更为面向对象的替代,在原来的synchronized内部 ...
- Activity之间的数据传递-android学习之旅(四十七)
activity之间的数据传递主要有两种,一种是直接发送数据,另一种接受新启动的activity返回的数据,本质是一样的 使用Bundle传递数据 Intent使用Bundle在activity之间传 ...
- Android的数字选择器NumberPicker-android学习之旅(三十七)
我想说的话 今天晚上我依然在图书馆写博客,其实此刻我的没心激动而忐忑,因为明天就是足球赛的决赛,我作为主力球员压力很大,因对对方很强大,但是那又怎么样.so what...我不会停止写博客的 Numb ...
- Android4.4.2KK竖屏强制更改为横屏的初步简略方案
点击打开链接 解决方案: 当前是根据当前问题场景即竖屏强制更改为横屏的需求而做的改动,基本是hardcode定义的状态,总共修改有效代码行数5行,如果后续有其他需求或者需要更灵活的配置横屏和竖屏,可以 ...
- LAPACK的C/C++接口及代码实例
今天介绍一个矩阵处理工具LAPACK,她有C\C++接口,可在windows下移植.本人最近正在学习,发现还是还不错滴~ 本博文分为三部分,第一部分介绍LAPACK的安装,这里只介绍最简单的部署:第二 ...
- Java学习从菜鸟变大鸟之二 输入输出流(IO)
在软件开发中,数据流和数据库操作占据了一个很重要的位置,所以,熟悉操作数据流和数据库,对于每一个开发者来说都是很重要的,今天就来总结一下JavaI/O. 流 流是一个很形象的概念,当程序需要读取数据的 ...
- Struts Chain ClassCastException Aop
我们知道struts的restult type 有很多,但主要就是四种 dispatch,rediret,chain,drdirectaction 要让数据从一个action传到另一个action,就 ...