声明:本栏目所使用的素材都是凯哥学堂VIP学员所写,学员有权匿名,对文章有最终解释权;凯哥学堂旨在促进VIP学员互相学习的基础上公开笔记。 1.数据库连接池什么是数据库连接池? ![image](http://os78f4hny.bkt.clouddn.com/201709110001.jpg?imageMogr2/blur/1x0/quality/75%7Cwatermark/1/image/aHR0cDovL29zNzhmNGhueS5ia3QuY2xvdWRkbi5jb20vd2F0ZXJtYXJrLnBuZw==/dissolve/50/gravity/SouthEast/dx/10/dy/10%7Cimageslim)
现阶段的连接数据库的方法,都是通过DriverManager.getConnection这个接口方法来获得连接对象Connection。但是每次都通过这种方式来获得数据库连接对象很麻烦也很耗费资源。 每次需要连接对象的时候,都要花费时间通过这个方法去获得数据库连接对象,很耗费时间。 怎么优化获得数据库连接对象,使耗时更短: ![image](http://os78f4hny.bkt.clouddn.com/201709110002.jpg?imageMogr2/blur/1x0/quality/75%7Cwatermark/1/image/aHR0cDovL29zNzhmNGhueS5ia3QuY2xvdWRkbi5jb20vd2F0ZXJtYXJrLnBuZw==/dissolve/50/gravity/SouthEast/dx/10/dy/10%7Cimageslim)
使用数据库连接池:就是在连接数据库前事先用几个数据库连接对象变量去和数据库进行连接好(一直连接,不断开),在池子中一直等待,当要使用数据库连接对象的时候,直接到池子中把已经提前连接好的对象拿来用,然后用完之后再把对象还回池子里面去。如果池子中有3个事先连接好的数据库连接对象,但是一次把三个对象都拿去用了,那么下一个用户过来要使用数据库连接对象的时候,就要等待了,等待前面的用完把对象还回到池子中,然后再去拿来使用。 连接池的目的就是提前准备多个已经连接到了数据库的对象在那里准备着被拿去使用,但你要使用的时候,直接从池子里拿出来用,用完之后再给人家放回去,用来优化数据库连接。 ![image](http://os78f4hny.bkt.clouddn.com/201709110003.jpg?imageMogr2/blur/1x0/quality/75%7Cwatermark/1/image/aHR0cDovL29zNzhmNGhueS5ia3QuY2xvdWRkbi5jb20vd2F0ZXJtYXJrLnBuZw==/dissolve/50/gravity/SouthEast/dx/10/dy/10%7Cimageslim)
Java公司并没有做数据库连接池的实现,它只是做了一个规范,也不给出具体的数据库连接池的概念,而是做了一个数据源javax.sql.DataSource,这个数据库源只是数据库连接的一个优化手段的规范的接口,至于你具体怎么去实现这个数据库连接池由自己定义。或者你自己不想使用数据库连接池这个方法也可以,你只要在这个地方能够以很优化的方式给我提供数据库连接对象就行了。 ![image](http://os78f4hny.bkt.clouddn.com/201709110004.jpg?imageMogr2/blur/1x0/quality/75%7Cwatermark/1/image/aHR0cDovL29zNzhmNGhueS5ia3QuY2xvdWRkbi5jb20vd2F0ZXJtYXJrLnBuZw==/dissolve/50/gravity/SouthEast/dx/10/dy/10%7Cimageslim)
![image](http://os78f4hny.bkt.clouddn.com/201709110005.jpg?imageMogr2/blur/1x0/quality/75%7Cwatermark/1/image/aHR0cDovL29zNzhmNGhueS5ia3QuY2xvdWRkbi5jb20vd2F0ZXJtYXJrLnBuZw==/dissolve/50/gravity/SouthEast/dx/10/dy/10%7Cimageslim)
![image](http://os78f4hny.bkt.clouddn.com/201709110006.jpg?imageMogr2/blur/1x0/quality/75%7Cwatermark/1/image/aHR0cDovL29zNzhmNGhueS5ia3QuY2xvdWRkbi5jb20vd2F0ZXJtYXJrLnBuZw==/dissolve/50/gravity/SouthEast/dx/10/dy/10%7Cimageslim)
这是数据源接口最主要的两个方法,这个接口只提供获得数据库连接对象的规范,自己去实现。业界中流行的两个数据源插件: ![image](http://os78f4hny.bkt.clouddn.com/201709110007.jpg?imageMogr2/blur/1x0/quality/75%7Cwatermark/1/image/aHR0cDovL29zNzhmNGhueS5ia3QuY2xvdWRkbi5jb20vd2F0ZXJtYXJrLnBuZw==/dissolve/50/gravity/SouthEast/dx/10/dy/10%7Cimageslim)
C3P0是用的比较多的,占用资源比较少的一种数据源插件,DBCP是apache提供的一种数据源插件, DBCP:DATABASE CONNECTION POOL:APACHE推出的数据源插件: ![image](http://os78f4hny.bkt.clouddn.com/201709110008.jpg?imageMogr2/blur/1x0/quality/75%7Cwatermark/1/image/aHR0cDovL29zNzhmNGhueS5ia3QuY2xvdWRkbi5jb20vd2F0ZXJtYXJrLnBuZw==/dissolve/50/gravity/SouthEast/dx/10/dy/10%7Cimageslim)
在apache官网下的components下。我们用maven来下载jar包。 ![image](http://os78f4hny.bkt.clouddn.com/201709110009.jpg?imageMogr2/blur/1x0/quality/75%7Cwatermark/1/image/aHR0cDovL29zNzhmNGhueS5ia3QuY2xvdWRkbi5jb20vd2F0ZXJtYXJrLnBuZw==/dissolve/50/gravity/SouthEast/dx/10/dy/10%7Cimageslim)
可以选用上面两个之一。 ![image](http://os78f4hny.bkt.clouddn.com/201709110010.jpg?imageMogr2/blur/1x0/quality/75%7Cwatermark/1/image/aHR0cDovL29zNzhmNGhueS5ia3QuY2xvdWRkbi5jb20vd2F0ZXJtYXJrLnBuZw==/dissolve/50/gravity/SouthEast/dx/10/dy/10%7Cimageslim)
![image](http://os78f4hny.bkt.clouddn.com/201709110011.jpg?imageMogr2/blur/1x0/quality/75%7Cwatermark/1/image/aHR0cDovL29zNzhmNGhueS5ia3QuY2xvdWRkbi5jb20vd2F0ZXJtYXJrLnBuZw==/dissolve/50/gravity/SouthEast/dx/10/dy/10%7Cimageslim)
上面是具体的操作类。 数据库连接池的目的是优化数据库连接对象的,不会像jdbc那样可以自动帮你加入驱动,需要自己手动加入驱动. ![image](http://os78f4hny.bkt.clouddn.com/201709110012.jpg?imageMogr2/blur/1x0/quality/75%7Cwatermark/1/image/aHR0cDovL29zNzhmNGhueS5ia3QuY2xvdWRkbi5jb20vd2F0ZXJtYXJrLnBuZw==/dissolve/50/gravity/SouthEast/dx/10/dy/10%7Cimageslim)
![image](http://os78f4hny.bkt.clouddn.com/201709110013.jpg?imageMogr2/blur/1x0/quality/75%7Cwatermark/1/image/aHR0cDovL29zNzhmNGhueS5ia3QuY2xvdWRkbi5jb20vd2F0ZXJtYXJrLnBuZw==/dissolve/50/gravity/SouthEast/dx/10/dy/10%7Cimageslim)
连接方式如上。 ![image](http://os78f4hny.bkt.clouddn.com/201709110014.jpg?imageMogr2/blur/1x0/quality/75%7Cwatermark/1/image/aHR0cDovL29zNzhmNGhueS5ia3QuY2xvdWRkbi5jb20vd2F0ZXJtYXJrLnBuZw==/dissolve/50/gravity/SouthEast/dx/10/dy/10%7Cimageslim)
![image](http://os78f4hny.bkt.clouddn.com/201709110015.jpg?imageMogr2/blur/1x0/quality/75%7Cwatermark/1/image/aHR0cDovL29zNzhmNGhueS5ia3QuY2xvdWRkbi5jb20vd2F0ZXJtYXJrLnBuZw==/dissolve/50/gravity/SouthEast/dx/10/dy/10%7Cimageslim)
可见,我们可以用数据源的类来获得多个连接对象,最多获得8个。默认情况下设置池子中养8个连接对象。可以自己设置池子里面最多养的连接对象的数量: ![image](http://os78f4hny.bkt.clouddn.com/201709110016.jpg?imageMogr2/blur/1x0/quality/75%7Cwatermark/1/image/aHR0cDovL29zNzhmNGhueS5ia3QuY2xvdWRkbi5jb20vd2F0ZXJtYXJrLnBuZw==/dissolve/50/gravity/SouthEast/dx/10/dy/10%7Cimageslim)
![image](http://os78f4hny.bkt.clouddn.com/201709110017.jpg?imageMogr2/blur/1x0/quality/75%7Cwatermark/1/image/aHR0cDovL29zNzhmNGhueS5ia3QuY2xvdWRkbi5jb20vd2F0ZXJtYXJrLnBuZw==/dissolve/50/gravity/SouthEast/dx/10/dy/10%7Cimageslim)
注意:连接池内的对象用完后,必须调用close方法来把对象放回池子里面去。 ![image](http://os78f4hny.bkt.clouddn.com/201709110018.jpg?imageMogr2/blur/1x0/quality/75%7Cwatermark/1/image/aHR0cDovL29zNzhmNGhueS5ia3QuY2xvdWRkbi5jb20vd2F0ZXJtYXJrLnBuZw==/dissolve/50/gravity/SouthEast/dx/10/dy/10%7Cimageslim)
![image](http://os78f4hny.bkt.clouddn.com/201709110019.jpg?imageMogr2/blur/1x0/quality/75%7Cwatermark/1/image/aHR0cDovL29zNzhmNGhueS5ia3QuY2xvdWRkbi5jb20vd2F0ZXJtYXJrLnBuZw==/dissolve/50/gravity/SouthEast/dx/10/dy/10%7Cimageslim)
把其中的一个关闭,则第四个又能连接上了,如果在老版本的DBCP里面,这种情况下,第四个的hashcode和第三个的hashcode是一样的,这就说明,其实是第三个连接完了之后把连接对象还回池子里面了,而第四个连的还是同一个对象。 ![image](http://os78f4hny.bkt.clouddn.com/201709110020.jpg?imageMogr2/blur/1x0/quality/75%7Cwatermark/1/image/aHR0cDovL29zNzhmNGhueS5ia3QuY2xvdWRkbi5jb20vd2F0ZXJtYXJrLnBuZw==/dissolve/50/gravity/SouthEast/dx/10/dy/10%7Cimageslim)
C3P0的优势就是速度快、占用内存小。DBCP的2版本在资源管理上进行了改善,DBCP的1版本在资源管理上面做的不好。 C3P0是开源的数据源插件,可以进行自动的资源回收,主要用于harbinet和spring这两个第三方框架上使用的比较多。 ![image](http://os78f4hny.bkt.clouddn.com/201709110021.jpg?imageMogr2/blur/1x0/quality/75%7Cwatermark/1/image/aHR0cDovL29zNzhmNGhueS5ia3QuY2xvdWRkbi5jb20vd2F0ZXJtYXJrLnBuZw==/dissolve/50/gravity/SouthEast/dx/10/dy/10%7Cimageslim)
在maven中下载。 ![image](http://os78f4hny.bkt.clouddn.com/201709110022.jpg?imageMogr2/blur/1x0/quality/75%7Cwatermark/1/image/aHR0cDovL29zNzhmNGhueS5ia3QuY2xvdWRkbi5jb20vd2F0ZXJtYXJrLnBuZw==/dissolve/50/gravity/SouthEast/dx/10/dy/10%7Cimageslim)
主要使用的操作类是上面这个,它的配置信息,可以使用set来设置也可以把信息放在文件中。 ![image](http://os78f4hny.bkt.clouddn.com/201709110023.jpg?imageMogr2/blur/1x0/quality/75%7Cwatermark/1/image/aHR0cDovL29zNzhmNGhueS5ia3QuY2xvdWRkbi5jb20vd2F0ZXJtYXJrLnBuZw==/dissolve/50/gravity/SouthEast/dx/10/dy/10%7Cimageslim)
![image](http://os78f4hny.bkt.clouddn.com/201709110024.jpg?imageMogr2/blur/1x0/quality/75%7Cwatermark/1/image/aHR0cDovL29zNzhmNGhueS5ia3QuY2xvdWRkbi5jb20vd2F0ZXJtYXJrLnBuZw==/dissolve/50/gravity/SouthEast/dx/10/dy/10%7Cimageslim)
![image](http://os78f4hny.bkt.clouddn.com/201709110025.jpg?imageMogr2/blur/1x0/quality/75%7Cwatermark/1/image/aHR0cDovL29zNzhmNGhueS5ia3QuY2xvdWRkbi5jb20vd2F0ZXJtYXJrLnBuZw==/dissolve/50/gravity/SouthEast/dx/10/dy/10%7Cimageslim)
实际上DBCP和C3P0里面的操作类都是实现了javax.sql.DataSource这个接口的。 以后公司一般使用C3P0好一点,对资源的优化好一点。 ![image](http://os78f4hny.bkt.clouddn.com/201709110026.jpg?imageMogr2/blur/1x0/quality/75%7Cwatermark/1/image/aHR0cDovL29zNzhmNGhueS5ia3QuY2xvdWRkbi5jb20vd2F0ZXJtYXJrLnBuZw==/dissolve/50/gravity/SouthEast/dx/10/dy/10%7Cimageslim)
C3P0的好处是:能够自动回收资源, 在DBManager中利用C3P0来获得连接对象: ![image](http://os78f4hny.bkt.clouddn.com/201709110027.jpg?imageMogr2/blur/1x0/quality/75%7Cwatermark/1/image/aHR0cDovL29zNzhmNGhueS5ia3QuY2xvdWRkbi5jb20vd2F0ZXJtYXJrLnBuZw==/dissolve/50/gravity/SouthEast/dx/10/dy/10%7Cimageslim)
|