Advertisement
Guest User

Untitled

a guest
May 14th, 2016
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.33 KB | None | 0 0
  1. package com.amitcodes.dbcp;
  2.  
  3. import java.sql.Connection;
  4. import java.sql.DriverManager;
  5. import java.sql.SQLException;
  6. import java.util.concurrent.BlockingQueue;
  7. import java.util.concurrent.LinkedBlockingQueue;
  8. import java.util.logging.Level;
  9. import java.util.logging.Logger;
  10.  
  11. public class ConnectionPool {
  12.  
  13. private static final Logger logger = Logger.getLogger(ConnectionPool.class.getCanonicalName());
  14.  
  15. private BlockingQueue<Connection> pool;
  16. /**
  17. * Maximum number of connections that the pool can have
  18. */
  19. private int maxPoolSize;
  20. /**
  21. * Number of connections that should be created initially
  22. */
  23. private int initialPoolSize;
  24. /**
  25. * Number of connections generated so far
  26. */
  27. private int currentPoolSize;
  28.  
  29. private String dbUrl;
  30. private String dbUser;
  31. private String dbPassword;
  32.  
  33. public ConnectionPool(int maxPoolSize, int initialPoolSize, String url, String username,
  34. String password, String driverClassName) throws ClassNotFoundException, SQLException {
  35.  
  36. if ((initialPoolSize > maxPoolSize) || initialPoolSize < 1 || maxPoolSize < 1) {
  37. throw new IllegalArgumentException("Invalid pool size parameters");
  38. }
  39.  
  40. // default max pool size to 10
  41. this.maxPoolSize = maxPoolSize > 0 ? maxPoolSize : 10;
  42. this.initialPoolSize = initialPoolSize;
  43. this.dbUrl = url;
  44. this.dbUser = username;
  45. this.dbPassword = password;
  46. this.pool = new LinkedBlockingQueue<Connection>(maxPoolSize);
  47.  
  48. initPooledConnections(driverClassName);
  49.  
  50. if (pool.size() != initialPoolSize) {
  51. logger.log(Level.WARNING,
  52. "Initial sized pool creation failed. InitializedPoolSize={0}, initialPoolSize={1}",
  53. new Object[]{pool.size(), initialPoolSize});
  54. }
  55.  
  56. }
  57.  
  58. private void initPooledConnections(String driverClassName)
  59. throws ClassNotFoundException, SQLException {
  60.  
  61. // 1. Attempt to load the driver class
  62. Class.forName(driverClassName);
  63.  
  64. // 2. Create and pool connections
  65. for (int i = 0; i < initialPoolSize; i++) {
  66. openAndPoolConnection();
  67. }
  68. }
  69.  
  70. private synchronized void openAndPoolConnection() throws SQLException {
  71. if (currentPoolSize == maxPoolSize) {
  72. return;
  73. }
  74.  
  75. Connection conn = DriverManager.getConnection(dbUrl, dbUser, dbPassword);
  76. pool.offer(new PooledConnection(conn, this));
  77. currentPoolSize++;
  78.  
  79. logger.log(Level.FINE, "Created connection {0}, currentPoolSize={1}, maxPoolSize={2}",
  80. new Object[]{conn, currentPoolSize, maxPoolSize});
  81. }
  82.  
  83. public Connection borrowConnection() throws InterruptedException, SQLException {
  84.  
  85. if (pool.peek()==null && currentPoolSize < maxPoolSize) {
  86. openAndPoolConnection();
  87. }
  88. // Borrowing thread will be blocked till connection
  89. // becomes available in the queue
  90. return pool.take();
  91. }
  92.  
  93. public void surrenderConnection(Connection conn) {
  94. if (!(conn instanceof PooledConnection)) {
  95. return;
  96. }
  97. pool.offer(conn); // offer() as we do not want to go beyond capacity
  98. }
  99. }
  100.  
  101. package com.amitcodes.dbcp;
  102.  
  103. import java.sql.*;
  104. import java.util.*;
  105. import java.util.concurrent.Executor;
  106.  
  107. public class PooledConnection implements Connection {
  108.  
  109. private Connection coreConnection;
  110. private ConnectionPool connectionPool;
  111.  
  112. public PooledConnection(Connection coreConnection, ConnectionPool connectionPool) {
  113. this.connectionPool = connectionPool;
  114. this.coreConnection = coreConnection;
  115. }
  116.  
  117. @Override
  118. public void close() throws SQLException {
  119. connectionPool.surrenderConnection(this);
  120. }
  121.  
  122. /* ****************************************************************
  123. * Proxy Methods
  124. * ****************************************************************/
  125. @Override
  126. public Statement createStatement() throws SQLException {
  127. return coreConnection.createStatement();
  128. }
  129.  
  130. @Override
  131. public PreparedStatement prepareStatement(String sql) throws SQLException {
  132. return coreConnection.prepareStatement(sql);
  133. }
  134.  
  135. // SOME CODE SKIPPED
  136. }
  137.  
  138. this.connectionPool =
  139. checkNotNull(connectionPool, "connectionPool cannot be null");
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement