The connection pool itself does not require connections that must be obtained by JNDI. You can also simply configure and use the connection pool independently of JNDI. Suppose you would like to use C3P0 , which is one of the best connection pools, then you can find "raw" JNDI - less configuration details in this tutorial .
Here is an excerpt from the textbook:
ComboPooledDataSource cpds = new ComboPooledDataSource(); cpds.setDriverClass( "org.postgresql.Driver" ); //loads the jdbc driver cpds.setJdbcUrl( "jdbc:postgresql://localhost/testdb" ); cpds.setUser("swaldman"); cpds.setPassword("test-password");
Create a data source once during application startup and save it somewhere in context. Then the connection can be obtained and used as follows:
Connection connection = null; // ... try { connection = cpds.getConnection(); // ... } finally { // ... if (connection != null) try { connection.close(); } catch (SQLException ignore) {} }
Yes, closing at the end is still required, otherwise the connection pool will not be able to connect the connection in the pool for reuse in the future, and it will end the connection.
source share