开源数据链接池
2013-12-11 21:49
399 查看
l 使用数据库连接池优化程序性能
数据库连接池原理: 一次性创建多个连接,将多个连接缓存在内存中 ,形成数据库连接池(内存数据库连接集合),如果应用程序需要操作数据库,只需要从连接池中获取一个连接,使用后,关闭连接的同时,将连接放回到连接池中。
DBCP数据源:
需要使用的jar包:
Commons-dbcp.jar:连接池的实现Commons-pool.jar:连接池实现的依赖库需要使用的类:
1:BasicDataSource 直接使用其无参构造器(基本上没用)
1):void setDriverClassName(“com.mysql.jdbc.Driver”);2):void setUrl(“jdbc:mysql:///DataBase”);3):void setUsername(“”);4):void setPassword(“”);5)Connection getConnection();2:BasicDataSourceFactory 直接使用其构造器,配置文件
常见设置:#连接设置driverClassName=com.mysql.jdbc.Driverurl=jdbc:mysql://localhost:3306/jdbcusername=rootpassword=#<!-- 初始化连接 -->initialSize=10#最大连接数量maxActive=50#<!-- 最大空闲连接 -->maxIdle=20#<!-- 最小空闲连接 -->minIdle=5#<!-- 超时等待时间以毫秒为单位 6000毫秒/1000等于60秒 -->maxWait=60000#JDBC驱动建立连接时附带的连接属性属性的格式必须为这样:[属性名=property;] #注意:"user" 与 "password" 两个属性会被明确地传递,因此这里不需要包含他们。connectionProperties=useUnicode=true;characterEncoding=gbk
#指定由连接池所创建的连接的自动提交(auto-commit)状态。defaultAutoCommit=true
#driver default 指定由连接池所创建的连接的事务级别(TransactionIsolation)。#可用值为下列之一:(详情可见javadoc。)NONE,READ_UNCOMMITTED, READ_COMMITTED, REPEATABLE_READ, SERIALIZABLEdefaultTransactionIsolation=READ_UNCOMMITTED
1) DataSource createDataSource(Properties properties)
C3P0数据源:
ComboPooledDataSource类构造方法:new ComboPooledDataSource()ComboPooledDataSource cpds = new ComboPooledDataSource(); cpds.setDriverClass("com.mysql.jdbc.Driver"); cpds.setJdbcUrl("jdbc:mysql://localhost/database"); cpds.setUser("xxx); cpds.setPassword("xxx"); Connection conn = cpds.getConnection();
使用配置文件By default, c3p0 will look for an XML configuration file in its classloader's resource path under the name "/c3p0-config.xml"
如果配置了xml文件,使用无参构造器,会默认读取配置文件中的信息,<default-config>,可以通过构造器指定要使用哪一个<name-config name=””>中配置的信息
配置更多信息:
Configuration· acquireIncrement 当连接池中没有连接时,连接池自动获取连接时,一次获取的连接个数· initialPoolSize 连接池初始化时获取连接的个数· maxPoolSize 连接池可以保留的最大连接数量· maxIdleTime 当连接池空闲多就是释放连接,如果时间值为0,表示不释放连接· minPoolSize 连接池保有的最少连接数量
l JNDI技术简介1) JNDI(Java Naming and Directory Interface),Java命名和目录接口,它对应于J2SE中的javax.naming包2) 这套API的主要作用在于:它可以把Java对象放在一个容器中(支持JNDI容器 Tomcat),并为容器中的java对象取一个名称,以后程序想获得Java对象,只需通过名称检索即可。3) 其核心API为Context,它代表JNDI容器,其lookup方法为检索容器中对应名称的对象。
Tomcat数据源
原理:底层是dbcp,基于jndi技术工作---------------------------------
Jndi:Java命名和目录接口本身是一个容器,允许名字和值的方式在其中配置属性,操作步骤1、配置使用tomcat 内置连接池 配置<context> 元素 context元素有三种常见配置位置1) tomcat/conf/context.xml 所有虚拟主机,所有工程都可以访问该连接池 2) tomcat/conf/Catalina/localhost/context.xml 当前虚拟主机(localhost)下所有工程都可以使用该连接池3) 当前工程/META-INF/context.xml(文件名不能变) 只有当前工程可以访问该连接池
<Context> <Resource name="jdbc/EmployeeDB" auth="Container" type="javax.sql.DataSource" username="root" password="abc" driverClassName="com.mysql.jdbc.Driver" url="jdbc:mysql:///day14" maxActive="8" maxIdle="4"/></Context>
* 必须先将mysql驱动jar包 复制tomcat/lib下 * 在tomcat启动服务器时,创建连接池对象,绑定 jdbc/EmployeeDB 指定名称上
2、通过运行在JNDI容器内部的程序(Servlet/JSP)去访问tomcat内置连接池 Context context = new InitialContext();Context envCtx = (Context)context.lookup("java:comp/env"); 固定路径,JBDI的名字DataSource datasource = (DataSource) envCtx.lookup("jdbc/EmployeeDB"); 通过绑定名称,查找指定java对象
数据库连接池原理: 一次性创建多个连接,将多个连接缓存在内存中 ,形成数据库连接池(内存数据库连接集合),如果应用程序需要操作数据库,只需要从连接池中获取一个连接,使用后,关闭连接的同时,将连接放回到连接池中。
DBCP数据源:
需要使用的jar包:
Commons-dbcp.jar:连接池的实现Commons-pool.jar:连接池实现的依赖库需要使用的类:
1:BasicDataSource 直接使用其无参构造器(基本上没用)
1):void setDriverClassName(“com.mysql.jdbc.Driver”);2):void setUrl(“jdbc:mysql:///DataBase”);3):void setUsername(“”);4):void setPassword(“”);5)Connection getConnection();2:BasicDataSourceFactory 直接使用其构造器,配置文件
常见设置:#连接设置driverClassName=com.mysql.jdbc.Driverurl=jdbc:mysql://localhost:3306/jdbcusername=rootpassword=#<!-- 初始化连接 -->initialSize=10#最大连接数量maxActive=50#<!-- 最大空闲连接 -->maxIdle=20#<!-- 最小空闲连接 -->minIdle=5#<!-- 超时等待时间以毫秒为单位 6000毫秒/1000等于60秒 -->maxWait=60000#JDBC驱动建立连接时附带的连接属性属性的格式必须为这样:[属性名=property;] #注意:"user" 与 "password" 两个属性会被明确地传递,因此这里不需要包含他们。connectionProperties=useUnicode=true;characterEncoding=gbk
#指定由连接池所创建的连接的自动提交(auto-commit)状态。defaultAutoCommit=true
#driver default 指定由连接池所创建的连接的事务级别(TransactionIsolation)。#可用值为下列之一:(详情可见javadoc。)NONE,READ_UNCOMMITTED, READ_COMMITTED, REPEATABLE_READ, SERIALIZABLEdefaultTransactionIsolation=READ_UNCOMMITTED
1) DataSource createDataSource(Properties properties)
C3P0数据源:
ComboPooledDataSource类构造方法:new ComboPooledDataSource()ComboPooledDataSource cpds = new ComboPooledDataSource(); cpds.setDriverClass("com.mysql.jdbc.Driver"); cpds.setJdbcUrl("jdbc:mysql://localhost/database"); cpds.setUser("xxx); cpds.setPassword("xxx"); Connection conn = cpds.getConnection();
使用配置文件By default, c3p0 will look for an XML configuration file in its classloader's resource path under the name "/c3p0-config.xml"
模板: <c3p0-config> <default-config> <propertyname="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> </c3p0-config> |
<?xmlversion="1.0"encoding="UTF-8"?> <c3p0-config> <default-config> <propertyname="driverClass">com.mysql.jdbc.Driver</property> <propertyname="jdbcUrl">jdbc:mysql:///mydb</property> <propertyname="user">root</property> <propertyname="password">root</property> </default-config> <name-configname="mysql"> <propertyname="driverClass">com.mysql.jdbc.Driver</property> <propertyname="jdbcUrl">jdbc:mysql:///mydb</property> <propertyname="user">root</property> <propertyname="password">root</property> </name-config> </c3p0-config> |
publicvoid fun2() throws SQLException { //ComboPooledDataSource dataSource = new ComboPooledDataSource(); ComboPooledDataSource dataSource = new ComboPooledDataSource("mysql"); Connection conn = dataSource.getConnection(); Statement ps = conn.createStatement(); ResultSet set = ps.executeQuery("select * from test1"); while(set.next()) { String name = set.getString("name"); System.out.println(name); } set.close(); ps.close(); conn.close(); } |
Configuration· acquireIncrement 当连接池中没有连接时,连接池自动获取连接时,一次获取的连接个数· initialPoolSize 连接池初始化时获取连接的个数· maxPoolSize 连接池可以保留的最大连接数量· maxIdleTime 当连接池空闲多就是释放连接,如果时间值为0,表示不释放连接· minPoolSize 连接池保有的最少连接数量
l JNDI技术简介1) JNDI(Java Naming and Directory Interface),Java命名和目录接口,它对应于J2SE中的javax.naming包2) 这套API的主要作用在于:它可以把Java对象放在一个容器中(支持JNDI容器 Tomcat),并为容器中的java对象取一个名称,以后程序想获得Java对象,只需通过名称检索即可。3) 其核心API为Context,它代表JNDI容器,其lookup方法为检索容器中对应名称的对象。
Tomcat数据源
原理:底层是dbcp,基于jndi技术工作---------------------------------
Jndi:Java命名和目录接口本身是一个容器,允许名字和值的方式在其中配置属性,操作步骤1、配置使用tomcat 内置连接池 配置<context> 元素 context元素有三种常见配置位置1) tomcat/conf/context.xml 所有虚拟主机,所有工程都可以访问该连接池 2) tomcat/conf/Catalina/localhost/context.xml 当前虚拟主机(localhost)下所有工程都可以使用该连接池3) 当前工程/META-INF/context.xml(文件名不能变) 只有当前工程可以访问该连接池
<Context> <Resource name="jdbc/EmployeeDB" auth="Container" type="javax.sql.DataSource" username="root" password="abc" driverClassName="com.mysql.jdbc.Driver" url="jdbc:mysql:///day14" maxActive="8" maxIdle="4"/></Context>
* 必须先将mysql驱动jar包 复制tomcat/lib下 * 在tomcat启动服务器时,创建连接池对象,绑定 jdbc/EmployeeDB 指定名称上
2、通过运行在JNDI容器内部的程序(Servlet/JSP)去访问tomcat内置连接池 Context context = new InitialContext();Context envCtx = (Context)context.lookup("java:comp/env"); 固定路径,JBDI的名字DataSource datasource = (DataSource) envCtx.lookup("jdbc/EmployeeDB"); 通过绑定名称,查找指定java对象
相关文章推荐
- 初学Java的备忘录
- 装系统软件备忘
- Ubuntu系统下添加程序启动器
- Mysql 常用命令备忘
- 备忘
- Jsp连接mySQL、Oracle数据库备忘(Windows平台) 推荐
- ZendGuard-5_0_1 使用备忘
- zookeeper自带工具
- 运行shell脚本文件,启动java application
- CentOS 6.5 shutdown now关机无效
- Erlang的supervisor
- ubuntu 下anroid开发--工作备忘
- 占位
- win32备忘
- 如何快速转载CSDN中的博客
- Windows 下编程开发备忘
- 备忘
- 备忘——ssh连接vbox中centos
- mgr分析
- 国内好的镜像地址