Advertisement
Guest User

ertg

a guest
Jul 24th, 2014
208
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.40 KB | None | 0 0
  1. package org.yveltal.database;
  2.  
  3. import java.sql.Connection;
  4. import java.sql.DriverManager;
  5. import java.util.Hashtable;
  6. import java.util.Map;
  7. import java.util.concurrent.Callable;
  8. import java.util.concurrent.ExecutorService;
  9. import java.util.concurrent.Executors;
  10. import java.util.concurrent.Future;
  11.  
  12. /**
  13.  *
  14.  * @author Sino
  15.  *
  16.  */
  17. public class DatabasePool {
  18.     /**
  19.      *
  20.      */
  21.     private final ExecutorService connectionPool = Executors.newCachedThreadPool();
  22.    
  23.     /**
  24.      *
  25.      */
  26.     private final Map<String, Connection> connectionsCache = new Hashtable<String, Connection>();
  27.    
  28.     /**
  29.      *
  30.      */
  31.     private Connection connection;
  32.    
  33.     /**
  34.      *
  35.      */
  36.     public DatabasePool() {
  37.         try {
  38.             Class.forName("com.mysql.jdbc.Driver");
  39.         } catch (Exception e) {
  40.             e.printStackTrace();
  41.         }
  42.     }
  43.    
  44.     /**
  45.      *
  46.      * @return
  47.      */
  48.     public void establishConnection(final Database database) {
  49.         try {
  50.             //Throw a callback and use a Future to asynchronously establish the connection
  51.             final Future<Connection> connectionFuture = connectionPool.submit(new Callable<Connection>() {
  52.                 @Override
  53.                 public Connection call() throws Exception {
  54.                     return DriverManager.getConnection("", database.getUsername(), database.getPassword());
  55.                 }
  56.             });
  57.            
  58.             //Stores this connection if successfully established into our connections cache
  59.             connectionsCache.put(database.getDatabaseName(), connection = connectionFuture.get());
  60.         } catch (Exception e) {
  61.             e.printStackTrace();
  62.         }
  63.     }
  64.    
  65.     /**
  66.      *
  67.      * @param runnable
  68.      * @return
  69.      */
  70.     public Future<?> submit(Runnable task) {
  71.         if (connectionsCache.isEmpty()) {
  72.             throw new IllegalStateException("No connection ready for submission");
  73.         }
  74.         return connectionPool.submit(task);
  75.     }
  76.    
  77.     /**
  78.      *
  79.      * @param statement
  80.      * @return
  81.      */
  82.     public Future<?> prepare(final String databaseName, final DatabaseStatement statement) throws Exception {
  83.         if (connectionsCache.containsKey(databaseName)) {
  84.             final Future<?> prepare = connectionPool.submit(new Runnable() {
  85.                 @Override
  86.                 public void run() {
  87.                     statement.execute(connectionsCache.get(databaseName));
  88.                 }
  89.             });
  90.             return prepare;
  91.         }
  92.        
  93.         return null;
  94.     }
  95.    
  96.     /**
  97.      *
  98.      * @return
  99.      */
  100.     public Connection getConnection() {
  101.         return connection;
  102.     }
  103.    
  104.     /**
  105.      *
  106.      * @return
  107.      */
  108.     public Connection getConnection(String databaseName) {
  109.         return connectionsCache.get(databaseName);
  110.     }
  111.  
  112. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement