数据库连接池之c3p0
来源:互联网 发布:戴尔游匣7557优化教程 编辑:程序博客网 时间:2024/05/22 01:49
使用c3p0连接池初始化DataSource有两种方式,一种可以在Java代码中编码连接池信息,一种是在配置文件中配置连接池信息。
配置文件形式
例如在Java 的Maven项目中,引入c3p0和数据库驱动:
<dependency> <groupId>com.mchange</groupId> <artifactId>c3p0</artifactId> <version>0.9.5.2</version></dependency><dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.38</version></dependency>
配置文件放在resources下,文件名称为c3p0-config.xml:
<c3p0-config> <default-config> <property name="maxPoolSize">30</property> <property name="minPoolSize">10</property> </default-config> <named-config name="jt-datasource"> <property name="user">root</property> <property name="password">root</property> <property name="driverClass">com.mysql.jdbc.Driver</property> <property name="jdbcUrl">jdbc:mysql://10.47.98.144:3306/quickview?characterEncoding=utf8&useSSL=false</property> <!--初始化时获取三个连接,取值应在minPoolSize与maxPoolSize之间。Default: 3 --> <property name="initialPoolSize">3</property> <!--连接池中保留的最大连接数。Default: 15 --> <property name="maxPoolSize">50</property> <!--JDBC的标准参数,用以控制数据源内加载的PreparedStatements数量。但由于预缓存的statements 属于单个connection而不是整个连接池。所以设置这个参数需要考虑到多方面的因素。 如果maxStatements与maxStatementsPerConnection均为0,则缓存被关闭。Default: 0--> <property name="maxStatements">100</property> <!--c3p0是异步操作的,缓慢的JDBC操作通过帮助进程完成。扩展这些操作可以有效的提升性能 通过多线程实现多个操作同时被执行。Default: 3--> <property name="numHelperThreads">3</property> <!--最大空闲时间,60秒内未使用则连接被丢弃。若为0则永不丢弃。Default: 0 --> <property name="maxIdleTime">60</property> <!--当连接池中的连接耗尽的时候c3p0一次同时获取的连接数。Default: 3 --> <property name="acquireIncrement">3</property> <!--定义在从数据库获取新连接失败后重复尝试的次数。Default: 30 --> <property name="acquireRetryAttempts">30</property> <!--当连接池用完时客户端调用getConnection()后等待获取新连接的时间,超时后将抛出SQLException,如设为0则无限期等待。单位毫秒。Default: 0 --> <property name="checkoutTimeout">3000</property> <!--每60秒检查所有连接池中的空闲连接。Default: 0 --> <property name="idleConnectionTestPeriod">60</property> </named-config></c3p0-config>
全局实例化唯一DataSource
/** * 数据库连接池 * Created by weifeng on 2017/4/1. */public class DataSource { private static Logger LOG = Logger.getLogger(DataSource.class); private static ComboPooledDataSource cpds = null; private static DataSource instance = null; private DataSource() { } static { cpds = new ComboPooledDataSource("jt-datasource");// cpds = new ComboPooledDataSource();// cpds.setJdbcUrl("jdbc:mysql://10.47.98.144:3306/quickview?characterEncoding=utf8&useSSL=false");// cpds.setDriverClass("com.mysql.jdbc.Driver");// cpds.setUser("root");// cpds.setPassword("root"); } public static DataSource getInstance() { if (instance == null) { synchronized (DataSource.class) { if (instance == null) { instance = new DataSource(); } } } return instance; } public static Connection getConnection() { Connection connection = null; try { connection = cpds.getConnection(); } catch (SQLException e) { LOG.error(e.getMessage()); System.out.println(e.getMessage()); } return connection; } public static void close(ResultSet rs, Statement statement, Connection connection) { try { if (rs != null) { rs.close(); } if (statement != null) { statement.close(); } if (connection != null) { connection.close(); } } catch (SQLException e) { e.printStackTrace(); LOG.error(e.getMessage()); } } public static void close(Statement statement, Connection connection) { try { if (statement != null) { statement.close(); } if (connection != null) { connection.close(); } } catch (SQLException e) { e.printStackTrace(); LOG.error(e.getMessage()); } } public static void close(Connection connection) { try { if (connection != null) { connection.close(); } } catch (SQLException e) { e.printStackTrace(); LOG.error(e.getMessage()); } }}
0 0
- 数据库连接池之c3p0
- 数据库连接池之C3P0
- 数据库连接池 数据源之C3P0
- c3p0 数据库连接池配置
- 数据库连接池C3P0 小结
- 数据库连接池 c3p0
- c3p0数据库连接池dbutil
- 数据库连接池-C3P0
- c3p0数据库连接池实例
- 数据库连接池C3P0学习
- java c3p0数据库连接池
- C3P0数据库连接池源代码
- 数据库连接池C3P0学习
- C3p0-数据库连接池
- 数据库连接池框架:c3p0
- 数据库连接池 - C3P0
- C3P0数据库连接池使用
- C3P0数据库连接池使用
- C# 枚举和结构
- 如何写一个DOM加载完成的执行行数
- 官方 React 快速上手脚手架 create-react-app
- 个人记录-LeetCode 101. Symmetric Tree
- js移动表格数据和给表格数字排序
- 数据库连接池之c3p0
- HTML5无刷新修改URL:利用 History API 无刷新更改地址栏
- bom
- 二、stpringMVC mysql+jpa 篇
- Flask中如何简便从Request中提取请求的参数值
- 百度地图搜索(三)
- 倒计时6小时
- Xcode8 missing file 报 ”xx“is missing from working copy 警告
- 三维重建(二)Sift特征提取与匹配