資料庫連線池---(一)
阿新 • • 發佈:2018-11-29
一般我們要建立一個數據連線池存放一些Connection連線,當然了,不要放太多,比方說我這裡就只放了三個做例子而已。
每當我們需要使用Connection連線的時候就從中取出來,當他沒有了的時候就等待,等有用完了歡回來了就再取。
當一個事務完成了就會將連線還回去。
具體示例:
配置檔案jdbc.properties
因為我沒有設定密碼,因此上面的密碼我讓他空了#mysql driver=com.mysql.jdbc.Driver url=jdbc:mysql:///mydata?useUnicode=true&characterEncoding=utf-8 userName=root password=
連線池ConnsUtil.java
package cn.hncu.pool; import java.sql.Connection; import java.sql.DriverManager; import java.util.ArrayList; import java.util.List; import java.util.Properties; import cn.hncu.utils.ConnectionFactory; public class ConnsUtil { private static List<Connection> pool=new ArrayList<Connection>(); private static int num=3; static { try { //讀取配置檔案 Properties p=new Properties(); p.load(ConnectionFactory.class.getClassLoader().getResourceAsStream("jdbc.properties")); String driver=p.getProperty("driver"); String url=p.getProperty("url"); String userName=p.getProperty("userName"); String password=p.getProperty("password"); Class.forName(driver); for (int i=0;i<num;i++){ Connection con=DriverManager.getConnection(url, userName, password); pool.add(con); } } catch (Exception e) { e.printStackTrace(); } } public static synchronized Connection getConnection() throws Exception{ if (pool.size()<=0){ Thread.sleep(100); return getConnection(); } return pool.remove(0); } public static void back(Connection con){ pool.add(con); System.out.println("還回來一個連線"); } }
測試TestPool.java
package cn.hncu.pool; import java.sql.Connection; import java.sql.SQLException; import java.sql.Statement; public class TestPool { public static void main(String[] args) { Connection con=null; try { con=ConnsUtil.getConnection(); con.setAutoCommit(false); new SecondThread(1).start(); new SecondThread(2).start(); new SecondThread(3).start(); new SecondThread(4).start(); new SecondThread(5).start(); Statement st=con.createStatement(); String sql="insert into stud values('p3001','鳳姐',50)"; st.execute(sql); sql="insert into stud values('p3002','芙蓉姐',40)"; st.execute(sql); System.out.println("主執行緒準備提交"); con.commit(); System.out.println("主執行緒提交完畢"); } catch (Exception e) { try { con.rollback(); System.out.println("主執行緒回滾了"); } catch (SQLException e1) { throw new RuntimeException("主執行緒事務回滾失敗", e1); } } finally { try { if (con!=null){ con.setAutoCommit(true); ConnsUtil.back(con); //con.close();//如果要把close內部的功能換成還連線,就需要我們以後的技術來實現 } } catch (SQLException e) { throw new RuntimeException("主執行緒連線關閉失敗", e); } } } } class SecondThread extends Thread{ private int n; public SecondThread(int n) { this.n=n; } @Override public void run() { Connection con=null; try { con=ConnsUtil.getConnection(); con.setAutoCommit(false); Statement st=con.createStatement(); String sql="insert into stud values('p400"+n+"','鳳姐2',50)"; st.execute(sql); sql="insert into stud values('p410"+n+"','芙蓉姐2',40)"; st.execute(sql); System.out.println("第"+n+"個執行緒準備提交"); con.commit(); System.out.println("第"+n+"個執行緒提交完畢"); } catch (Exception e) { try { con.rollback(); System.out.println("第"+n+"個執行緒回滾了"); } catch (SQLException e1) { throw new RuntimeException("第"+n+"個事務回滾失敗", e1); } } finally { try { if (con!=null){ con.setAutoCommit(true); //con.close(); ConnsUtil.back(con); } } catch (SQLException e) { throw new RuntimeException("第"+n+"個連線關閉失敗", e); } } } }