Question

Considering a simple thread pool. The main purpose of a thread pool is reusing(recycling) a thread in order to limit the maximum number of threads and preventing of remove and create threads, am I right?
Recently I have read(wikipedia connection pool) that in Java EE, database connection are pooled by server, but I cannot figure out why?
Questions:

  • What is the main purpose of pooling sql connection in java?
  • Is it okay to pool 128 connections and keep all of them open?
  • What is the difference if 128 concurrent select commands get run by one connection or 128 connection?
  • What is the correct way to connect to the database through a connection pool?
  • How do both server and database handle the connections?
  • How does server reuse a closed connection?
  • Is pool still necessary when whole server components use a shared connection?

Thanks in advance.

Was it helpful?

Solution

Database connection pooling solve is very similar to the thread pooling you have mentioned here. Here are the main reasons.

  • Main purpose of the db connection pool is to create some db connections and keep them in the pool and reuse them whenever your application code need to execute queries to db. Because creation of connections is a very costly operation, Connection pooling allows us to save that effort.
  • It is perfectly fine to create 128 connections and keep them in the pool.
  • No 128 concurrent selects on one connection may not work properly. With connection pooling you get the connection from the pool and the use it to fire all the queries that are required for the operation and then you return the connection back to the pool. So you would need 128 connections to run the concurrent selects.
  • Generally you let the container handle the connection pooling. You can configure connection pooling in web.xml or in the container specify configuration file and associate a JNDI name with data source that is connected to the pool. Then you lookup that DataSource and get the connections from there. Here is the example code for getting connections.
public static Connection getConnection() {
        Connection con = null;
        try {
            Context initContext = new InitialContext();
            Context envContext = (Context) initContext.lookup("java:/comp/env");
            DataSource dataSource = (DataSource) envContext.lookup("jdbc/db");
            con = dataSource.getConnection();
        } catch (NamingException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return con;
    }
  • Database will assume that the connection are still alive and its the responsibility of the application server to keep the connections in the pool alive (of course by the configuration by user) and avoid the connection timeouts.

  • When your application code requests the connection to be closed by calling connection.close(), This connection is not closed and is actually returned to the pool so that further requests for connections from the pool will use this connection.

  • For all web applications its is strongly advised that you create the connection pool else you will run into problems related to performance and too many connections opened.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top