C3P0代碼分析
class {
// 第一步:靜態初始化快,加載配置文件
e ds = null;
{
利用xml中的文件
ds = new e();
ds.("com.mysql.jdbc.");
ds.("jdbc:mysql://:3306/jdbc");
ds.("root");
ds.("qiao");
ds.(10);
ds.(50);
ds.(10);
}
() {
ds.();
}
// 是把資源歸還到連接池中
void ( con, st,
rs) {
if (rs != null) {
try {
rs.close();
} catch ( e) {
e.();
}
rs = null;
}
if (con != null) {
try {
con.close();
} catch ( e) {
e.();
}
con = null;
}
if (st != null) {
try {
st.close();
} catch ( e) {
// TODO Auto- catch block
e.();
}
}
}
注:最核心的地方就是靜態初始化()里的東西,也就相當于是配置文件中的數值設置,即是數據庫的連接數據庫連接池代碼實現,我們完全可以把這么數據寫成一個xml文檔,用來存儲這些數據數據庫連接池代碼實現,xml文檔為c3p0-.xml
"">com.mysql.jdbc.
"">jdbc:mysql://:3306/jdbc
"user">root
"">qiao
"">
"">30000
"riod">30
"">10
"">30
"">100
"">10
"">200
"mysql">
"">com.mysql.jdbc.
"">jdbc:mysql://:3306/jdbc
"user">root
"">qiao
"">50
"">100
"">50
"">1000
所有上面的代碼可以寫為:
class {
// 第一步:靜態初始化塊,加載配置文件
e ds = null;
{
//利用xml中的文件
ds = new e("mysql");
}
() {
ds.();
}
// 是把資源歸還到連接池中
void ( con, st,
rs) {
if (rs != null) {
try {
rs.close();
} catch ( e) {
e.();
}
rs = null;
}
if (con != null) {
try {
con.close();
} catch ( e) {
e.();
}
con = null;
}
if (st != null) {
try {
st.close();
} catch ( e) {
// TODO Auto- catch block
e.();
}
}
}
注:ds = new e("mysql");中的參數是xml文檔中的命名的name,也可以用默認的,如果沒有指定則為默認的。