java--数据库连接池2(多例)

时间:2022-02-25 04:41:36

批量创建与数据库的连接,并把创建的连接加入List< Connection> pool对象中。
让getConnection方法每次调用时,从pool中取一个Connection返回给用户。
当用户使用完Connection,本例是调用Connection.back()(规范是调用Connection.close()方法–在数据库连接池2(多例)和 数据库连接池3(多例)中实现)方法时,Collection对象应保证将自己返回到pool中,而不要把conn还给数据库。所以Collection保证将自己返回到pool中是此处编程的难点。

package cn.hncu.pool;

import java.sql.Connection;
import java.sql.DriverManager;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

public class ConnUtils {

private final static List<Connection> pool=new ArrayList<Connection>();
private final static int num=3;
private ConnUtils(){

}
static{
Properties p=new Properties();
try {
// p.load(ConnUtils.class.getClassLoader().getResourceAsStream("jdbc.properties"));
p.load(ClassLoader.getSystemResourceAsStream("jdbc.properties"));
String driver=p.getProperty("driver");
String url=p.getProperty("url");
String name=p.getProperty("username");
String pwd=p.getProperty("password");
Class.forName(driver);
for(int i=0;i<num;i++){
Connection con=DriverManager.getConnection(url, name, pwd);
pool.add(con);
}
} catch (Exception e) {
e.printStackTrace();
}
}
public static synchronized Connection getConnection(){
if(pool.size()<=0){
System.out.println("池中连接没有了...");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
return getConnection();
}
return pool.remove(0);//拿一个移一个
}
public static void back(Connection con){
System.out.println("往池中还入连接"+con);
pool.add(con);
}
}

jdbc.properties

##MySQL
driver=com.mysql.jdbc.Driver
url=jdbc:mysql://127.0.0.1:3306/hncu?useUnicode=true&characterEncoding=utf-8
username=root
password=1234
size=3
##Oracle
#driver=oracle.jdbc.driver.OracleDriver
#url=jdbc:oracle:thin:@127.0.0.1:1521:orcl
#username=scott
#password=tiger

演示

package cn.hncu.tx;

import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;

import cn.hncu.pool.ConnUtils;
import cn.hncu.pubs.ConnFacory;
//使用的是多例的连接池ConnUtils
public class TxDemo2 {

public static void main(String[] args) throws Exception {
Connection con=ConnUtils.getConnection();
System.out.println("main线程获得一个连接:"+con);
Statement st = con.createStatement();

try {
con.setAutoCommit(false);
st.execute("INSERT INTO person VALUES('P200','赵子龙','1')");

new MyThread2(1).start();
new MyThread2(2).start();
new MyThread2(3).start();
new MyThread2(4).start();
new MyThread2(5).start();

Thread.sleep(1000);

con.commit();
System.out.println("main线程提交一个事务....");
} catch (Exception e) {
con.rollback();
System.out.println("main线程回滚一个事务.....");
}finally{
con.setAutoCommit(true);
ConnUtils.back(con);
}

}

}
class MyThread2 extends Thread{
private int num;
public MyThread2(int num){
this.num = num;
}
@Override
public void run() {
Connection con=ConnUtils.getConnection();
System.out.println(num+"线程获得一个连接:"+con);
try {
Statement st = con.createStatement();
con.setAutoCommit(false);
String sql = "INSERT INTO person VALUES('P09"+num+"','赵子龙','1')";
if(num==3){
sql = "INSERT INTO person VALUES('P09"+num+"'赵子龙','1')";
}else{
Thread.sleep(100);
}
st.executeUpdate(sql);
con.commit();
System.out.println(num+"线程提交一个事务....");
} catch (Exception e) {
try {
con.rollback();
System.out.println(num+"线程回滚一个事务.....");
} catch (SQLException e1) {
e1.printStackTrace();
}
}finally{
try {
con.setAutoCommit(true);
ConnUtils.back(con);
} catch (SQLException e) {
e.printStackTrace();
}
}


}

}