|
| 1 | +--- |
| 2 | +title: Java samples to illustrate connection pooling |
| 3 | +description: This article lists java samples to illustrate connection pooling. |
| 4 | +author: ambhatna |
| 5 | +ms.author: ambhatna |
| 6 | +ms.service: mysql |
| 7 | +ms.topic: sample |
| 8 | +ms.date: 02/28/2018 |
| 9 | +--- |
| 10 | +# Java sample to illustrate connection pooling |
| 11 | + |
| 12 | +The below sample code illustrates connection pooling in java. |
| 13 | + |
| 14 | +```java |
| 15 | +import java.sql.Connection; |
| 16 | +import java.sql.DriverManager; |
| 17 | +import java.sql.ResultSet; |
| 18 | +import java.sql.SQLException; |
| 19 | +import java.sql.Statement; |
| 20 | +import java.util.HashSet; |
| 21 | +import java.util.Set; |
| 22 | +import java.util.Stack; |
| 23 | + |
| 24 | +public class MySQLConnectionPool { |
| 25 | + private String databaseUrl; |
| 26 | + private String userName; |
| 27 | + private String password; |
| 28 | + private int maxPoolSize = 10; |
| 29 | + private int connNum = 0; |
| 30 | + |
| 31 | + private static final String SQL_VERIFYCONN = "select 1"; |
| 32 | + |
| 33 | + Stack<Connection> freePool = new Stack<>(); |
| 34 | + Set<Connection> occupiedPool = new HashSet<>(); |
| 35 | + |
| 36 | + /** |
| 37 | + * Constructor |
| 38 | + * |
| 39 | + * @param databaseUrl |
| 40 | + * The connection url |
| 41 | + * @param userName |
| 42 | + * user name |
| 43 | + * @param password |
| 44 | + * password |
| 45 | + * @param maxSize |
| 46 | + * max size of the connection pool |
| 47 | + */ |
| 48 | + public MySQLConnectionPool(String databaseUrl, String userName, |
| 49 | + String password, int maxSize) { |
| 50 | + this.databaseUrl = databaseUrl; |
| 51 | + this.userName = userName; |
| 52 | + this.password = password; |
| 53 | + this.maxPoolSize = maxSize; |
| 54 | + } |
| 55 | + |
| 56 | + /** |
| 57 | + * Get an available connection |
| 58 | + * |
| 59 | + * @return An available connection |
| 60 | + * @throws SQLException |
| 61 | + * Fail to get an available connection |
| 62 | + */ |
| 63 | + public synchronized Connection getConnection() throws SQLException { |
| 64 | + Connection conn = null; |
| 65 | + |
| 66 | + if (isFull()) { |
| 67 | + throw new SQLException("The connection pool is full."); |
| 68 | + } |
| 69 | + |
| 70 | + conn = getConnectionFromPool(); |
| 71 | + |
| 72 | + // If there is no free connection, create a new one. |
| 73 | + if (conn == null) { |
| 74 | + conn = createNewConnectionForPool(); |
| 75 | + } |
| 76 | + |
| 77 | + // For Azure Database for MySQL, if there is no action on one connection for some |
| 78 | + // time, the connection is lost. By this, make sure the connection is |
| 79 | + // active. Otherwise reconnect it. |
| 80 | + makeAvailable(conn); |
| 81 | + return conn; |
| 82 | + } |
| 83 | + |
| 84 | + /** |
| 85 | + * Return a connection to the pool |
| 86 | + * |
| 87 | + * @param conn |
| 88 | + * The connection |
| 89 | + * @throws SQLException |
| 90 | + * When the connection is returned already or it isn't gotten |
| 91 | + * from the pool. |
| 92 | + */ |
| 93 | + public synchronized void returnConnection(Connection conn) |
| 94 | + throws SQLException { |
| 95 | + if (conn == null) { |
| 96 | + throw new NullPointerException(); |
| 97 | + } |
| 98 | + if (!occupiedPool.remove(conn)) { |
| 99 | + throw new SQLException( |
| 100 | + "The connection is returned already or it isn't for this pool"); |
| 101 | + } |
| 102 | + freePool.push(conn); |
| 103 | + } |
| 104 | + |
| 105 | + /** |
| 106 | + * Verify if the connection is full. |
| 107 | + * |
| 108 | + * @return if the connection is full |
| 109 | + */ |
| 110 | + private synchronized boolean isFull() { |
| 111 | + return ((freePool.size() == 0) && (connNum >= maxPoolSize)); |
| 112 | + } |
| 113 | + |
| 114 | + /** |
| 115 | + * Create a connection for the pool |
| 116 | + * |
| 117 | + * @return the new created connection |
| 118 | + * @throws SQLException |
| 119 | + * When fail to create a new connection. |
| 120 | + */ |
| 121 | + private Connection createNewConnectionForPool() throws SQLException { |
| 122 | + Connection conn = createNewConnection(); |
| 123 | + connNum++; |
| 124 | + occupiedPool.add(conn); |
| 125 | + return conn; |
| 126 | + } |
| 127 | + |
| 128 | + /** |
| 129 | + * Crate a new connection |
| 130 | + * |
| 131 | + * @return the new created connection |
| 132 | + * @throws SQLException |
| 133 | + * When fail to create a new connection. |
| 134 | + */ |
| 135 | + private Connection createNewConnection() throws SQLException { |
| 136 | + Connection conn = null; |
| 137 | + try { |
| 138 | + Class.forName("com.mysql.jdbc.Driver"); |
| 139 | + conn = DriverManager.getConnection(databaseUrl, userName, password); |
| 140 | + } catch (ClassNotFoundException cnfe) { |
| 141 | + throw new SQLException(cnfe); |
| 142 | + } |
| 143 | + return conn; |
| 144 | + } |
| 145 | + |
| 146 | + /** |
| 147 | + * Get a connection from the pool. If there is no free connection, return |
| 148 | + * null |
| 149 | + * |
| 150 | + * @return the connection. |
| 151 | + */ |
| 152 | + private Connection getConnectionFromPool() { |
| 153 | + Connection conn = null; |
| 154 | + if (freePool.size() > 0) { |
| 155 | + conn = freePool.pop(); |
| 156 | + occupiedPool.add(conn); |
| 157 | + } |
| 158 | + return conn; |
| 159 | + } |
| 160 | + |
| 161 | + /** |
| 162 | + * Make sure the connection is available now. Otherwise, reconnect it. |
| 163 | + * |
| 164 | + * @param conn |
| 165 | + * The connection for verification. |
| 166 | + * @return the available connection. |
| 167 | + * @throws SQLException |
| 168 | + * Fail to get an available connection |
| 169 | + */ |
| 170 | + private Connection makeAvailable(Connection conn) throws SQLException { |
| 171 | + if (isConnectionAvailable(conn)) { |
| 172 | + return conn; |
| 173 | + } |
| 174 | + |
| 175 | + // If the connection is't available, reconnect it. |
| 176 | + occupiedPool.remove(conn); |
| 177 | + connNum--; |
| 178 | + conn.close(); |
| 179 | + |
| 180 | + conn = createNewConnection(); |
| 181 | + occupiedPool.add(conn); |
| 182 | + connNum++; |
| 183 | + return conn; |
| 184 | + } |
| 185 | + |
| 186 | + /** |
| 187 | + * By running a sql to verify if the connection is available |
| 188 | + * |
| 189 | + * @param conn |
| 190 | + * The connection for verification |
| 191 | + * @return if the connection is available for now. |
| 192 | + */ |
| 193 | + private boolean isConnectionAvailable(Connection conn) { |
| 194 | + try (Statement st = conn.createStatement()) { |
| 195 | + st.executeQuery(SQL_VERIFYCONN); |
| 196 | + return true; |
| 197 | + } catch (SQLException e) { |
| 198 | + return false; |
| 199 | + } |
| 200 | + } |
| 201 | + |
| 202 | + // Just an Example |
| 203 | + public static void main(String[] args) throws SQLException { |
| 204 | + Connection conn = null; |
| 205 | + MySQLConnectionPool pool = new MySQLConnectionPool( |
| 206 | + "jdbc:mysql://mysqlaasdevintic-sha.cloudapp.net:3306/<Your DB name>", |
| 207 | + "<Your user>", "<Your Password>", 2); |
| 208 | + try { |
| 209 | + conn = pool.getConnection(); |
| 210 | + try (Statement statement = conn.createStatement()) |
| 211 | + { |
| 212 | + ResultSet res = statement.executeQuery("show tables"); |
| 213 | + System.out.println("There are below tables:"); |
| 214 | + while (res.next()) { |
| 215 | + String tblName = res.getString(1); |
| 216 | + System.out.println(tblName); |
| 217 | + } |
| 218 | + } |
| 219 | + } |
| 220 | + finally { |
| 221 | + if (conn != null) { |
| 222 | + pool.returnConnection(conn); |
| 223 | + } |
| 224 | + } |
| 225 | + } |
| 226 | + |
| 227 | +} |
| 228 | + |
| 229 | +``` |
0 commit comments