Advertisement
Guest User

Untitled

a guest
Oct 24th, 2018
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.87 KB | None | 0 0
  1. package by.epam.casino.dao.pool;
  2.  
  3. import java.sql.Connection;
  4. import java.sql.DriverManager;
  5. import java.sql.SQLException;
  6. import java.util.Set;
  7. import java.util.concurrent.BlockingQueue;
  8. import java.util.concurrent.ConcurrentSkipListSet;
  9. import java.util.concurrent.LinkedBlockingQueue;
  10.  
  11. import by.epam.casino.exception.PersistentException;
  12.  
  13. final public class ConnectionPool {
  14. //private static Logger logger = Logger.getLogger(ConnectionPool.class);
  15.  
  16. private String url;
  17. private String user;
  18. private String password;
  19. private int maxSize;
  20. private int checkConnectionTimeout;
  21.  
  22. private BlockingQueue<PooledConnection> freeConnections = new LinkedBlockingQueue<>();
  23. private Set<PooledConnection> usedConnections = new ConcurrentSkipListSet<>();
  24.  
  25. private ConnectionPool() {}
  26.  
  27. public synchronized Connection getConnection() throws PersistentException {
  28. PooledConnection connection = null;
  29. while(connection == null) {
  30. try {
  31. if(!freeConnections.isEmpty()) {
  32. connection = freeConnections.take();
  33. if(!connection.isValid(checkConnectionTimeout)) {
  34. try {
  35. connection.getConnection().close();
  36. } catch(SQLException e) {}
  37. connection = null;
  38. }
  39. } else if(usedConnections.size() < maxSize) {
  40. connection = createConnection();
  41. } else {
  42. System.out.println("The limit of number of database connections is exceeded");
  43. throw new PersistentException();
  44. }
  45. } catch(InterruptedException | SQLException e) {
  46. System.out.println("It is impossible to connect to a database" + e);
  47. throw new PersistentException(e);
  48. }
  49. }
  50. usedConnections.add(connection);
  51. System.out.println(String.format("Connection was received from pool. Current pool size: %d used connections; %d free connection", usedConnections.size(), freeConnections.size()));
  52. return connection;
  53. }
  54.  
  55. synchronized void freeConnection(PooledConnection connection) {
  56. try {
  57. if(connection.isValid(checkConnectionTimeout)) {
  58. connection.clearWarnings();
  59. connection.setAutoCommit(true);
  60. usedConnections.remove(connection);
  61. freeConnections.put(connection);
  62. System.out.println(String.format("Connection was returned into pool. Current pool size: %d used connections; %d free connection", usedConnections.size(), freeConnections.size()));
  63. }
  64. } catch(SQLException | InterruptedException e1) {
  65. System.out.println("It is impossible to return database connection into pool" + e1);
  66. try {
  67. connection.getConnection().close();
  68. } catch(SQLException e2) {}
  69. }
  70. }
  71.  
  72. public synchronized void init(String driverClass, String url, String user, String password, int startSize, int maxSize, int checkConnectionTimeout) throws PersistentException {
  73. try {
  74. destroy();
  75. Class.forName(driverClass);
  76. this.url = url;
  77. this.user = user;
  78. this.password = password;
  79. this.maxSize = maxSize;
  80. this.checkConnectionTimeout = checkConnectionTimeout;
  81. for(int counter = 0; counter < startSize; counter++) {
  82. freeConnections.put(createConnection());
  83. }
  84. } catch(ClassNotFoundException | SQLException | InterruptedException e) {
  85. System.out.println("It is impossible to initialize connection pool" + e);
  86. throw new PersistentException(e);
  87. }
  88. }
  89.  
  90. private static ConnectionPool instance = new ConnectionPool();
  91.  
  92. public static ConnectionPool getInstance() {
  93. return instance;
  94. }
  95.  
  96. private PooledConnection createConnection() throws SQLException {
  97. return new PooledConnection(DriverManager.getConnection(url, user, password));
  98. }
  99.  
  100. public synchronized void destroy() {
  101. usedConnections.addAll(freeConnections);
  102. freeConnections.clear();
  103. for(PooledConnection connection : usedConnections) {
  104. try {
  105. connection.getConnection().close();
  106. } catch(SQLException e) {}
  107. }
  108. usedConnections.clear();
  109. }
  110.  
  111. @Override
  112. protected void finalize() throws Throwable {
  113. destroy();
  114. }
  115. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement