patternjavaMinor
Simple singleton database connection pool
Viewed 0 times
simpledatabasesingletonpoolconnection
Problem
I'm studying design patterns, and to demonstrate a singleton, I've implemented a primitive database connection pool.
ConnectionPool.java
ConnectionPoolDemo.java
```
package com.levent.connpool;
import java.sql.Connection;
import java.sql.SQLException;
public class ConnectionPoolDemo {
public static void main(String[] args) {
ConnectionPool pool = ConnectionPool.getInstance();
Connection[] connections = new Connection[17];
for(int i = 0; i < connections.length; i++)
connections[i] = pool.getConnection();
int preIndex = connections[0].getClass().
ConnectionPool.java
package com.levent.connpool;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class ConnectionPool {
private final static int MAX_CONNECTIONS = 8;
private static ConnectionPool instance = null; // lazy loading
private static Connection[] connections = new Connection[MAX_CONNECTIONS];
private static String dbUrl = "jdbc:derby://localhost:1527/memory:levoDB/singletonDemo;create=true";
private static int counter;
private ConnectionPool() { }
public static ConnectionPool getInstance() {
if(instance == null) {
synchronized(ConnectionPool.class) {
if(instance == null) {
instance = new ConnectionPool();
initializeConnections();
counter = 0;
}
}
}
return instance;
}
private static void initializeConnections() {
for(int i = 0; i < MAX_CONNECTIONS; i++) {
try {
connections[i] = DriverManager.getConnection(dbUrl);
} catch (SQLException e) {
e.printStackTrace();
}
}
}
public static Connection getConnection() {
counter++;
if(counter == Integer.MAX_VALUE)
counter = 0;
return connections[counter%MAX_CONNECTIONS];
}
}ConnectionPoolDemo.java
```
package com.levent.connpool;
import java.sql.Connection;
import java.sql.SQLException;
public class ConnectionPoolDemo {
public static void main(String[] args) {
ConnectionPool pool = ConnectionPool.getInstance();
Connection[] connections = new Connection[17];
for(int i = 0; i < connections.length; i++)
connections[i] = pool.getConnection();
int preIndex = connections[0].getClass().
Solution
- Your connection pool is not threadsafe / concurrently usable, if you are sure you do not need it to be ignore my points 1. to 3.
- Your pool does not offer connection locking (mutual exclusive usage of one Connection object) which is usually implemented by making the connection inaccessible after getConnection() and making it eligible for takeout after calling its close() method. You close the raw connections manually, leaving the pool in a broken state.
This is problematic: https://stackoverflow.com/questions/9428573/
- Your counter int might return the same int for two concurrent calls at the same time. Use AtomicInteger / AtomicLong instead.
- Your pool does not implement connection testing: Your connections could expire at any time. You need to replace broken Connections with new Connections or your program might receive dead Connection objects!
- You might want to implement Closeable to close all connections at once when your application shuts down and clear the instance.
- You might want to throw if initializeConnections fails, because what are you supposed to do with a broken object?
- Hardcoding URLs that might change is not considered good practice
Context
StackExchange Code Review Q#126621, answer score: 4
Revisions (0)
No revisions yet.