19 、连接池
创建Connection的过程是非常耗时的,为了保证Conection可以重用。应该将Connection进行池管理。
使用静态工厂方法管理一个唯一的连接:
package cn.itcast.utils; import java.sql.Connection; import java.sql.Driver; import java.sql.DriverManager; import java.util.Enumeration; /** * 用静态工厂方法管理一个唯一的可重用的连接 */ public class ConnUtils { private ConnUtils(){} private static Connection con; //在静态代码块中创建与数据库的连接 static{ try{ Class.forName("com.mysql.jdbc.Driver"); String url = "jdbc:mysql:///db909?characterEncoding=UTf8"; con = DriverManager.getConnection(url,"root","1234"); }catch(Exception e){ throw new RuntimeException(e.getMessage(),e); } } //使用一个静态方法-静态工厂方法,返回connection实例 public static Connection getCon(){ //静态工厂方法 return con; } }
2 连接池的实现
800x600
Normal
0
7.8 磅
0
2
false
false
false
EN-US
ZH-CN
X-NONE
MicrosoftInternetExplorer4
/* Style Definitions */
table.MsoNormalTable
{mso-style-name:普通表格;
mso-tstyle-rowband-size:0;
mso-tstyle-colband-size:0;
mso-style-noshow:yes;
mso-style-priority:99;
mso-style-parent:"";
mso-padding-alt:0cm 5.4pt 0cm 5.4pt;
mso-para-margin:0cm;
mso-para-margin-bottom:.0001pt;
mso-pagination:widow-orphan;
font-size:10.0pt;
font-family:"Times New Roman","serif";}
为什么要有连接池
1:维护多个连接。必须要保证每一个线程获取到的是不同的connection对象。
2:提供一个方法可以回收连接。
以下是最基本的实现:
package cn.itcast.utils; import java.sql.Connection; import java.sql.DriverManager; import java.util.ArrayList; import java.util.List; public class ConnUtils2 { //声明一个容器,放所有声明的连接Connection private static List<Connection> pool = new ArrayList<Connection>(); static{ try{ Class.forName("com.mysql.jdbc.Driver"); String url = "jdbc:mysql:///db909?characterEncoding=UTf8"; for(int i=0;i<3;i++){ //创建三个连接 Connection con = DriverManager.getConnection(url,"root","1234"); //将这个三个连接放到pool中去 pool.add(con); } System.err.println("连接是:"+pool); }catch(Exception e){ throw new RuntimeException(e.getMessage(),e); } } public static Connection getCon(){ synchronized (pool) { Connection con = pool.remove(0); try { Thread.sleep(100); } catch (InterruptedException e) { e.printStackTrace(); } System.err.println("还有:"+pool.size()); return con; } } //手工的还连接 public static void back(Connection con){ System.err.println("还连接:"+con); pool.add(con); } }
800x600
Normal
0
7.8 磅
0
2
false
false
false
EN-US
ZH-CN
X-NONE
MicrosoftInternetExplorer4
/* Style Definitions */
table.MsoNormalTable
{mso-style-name:普通表格;
mso-tstyle-rowband-size:0;
mso-tstyle-colband-size:0;
mso-style-noshow:yes;
mso-style-priority:99;
mso-style-parent:"";
mso-padding-alt:0cm 5.4pt 0cm 5.4pt;
mso-para-margin:0cm;
mso-para-margin-bottom:.0001pt;
mso-pagination:widow-orphan;
font-size:10.0pt;
font-family:"Times New Roman","serif";}
为了回收连接。我们应该重写close方法。对close方法增强。
v\:* {behavior:url(#default#VML);}
o\:* {behavior:url(#default#VML);}
w\:* {behavior:url(#default#VML);}
.shape {behavior:url(#default#VML);}
800x600
Normal
0
7.8 磅
0
2
false
false
false
EN-US
ZH-CN
X-NONE
MicrosoftInternetExplorer4
/* Style Definitions */
table.MsoNormalTable
{mso-style-name:普通表格;
mso-tstyle-rowband-size:0;
mso-tstyle-colband-size:0;
mso-style-noshow:yes;
mso-style-priority:99;
mso-style-parent:"";
mso-padding-alt:0cm 5.4pt 0cm 5.4pt;
mso-para-margin:0cm;
mso-para-margin-bottom:.0001pt;
mso-pagination:widow-orphan;
font-size:10.0pt;
font-family:"Times New Roman","serif";}