Advertisement
Guest User

Untitled

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