Advertisement
see613

Example #2: MysqlConnectionPool

Nov 5th, 2013
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 2.38 KB | None | 0 0
  1. package see613.core.db;
  2.  
  3. import java.sql.Connection;
  4. import java.sql.DriverManager;
  5. import java.sql.SQLException;
  6. import java.util.LinkedList;
  7. import java.util.Queue;
  8.  
  9. import see613.core.log.ILog;
  10.  
  11. public class MysqlConnectionPool implements IConnectionPool {
  12.     private Queue<Connection> pool = new LinkedList<Connection>();
  13.     private ILog log;
  14.     private String address;
  15.     private String login;
  16.     private String pass;
  17.     public int connectTimeout = 3;
  18.    
  19.    
  20.     public Connection get() throws Exception {
  21.         synchronized (pool) {
  22.             Connection connect;
  23.            
  24.             while (true) {
  25.                 connect = pool.poll();
  26.                
  27.                 if ( connect == null ) {
  28.                     break;
  29.                 }
  30.                
  31.                 if ( isValid(connect) ) {
  32.                     return connect;
  33.                 }
  34.             }
  35.         }
  36.        
  37.         return create();
  38.     }
  39.    
  40.     public void add(Connection connect) {
  41.         synchronized (pool) {
  42.             pool.add(connect);
  43.         }
  44.     }
  45.  
  46.     private Connection create() throws Exception {
  47.         Connection connect = null;
  48.        
  49.         try {        
  50.             Class.forName("com.mysql.jdbc.Driver");
  51.             connect = DriverManager.getConnection( "jdbc:mysql://"+ getAddress(), getLogin(), getPass() );       
  52.              
  53.             getLog().notice("new mysql connection: "+ getAddress() );
  54.              
  55.         } catch (Exception e) {
  56.             close(connect);
  57.             connect = null;
  58.             throw e;
  59.         }  
  60.         return connect;
  61.     }
  62.    
  63.     public void close(Connection connect) throws Exception {
  64.         if (connect != null) {
  65.             connect.close();
  66.         }
  67.     }  
  68.    
  69.     private Boolean isValid(Connection connect) throws SQLException {
  70.         try {
  71.             return connect.isValid( connectTimeout );
  72.            
  73.         } catch (SQLException e) {
  74.             throw e;
  75.         }
  76.     }
  77.    
  78.     public void setLog(ILog value) {
  79.         log = value;
  80.     }
  81.    
  82.     private ILog getLog() {
  83.         if ( log == null ) {
  84.             throw new NullPointerException("log is null");
  85.         }
  86.         return log;
  87.     }
  88.      
  89.     public void setAddress(String value) {
  90.         address = value;
  91.     }
  92.        
  93.     private String getAddress() {
  94.         if ( address == null ) {
  95.             throw new NullPointerException("address is null");
  96.         }
  97.         return address;
  98.     }
  99.      
  100.     public void setLogin(String value) {
  101.         login = value;
  102.     }
  103.        
  104.     private String getLogin() {
  105.         if ( login == null ) {
  106.             throw new NullPointerException("login is null");
  107.         }
  108.         return login;
  109.     }
  110.      
  111.     public void setPass(String value) {
  112.         pass = value;
  113.     }
  114.        
  115.     private String getPass() {
  116.         if ( pass == null ) {
  117.             throw new NullPointerException("pass is null");
  118.         }
  119.         return pass;
  120.     }  
  121.    
  122. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement