giozh
giozh

Reputation: 10068

ConnectionManager: pool of connection

i have a jdbc client that communicate with a postgresql database by contact a RMI server. In this rmi server i have a simple ConnectionManager code:

public class ConnectionManager {
    private ConnectionManager(){};

    private static boolean driverLoad = false;
    private static final String pgDriver="org.postgresql.Driver";
    private static final String pgUrl = dbc:postgresql://localhost:5434/franchising_db";
    private static final String user = "postgres";
    private static final String pass = "password";

    public static Connection getConnection() throws ClassNotFoundException, SQLException {
        if(!driverLoad) {

            Class.forName(pgDriver);
            driverLoad = true;
        }
        return DriverManager.getConnection(pgUrl, user, pass);
    }
}

Now postgresql has a limit of 30 contemporary connection, but this kind of ConnectionManager does not avoid this use case. I would like to create a defined number of connection (for example 5) and when a client call getConnection method, if a connection is available, return it, then if all connections are busy, client wait for the first free connection. How should i modify that code for do that?

Upvotes: 0

Views: 515

Answers (1)

Shamim Ahmmed
Shamim Ahmmed

Reputation: 8383

You might look into the jdbc connection pooling where you can specify the number of open connections, idle time, wait time etc. The most popular libraries are C3P0 and DBCP

Upvotes: 1

Related Questions