Mirwais Faizi
Mirwais Faizi

Reputation: 43

The HikariPC connection pooling not share connection pool between clients?

I am using HikariPC API connection pooling with JDBC in my desktop application. The connection pooling is establish perfectly with maximum pooling size 10 and 10 session is created in oracle database. The problem is that when every individual client run the application 10 session created in database and it's not use the shared connection pooling which is already created. instead it's create for every new connected client 10 session in database. Here is my code:

 public class ConnectionPool {

private static Properties properties = null;
private static HikariDataSource dataSource;

static {
    try {
        dataSource = new HikariDataSource();
        dataSource.setDriverClassName("oracle.jdbc.OracleDriver");

        dataSource.setJdbcUrl("jdbc:oracle:thin:@//128.1.10.150:1521/orcl");
        dataSource.setUsername("scott");
        dataSource.setPassword("faizi");

        dataSource.setMinimumIdle(100);
        dataSource.setMaximumPoolSize(10);//The maximum number of connections, idle or busy, that can be present in the pool.
        dataSource.setAutoCommit(false);
        dataSource.setLoginTimeout(3);

    } catch (SQLException ex) {
        Logger.getLogger(ConnectionPool.class.getName()).log(Level.SEVERE, null, ex);
    }
}

public static DataSource getDataSource() {
    return dataSource;
}

}

And when I call the connection class :

public void addCountryToObservableList() {
    String sql = "select * from country";
    try (Connection conn =ConnectionPool.getDataSource().getConnecton();
        PreparedStatement pst = conn.prepareStatement(sql); ResultSet rs = pst.executeQuery()) {
        while (rs.next()) {
            countries.add(new Country(rs.getInt("code"), rs.getString("country")));
        }
    } catch (SQLException ex) {
        ShowErroMessage(ex, "addCountryToObservableList() methods");
    }
 }

I thing connection pooling work like share printer or DHCP which share between clients. I put connection pooling diagram may be help.enter image description here

Upvotes: 0

Views: 464

Answers (1)

Antoniossss
Antoniossss

Reputation: 32535

Connection pool is a singleton (usually, but does not have to be) in the scope of the application. If you open your application N times, you will end up with N connection pools. There is not magic sharing of internal resources between application instances. If you need such sharing, you have to implement it (or find library that does that for you)

The reason for collection pooling is that connection establishment is expensive, so instead of creating/closing new connecion every time you need one, connectin pool "lends" you already alive connection making whole processing faster. But that happens only within the application and worldwide

Upvotes: 1

Related Questions