JDBC數據源(DataSource)的簡單實現


前言:

    數據源技術是Java操作數據庫的一個很關鍵技術,流行的持久化框架都離不開數據源的應用。

數據源提供了一種簡單獲取數據庫連接的方式,並能在內部通過一個池的機制來復用數據庫連接,這樣就大大減少創建數據庫連接的次數,提高了系統性能。

對於數據源的應用,一般都選擇實用開源的數據源或數據庫連接池來使用,比如,常見的有DBCP、C3P0、Proxool等等。但用起來有些笨重和麻煩。下面自己手動實現個精簡的數據源,代碼如下:

 

  1 import org.apache.commons.logging.Log; 
  2 import org.apache.commons.logging.LogFactory; 
  3 
  4 import javax.sql.DataSource; 
  5 import java.util.Collections; 
  6 import java.util.LinkedList; 
  7 import java.sql.Connection; 
  8 import java.sql.SQLException; 
  9 import java.sql.DriverManager; 
 10 import java.io.PrintWriter; 
 11 
 12 /** 
 13 * 一個簡單的DataSource實現 
 14 * 
 15 * @author leizhimin 2010-1-14 0:03:17 
 16 */ 
 17 public class SimpleDateSource implements DataSource { 
 18         private static Log log = LogFactory.getLog(SimpleDateSource.class); 
 19         private static final String dirverClassName = "com.mysql.jdbc.Driver"; 
 20         private static final String url = "jdbc:mysql://127.0.0.1:3306/testdb"; 
 21         private static final String user = "root"; 
 22         private static final String pswd = "leizhimin"; 
 23         //連接池 
 24         private static LinkedList<Connection> pool = (LinkedList<Connection>) Collections.synchronizedList(new LinkedList<Connection>()); 
 25         private static SimpleDateSource instance = new SimpleDateSource(); 
 26 
 27         static { 
 28                 try { 
 29                         Class.forName(dirverClassName); 
 30                 } catch (ClassNotFoundException e) { 
 31                         log.error("找不到驅動類!", e); 
 32                 } 
 33         } 
 34 
 35         private SimpleDateSource() { 
 36         } 
 37 
 38         /** 
 39          * 獲取數據源單例 
 40          * 
 41          * @return 數據源單例 
 42          */ 
 43         public SimpleDateSource instance() { 
 44                 if (instance == null) instance = new SimpleDateSource(); 
 45                 return instance; 
 46         } 
 47 
 48         /** 
 49          * 獲取一個數據庫連接 
 50          * 
 51          * @return 一個數據庫連接 
 52          * @throws SQLException 
 53          */ 
 54         public Connection getConnection() throws SQLException { 
 55                 synchronized (pool) { 
 56                         if (pool.size() > 0) return pool.removeFirst(); 
 57                         else return makeConnection(); 
 58                 } 
 59         } 
 60 
 61         /** 
 62          * 連接歸池 
 63          * 
 64          * @param conn 
 65          */ 
 66         public static void freeConnection(Connection conn) { 
 67                 pool.addLast(conn); 
 68         } 
 69 
 70         private Connection makeConnection() throws SQLException { 
 71                 return DriverManager.getConnection(url, user, pswd); 
 72         } 
 73 
 74         public Connection getConnection(String username, String password) throws SQLException { 
 75                 return DriverManager.getConnection(url, username, password); 
 76         } 
 77 
 78         public PrintWriter getLogWriter() throws SQLException { 
 79                 return null; 
 80         } 
 81 
 82         public void setLogWriter(PrintWriter out) throws SQLException { 
 83 
 84         } 
 85 
 86         public void setLoginTimeout(int seconds) throws SQLException { 
 87 
 88         } 
 89 
 90         public int getLoginTimeout() throws SQLException { 
 91                 return 0; 
 92         } 
 93 
 94         public <T> T unwrap(Class<T> iface) throws SQLException { 
 95                 return null; 
 96         } 
 97 
 98         public boolean isWrapperFor(Class<?> iface) throws SQLException { 
 99                 return false; 
100         } 
101 }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM