mjain

Connection pool implementation

Jun 19th, 2019
35
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.17 KB | None | 0 0
  1. package testconnpoolproject;
  2.  
  3. import java.util.HashMap;
  4. import java.util.Iterator;
  5. import java.util.Set;
  6.  
  7. public abstract class TestPool<T> {
  8.     long             deadTime;
  9.     HashMap<T, Long> available;
  10.     HashMap<T, Long> used;
  11.  
  12.     TestPool() {
  13.         deadTime = 50 * 1000;
  14.         available = new HashMap<T, Long>();
  15.         used = new HashMap<T, Long>();
  16.     }
  17.  
  18.     abstract T create();
  19.  
  20.     abstract long dead(T o);
  21.  
  22.     abstract boolean validate(T o);
  23.  
  24.     synchronized void putBack(T obj) {
  25.         used.remove(obj);
  26.         available.put(obj, System.currentTimeMillis());
  27.     }
  28.  
  29.     synchronized T getObject() {
  30.         long cuTime = System.currentTimeMillis();
  31.         T t = null;
  32.         if (available.size() > 0) {
  33.             Set<T> keys = available.keySet();
  34.             if (keys != null && keys.size() > 0) {
  35.                 Iterator<T> iter = keys.iterator();
  36.                 if (iter.hasNext()) {
  37.                     t = iter.next();
  38.                     Long sleepTime = available.get(t);
  39.                     if ((cuTime - sleepTime) > deadTime) {
  40.                         available.remove(t);
  41.                         dead(t);
  42.                         t = null;
  43.                     } else {
  44.                         if (validate(t)) {
  45.                             available.remove(t);
  46.                             used.put(t, cuTime);
  47.                             return (t);
  48.                         } else {
  49.                             available.remove(t);
  50.                             dead(t);
  51.                             t = null;
  52.                         }
  53.                     }
  54.                 }
  55.             }
  56.         }
  57.  
  58.         t = create();
  59.         available.put(t, cuTime);
  60.         return t;
  61.     }
  62.  
  63. }
  64.  
  65.  
  66. package testconnpoolproject;
  67.  
  68. import java.sql.Connection;
  69. import java.sql.DriverManager;
  70. import java.sql.SQLException;
  71.  
  72. public class MyConnectionPool extends TestPool<Connection> {
  73.     String dsn, usr, pwd;
  74.  
  75.     MyConnectionPool(String driver, String dsn, String usr, String pwd) {
  76.         super();
  77.         try {
  78.             Class.forName(driver).newInstance();
  79.         } catch (Exception e) {
  80.             e.printStackTrace();
  81.         }
  82.         this.dsn = dsn;
  83.         this.usr = usr;
  84.         this.pwd = pwd;
  85.     }
  86.  
  87.     @Override
  88.     Connection create() {
  89.         try {
  90.             return DriverManager.getConnection(dsn, usr, pwd);
  91.         } catch (SQLException e) {
  92.             e.printStackTrace();
  93.         }
  94.         return null;
  95.     }
  96.  
  97.     @Override
  98.     long dead(Connection o) {
  99.         try {
  100.             o.close();
  101.         } catch (SQLException e) {
  102.             e.printStackTrace();
  103.         }
  104.         return 0;
  105.     }
  106.  
  107.     @Override
  108.     boolean validate(Connection o) {
  109.         try {
  110.             return (!((Connection) o).isClosed());
  111.         } catch (SQLException e) {
  112.             e.printStackTrace();
  113.             return (false);
  114.         }
  115.     }
  116.  
  117. }
  118.  
  119.  
  120. public class ConnectionPool {
  121.     public static void main() {
  122.         MyConnectionPool conn = new MyConnectionPool("org.hsqldb.jdbcDriver", "jdbc:hsqldb: //localhost/mydb", "sa",
  123.                 "password");
  124.         conn.getObject();
  125.     }
  126. }
Add Comment
Please, Sign In to add comment