|
1 | | -package com.baeldung.connectionpool.connectionpools; |
2 | | - |
3 | | -import java.sql.Connection; |
4 | | -import java.sql.DriverManager; |
5 | | -import java.sql.SQLException; |
6 | | -import java.util.ArrayList; |
7 | | -import java.util.List; |
8 | | - |
9 | | -public class BasicConnectionPool implements ConnectionPool { |
10 | | - |
11 | | - private final String url; |
12 | | - private final String user; |
13 | | - private final String password; |
14 | | - private final List<Connection> connectionPool; |
15 | | - private final List<Connection> usedConnections = new ArrayList<>(); |
16 | | - private static final int INITIAL_POOL_SIZE = 10; |
17 | | - private final int MAX_POOL_SIZE = 20; |
18 | | - |
19 | | - public static BasicConnectionPool create(String url, String user, String password) throws SQLException { |
20 | | - List<Connection> pool = new ArrayList<>(INITIAL_POOL_SIZE); |
21 | | - for (int i = 0; i < INITIAL_POOL_SIZE; i++) { |
22 | | - pool.add(createConnection(url, user, password)); |
23 | | - } |
24 | | - return new BasicConnectionPool(url, user, password, pool); |
25 | | - } |
26 | | - |
27 | | - private BasicConnectionPool(String url, String user, String password, List<Connection> connectionPool) { |
28 | | - this.url = url; |
29 | | - this.user = user; |
30 | | - this.password = password; |
31 | | - this.connectionPool = connectionPool; |
32 | | - } |
33 | | - |
34 | | - @Override |
35 | | - public Connection getConnection() throws SQLException { |
36 | | - if (connectionPool.isEmpty()) { |
37 | | - if (usedConnections.size() < MAX_POOL_SIZE) { |
38 | | - connectionPool.add(createConnection(url, user, password)); |
39 | | - } else { |
40 | | - throw new RuntimeException("Maximum pool size reached, no available connections!"); |
41 | | - } |
42 | | - } |
43 | | - |
44 | | - Connection connection = connectionPool.remove(connectionPool.size() - 1); |
45 | | - usedConnections.add(connection); |
46 | | - return connection; |
47 | | - } |
48 | | - |
49 | | - @Override |
50 | | - public boolean releaseConnection(Connection connection) { |
51 | | - connectionPool.add(connection); |
52 | | - return usedConnections.remove(connection); |
53 | | - } |
54 | | - |
55 | | - private static Connection createConnection(String url, String user, String password) throws SQLException { |
56 | | - return DriverManager.getConnection(url, user, password); |
57 | | - } |
58 | | - |
59 | | - public int getSize() { |
60 | | - return connectionPool.size() + usedConnections.size(); |
61 | | - } |
62 | | - |
63 | | - @Override |
64 | | - public String getUrl() { |
65 | | - return url; |
66 | | - } |
67 | | - |
68 | | - @Override |
69 | | - public String getUser() { |
70 | | - return user; |
71 | | - } |
72 | | - |
73 | | - @Override |
74 | | - public String getPassword() { |
75 | | - return password; |
76 | | - } |
77 | | - |
78 | | - public void shutdown() throws SQLException { |
79 | | - usedConnections.forEach(this::releaseConnection); |
80 | | - for (Connection c : connectionPool) { |
81 | | - c.close(); |
82 | | - } |
83 | | - connectionPool.clear(); |
84 | | - } |
85 | | -} |
| 1 | +package com.baeldung.connectionpool; |
| 2 | + |
| 3 | +import java.sql.Connection; |
| 4 | +import java.sql.DriverManager; |
| 5 | +import java.sql.SQLException; |
| 6 | +import java.util.ArrayList; |
| 7 | +import java.util.List; |
| 8 | + |
| 9 | +public class BasicConnectionPool implements ConnectionPool { |
| 10 | + |
| 11 | + private final String url; |
| 12 | + private final String user; |
| 13 | + private final String password; |
| 14 | + private final List<Connection> connectionPool; |
| 15 | + private final List<Connection> usedConnections = new ArrayList<>(); |
| 16 | + private static final int INITIAL_POOL_SIZE = 10; |
| 17 | + private final int MAX_POOL_SIZE = 20; |
| 18 | + |
| 19 | + public static BasicConnectionPool create(String url, String user, String password) throws SQLException { |
| 20 | + List<Connection> pool = new ArrayList<>(INITIAL_POOL_SIZE); |
| 21 | + for (int i = 0; i < INITIAL_POOL_SIZE; i++) { |
| 22 | + pool.add(createConnection(url, user, password)); |
| 23 | + } |
| 24 | + return new BasicConnectionPool(url, user, password, pool); |
| 25 | + } |
| 26 | + |
| 27 | + private BasicConnectionPool(String url, String user, String password, List<Connection> connectionPool) { |
| 28 | + this.url = url; |
| 29 | + this.user = user; |
| 30 | + this.password = password; |
| 31 | + this.connectionPool = connectionPool; |
| 32 | + } |
| 33 | + |
| 34 | + @Override |
| 35 | + public Connection getConnection() throws SQLException { |
| 36 | + if (connectionPool.isEmpty()) { |
| 37 | + if (usedConnections.size() < MAX_POOL_SIZE) { |
| 38 | + connectionPool.add(createConnection(url, user, password)); |
| 39 | + } else { |
| 40 | + throw new RuntimeException("Maximum pool size reached, no available connections!"); |
| 41 | + } |
| 42 | + } |
| 43 | + |
| 44 | + Connection connection = connectionPool.remove(connectionPool.size() - 1); |
| 45 | + usedConnections.add(connection); |
| 46 | + return connection; |
| 47 | + } |
| 48 | + |
| 49 | + @Override |
| 50 | + public boolean releaseConnection(Connection connection) { |
| 51 | + connectionPool.add(connection); |
| 52 | + return usedConnections.remove(connection); |
| 53 | + } |
| 54 | + |
| 55 | + private static Connection createConnection(String url, String user, String password) throws SQLException { |
| 56 | + return DriverManager.getConnection(url, user, password); |
| 57 | + } |
| 58 | + |
| 59 | + @Override |
| 60 | + public int getSize() { |
| 61 | + return connectionPool.size() + usedConnections.size(); |
| 62 | + } |
| 63 | + |
| 64 | + @Override |
| 65 | + public List<Connection> getConnectionPool() { |
| 66 | + return connectionPool; |
| 67 | + } |
| 68 | + |
| 69 | + @Override |
| 70 | + public String getUrl() { |
| 71 | + return url; |
| 72 | + } |
| 73 | + |
| 74 | + @Override |
| 75 | + public String getUser() { |
| 76 | + return user; |
| 77 | + } |
| 78 | + |
| 79 | + @Override |
| 80 | + public String getPassword() { |
| 81 | + return password; |
| 82 | + } |
| 83 | + |
| 84 | + @Override |
| 85 | + public void shutdown() throws SQLException { |
| 86 | + usedConnections.forEach(this::releaseConnection); |
| 87 | + for (Connection c : connectionPool) { |
| 88 | + c.close(); |
| 89 | + } |
| 90 | + connectionPool.clear(); |
| 91 | + } |
| 92 | +} |
0 commit comments