C3P0
1、C3P0简介
C3P0是开源免费的连接池,性能强点
2、C3P0D的使用
C3P0中池类是ComboPooledDataSource
配置文件要求
文件名称:必须叫c3p0-config.xml
文件位置:必须在src下
C3P0也可以指定配置文件,而且配置文件可以是properties,也可用xml的,当然xml的高级一些。
但是c3p0的配置文件名必须为c3p0-config.xml并且必须放在类路径下
Tomcat配置连接池
1、Tomcat配置JNDI资源
JNDI(Java Naming and Directory Interface)java命名和目录接口,JNDI的作用就是:在服务器上配置资源,然后通过统一的方式来获取配置的资源
我们需要配置的资源是连接池,这样项目就可以通过统一的方式来获取连接池对象了。
配置JNDI资源需要到<Context>元素中配置<Resource>子元素
name:指定资源的名称,这个名称可以随便给,在获取资源时需要这个名称
factory:用来创建资源的工厂,这个值基本上是固定的,不用修改
type:资源的类型,我们要给出的类型当然使我们连接池的类型
bar:表示资源的属性,如果资源存在名为bar的属性,那么就配置bar的值。对于DBCP连接池而言,
你需要配置的不是bar,因为它没有bar这个属性,而是应该去配置url、username等属性
2、获取资源
配置资源的目的是为了获取资源。只要启动了Tomcat,那么就可以在项目中任何类中通过JNDI获取资源的方式来获取资源了
获取资源
Context:java.naming.Context
InitialContext:javax.naming.InitialContext
lookup(String):获取资源的方法,其中:"java:comp/env"是资源的入口(是固定的名称),
获取过来的还是一个Context,这说明需要在获取到的Context上进一步进行获取。"jdbc/dataSource"对应<Resource>配置的name值,这回获取的是资源对象了
D:\apache-tomcat-8.5.28\conf\Catalina\localhost 创建项目名称.xml
<Context>
<!--
name:指定资源的名称
factory:资源由谁来负责创建
type:资源的类型
其他的东西都是资源的参数
-->
<Resource name="jdbc/dataSource"
factory="org.apache.naming.factory.BeanFactory"
type="com.mchange.v2.c3p0.ComboPooledDataSource"
jdbcUrl="jdbc:mysql://localhost:3306/db1"
driverClass="com.mysql.jdbc.Driver"
user="root"
password="123"
acquireIncrement="5"
initialPoolSize="10"
/>
</Context>
/**
* c3p0
* @author Administrator
*
*/
public class Demo1 {
/**
* 代码配置
* @throws PropertyVetoException
* @throws SQLException
*/
@Test
public void fun1() throws PropertyVetoException, SQLException{
//创建连接池对象
ComboPooledDataSource dataSource = new ComboPooledDataSource();
//对池进行四大参数的配置
dataSource.setDriverClass("com.mysql.jdbc.Driver");
dataSource.setJdbcUrl("jdbc:mysql://localhost:3306/db1");
dataSource.setUser("root");
dataSource.setPassword("123");
//池配置
dataSource.setAcquireIncrement(5);
dataSource.setInitialPoolSize(20);
dataSource.setMinPoolSize(2);
dataSource.setMaxPoolSize(50);
Connection con = dataSource.getConnection();
System.out.println(con);
con.close();
}
/**
* 配置文件的默认配置
* @throws SQLException
*/
@Test
public void fun2() throws SQLException{
/**
* 在创建连接池对象时,这个对象就会自动加载配置文件。不用我们来指定
* 代码优先级高 因为在获取配置文件后覆写了
*/
ComboPooledDataSource dataSource = new ComboPooledDataSource();
Connection con = dataSource.getConnection();
System.out.println(con);
con.close();
}
/**
* 使用命名配置信息
*/
@Test
public void fun3() throws SQLException{
/**
* 构造器的参数指定了命名配置元素的名称
* <named-config name="oracle-config">
*/
ComboPooledDataSource dataSource = new ComboPooledDataSource("oracle-config");
Connection con = dataSource.getConnection();
System.out.println(con);
con.close();
}
}
c3p0的配置文件.xml
<?xml version="1.0" encoding="UTF-8"?>
<c3p0-config>
<!-- 默认的配置信息 -->
<default-config>
<!-- 连接四大参数配置 -->
<property name="jdbcUrl">jdbc:mysql://localhost:3306/db1</property>
<property name="driverClass">com.mysql.jdbc.Driver</property>
<property name="user">root</property>
<property name="password">123</property>
<!-- 池参数配置 -->
<property name="acquireIncrement">5</property>
<property name="initialPoolSize">10</property>
<property name="minPoolSize">2</property>
<property name="maxPoolSize">10</property>
</default-config>
<!-- 专门为oracle提供的配置信息 -->
<named-config name="oracle-config">
<property name="jdbcUrl">jdbc:mysql://localhost:3306/db1</property>
<property name="driverClass">com.mysql.jdbc.Driver</property>
<property name="user">root</property>
<property name="password">123</property>
<property name="acquireIncrement">5</property>
<property name="initialPoolSize">10</property>
<property name="minPoolSize">2</property>
<property name="maxPoolSize">10</property>
</named-config>
</c3p0-config>
public class JdbcUtils {
//配置文件的默认配置。要求必须给出c3p0-config.xml
private static ComboPooledDataSource dataSource = new ComboPooledDataSource();
/**
* 使用连接池返回一个连接对象
* @return
* @throws SQLException
*/
public static Connection getConnection() throws SQLException{
return dataSource.getConnection();
}
/**
* 返回连接池对象
*/
public static DataSource getDataSource(){
return dataSource;
}
}
JNDI
/**
* 获取JNDI的资源
*/
public class AServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
/*
* c3p0管理连接 重用连接对象
* mchange创建连接池
* mysql-connector jdbc的实现为mysql数据库使用
* 1、创建JNDI的上下文对象
*/
try {
Context cxt = new InitialContext();
//2、查询入口
//Context envContext = (Context) cxt.lookup("java:comp/env");
//3、再进行二次查询,找到我们的资源
//使用的名称与<Resource>元素的那么对应
//DataSource dataSource = (DataSource) envContext.lookup("jdbc/dataSource");
DataSource dataSource = (DataSource) cxt.lookup("java:comp/env/jdbc/dataSource");
Connection con = dataSource.getConnection();
System.out.println(con);
con.close();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}