数据库连接池出现的原因:
由于获取连接和关闭连接是一个非常耗系统资源的这么一件事!,所以就有了数据库连接池来管理这些连接!
常见的连接池有DBCP 和 c3p0, 我们这里使用c3p0
使用c3p0的话要点:必须在src下创建一个名为c3p0-config.xml
<?xml version="1.0" encoding="UTF-8"?>
<c3p0-config>
<!--
配置c3p0-config.xml数据库连接池报错Type The reference to entity "useUnicode" must end with the ';' delimiter.
就是不能直接写&, 需要转义!
http://www.manongjc.com/article/17900.html
-->
<!-- 这是默认配置信息 -->
<default-config>
<!-- 连接四大参数配置 -->
<property name="driverClass">com.mysql.jdbc.Driver</property>
<property name="jdbcUrl">jdbc:mysql://127.0.0.1:3306/exam?serverTimezone=UTC&useUnicode=true&charaterEncoding=utf-8&useSSL=false</property>
<property name="user">root</property>
<property name="password">123456</property>
<!-- 池参数配置 -->
<property name="acquireIncrement">3</property>
<property name="initialPoolSize">10</property>
<property name="minPoolSize">2</property>
<property name="maxPoolSize">10</property>
</default-config>
<!-- 专门为oracle提供的配置信息 -->
<named-config name="oracle-config">
<!--省了。。。 -->
</named-config>
</c3p0-config>
public class TestC3p0 {
public static void main(String[] args) throws SQLException {
//c3p0连接池
ComboPooledDataSource dataSource = new ComboPooledDataSource();
//ComboPooledDataSource dataSource = new ComboPooledDataSource("oracel-config");//获取专为oracel配置的信息
Connection connection = dataSource.getConnection();
System.out.println(connection);//调用池的close()是把连接归还给池,而不是关闭连接,这里面应该使用了装饰者模式
}
}
以后获取连接就很方便了,直接从连接池拿就行!
/**
* 用连接池来管理连接!
*/
public class JdbcUtils2 {
//使用默认配置!
private static ComboPooledDataSource dataSource = new ComboPooledDataSource();
public static Connection getConnection() throws SQLException{
return dataSource.getConnection();
}
public static DataSource getDataSource() {
return dataSource;
}
public static void main(String[] args) throws SQLException {
System.out.println(JdbcUtils2.getConnection());
}
}
演示在tomcat下配置JNDI:
JNDI的作用就是:在服务器上配置资源,然后通过统一的方式来获取配置的资源。我们这里配置的资源就是连接池!
在需要配置JNDI的项目下的META-INF下创建一个context.xml, 这时配置局部的!!
要想配置全局的,在\apache-tomcat-8.5.39\conf\context.xml下配置,这样所有项目都可以访问JNDI下的配置的资源了!!
注意:不要写<?xml version="1.0" encoding="UTF-8"?>
<Context>
<Resource name="myc3p0"
type="com.mchange.v2.c3p0.ComboPooledDataSource"
factory="org.apache.naming.factory.BeanFactory"
driverClass="com.mysql.jdbc.Driver"
jdbcUrl="jdbc:mysql://127.0.0.1:3306/exam?serverTimezone=UTC&useUnicode=true&charaterEncoding=utf-8&useSSL=false"
user="root"
password="123456"
acquireIncrement="3"
initialPoolSize="10"
minPoolSize="2"
maxPoolSize="10"/>
</Context>
获取JNDI下的资源:不要直接在main下面测试
在用JNDI连接数据库时用main函数测试时总报错,
InitialContext 是要在应用服务器的上下文里才有的。
你在普通的应用程序里怎么行!
放到tomcat里面运行看看吧!
不能用main函数直接测试,只能放到tomcat或者servlet、jsp显示
Exception in thread "main" javax.naming.NoInitialContextException: Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file: java.naming.factory.initial
创建一个Servlet测试
@WebServlet("/JNDIServlet")
public class JNDIServlet extends HttpServlet{
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
try {
Context cxt = new InitialContext();
Context envCxt = (Context)cxt.lookup("java:/comp/env");
DataSource ds = (DataSource)envCxt.lookup("myc3p0"); //名字要和配置的资源名一致
Connection con = ds.getConnection();
System.out.println(con);
con.close();
} catch(Exception e) {
e.printStackTrace();
}
}
}
需要的jar包:c3p0-0.9.2-pre1.jar,mchange-commons-0.2.jar,mysql驱动包!