Advertisement
Guest User

Untitled

a guest
Aug 1st, 2017
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.70 KB | None | 0 0
  1. public class ConnectionPoolManager {
  2.  
  3. private static org.apache.log4j.Logger log;
  4. private String databaseUrl;
  5. String userName = null;
  6. String password = null;
  7. Properties prop = new Properties();
  8. Vector connectionPool = new Vector();
  9.  
  10. public ConnectionPoolManager() {
  11.  
  12. databaseUrl();
  13. initialize();
  14.  
  15.  
  16. }
  17.  
  18. public void databaseUrl() {
  19.  
  20. try {
  21. URL url = getClass().getClassLoader().getResource("database.properties");
  22.  
  23. // load a properties file
  24. prop.load(new FileInputStream(url.getPath()));
  25. // get the property value
  26. databaseUrl = prop.getProperty("database");
  27. } catch (IOException ex) {
  28. log.error("Date & Time: " + new Date() + ", Class Name : ConnectionPoolManager, Method Name : databaseUrl"
  29. + "n" + "Error : " + ex.getMessage());
  30. } catch (Exception e) {
  31. log.error("Date & Time: " + new Date() + ", Class Name : ConnectionPoolManager, Method Name : databaseUrl"
  32. + "n" + "Error : " + e.getMessage());
  33. }
  34. userName = prop.getProperty("username");
  35. password = prop.getProperty("password");
  36.  
  37. }
  38.  
  39.  
  40. public ConnectionPoolManager(String databaseUrl, String userName, String password) {
  41. this.databaseUrl = databaseUrl;
  42. this.userName = userName;
  43. this.password = password;
  44. initialize();
  45. }
  46.  
  47. private void initialize() {
  48. // Here we can initialize all the information that we need
  49. initializeConnectionPool();
  50. }
  51.  
  52. private void initializeConnectionPool() {
  53. while (!checkIfConnectionPoolIsFull()) {
  54. System.out.println("Connection Pool is NOT full. Proceeding with adding new connections");
  55. // Adding new connection instance until the pool is full
  56. connectionPool.addElement(createNewConnectionForPool());
  57. }
  58. System.out.println("Connection Pool is full");
  59. }
  60.  
  61. private synchronized boolean checkIfConnectionPoolIsFull() {
  62. final int MAX_POOL_SIZE = 5;
  63.  
  64. // Check if the pool size
  65. if (connectionPool.size() < 5) {
  66. return false;
  67. }
  68.  
  69. return true;
  70. }
  71.  
  72. // Creating a connection
  73. private Connection createNewConnectionForPool() {
  74. Connection connection = null;
  75.  
  76. try {
  77. Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
  78. // connection = DriverManager.getConnection(databaseUrl, userName,
  79. // password);
  80. connection = DriverManager.getConnection(databaseUrl);
  81.  
  82. } catch (SQLException sqle) {
  83. System.err.println("SQLException: " + sqle);
  84. return null;
  85. } catch (ClassNotFoundException cnfe) {
  86. System.err.println("ClassNotFoundException: " + cnfe);
  87. return null;
  88. }
  89.  
  90. return connection;
  91. }
  92.  
  93. public synchronized Connection getConnectionFromPool() {
  94.  
  95. Connection connection = null;
  96.  
  97. // Check if there is a connection available. There are times when all
  98. // the connections in the pool may be used up
  99. if (connectionPool.size() > 0) {
  100. connection = (Connection) connectionPool.firstElement();
  101. connectionPool.removeElementAt(0);
  102. }
  103. // Giving away the connection from the connection pool
  104. return connection;
  105. }
  106.  
  107. /**
  108. * The method adds the connection from the client back to the connection
  109. * pool
  110. *
  111. * @return void
  112. * @param connection
  113. */
  114. public synchronized void returnConnectionToPool(Connection connection) {
  115. // Adding the connection from the client back to the connection pool
  116. connectionPool.addElement(connection);
  117. }
  118.  
  119. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement