Advertisement
Guest User

Untitled

a guest
May 27th, 2017
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.85 KB | None | 0 0
  1. class ConnectionPoolManager {
  2.  
  3. String databaseUrl = "jdbc:mysql://192.168.X.X:3306/database";
  4. String userName = "name";
  5. String password = "pass";
  6. final int MAX_POOL_SIZE = 50;
  7. Vector connectionPool = new Vector();
  8.  
  9. public ConnectionPoolManager() {
  10. initialize();
  11. }
  12.  
  13. public ConnectionPoolManager(
  14. //String databaseName,
  15. String databaseUrl,
  16. String userName,
  17. String password) {
  18. this.databaseUrl = databaseUrl;
  19. this.userName = userName;
  20. this.password = password;
  21. initialize();
  22. }
  23.  
  24. private void initialize() {
  25. //Here we can initialize all the information that we need
  26. initializeConnectionPool();
  27. }
  28.  
  29. private void initializeConnectionPool() {
  30. while (!checkIfConnectionPoolIsFull()) {
  31. System.out.println("Connection Pool is NOT full. Proceeding with adding new connections");
  32. //Adding new connection instance until the pool is full
  33. connectionPool.addElement(createNewConnectionForPool());
  34. }
  35. System.out.println("Connection Pool is full.");
  36. }
  37.  
  38. private synchronized boolean checkIfConnectionPoolIsFull() {
  39.  
  40. //Check if the pool size
  41. if (connectionPool.size() < MAX_POOL_SIZE) {
  42. return false;
  43. }
  44.  
  45. return true;
  46. }
  47.  
  48. //Creating a connection
  49. private Connection createNewConnectionForPool() {
  50. Connection connection = null;
  51.  
  52. try {
  53. Class.forName("com.mysql.jdbc.Driver");
  54. connection = DriverManager.getConnection(databaseUrl, userName, password);
  55. System.out.println("Connection: " + connection);
  56. } catch (SQLException sqle) {
  57. System.err.println("SQLException: " + sqle);
  58. return null;
  59. } catch (ClassNotFoundException cnfe) {
  60. System.err.println("ClassNotFoundException: " + cnfe);
  61. return null;
  62. }
  63.  
  64. return connection;
  65. }
  66.  
  67. public synchronized Connection getConnectionFromPool() {
  68. Connection connection = null;
  69.  
  70. //Check if there is a connection available. There are times when all the connections in the pool may be used up
  71. if (connectionPool.size() > 0) {
  72. connection = (Connection) connectionPool.firstElement();
  73. connectionPool.removeElementAt(0);
  74. }
  75. //Giving away the connection from the connection pool
  76. return connection;
  77. }
  78.  
  79. public synchronized void returnConnectionToPool(Connection connection) {
  80. //Adding the connection from the client back to the connection pool
  81. connectionPool.addElement(connection);
  82. }
  83.  
  84. /*public static void main(String args[])
  85. {
  86. ConnectionPoolManager ConnectionPoolManager = new ConnectionPoolManager();
  87. }*/
  88. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement