Questions And Answers

More Tutorials

postgreSQL Connecting with javax sql DataSource using a connection pool

It is common to use javax.sql.DataSource with JNDI in application server containers, where you register a data source under a name and look it up whenever you need a connection.

This is code that demonstrates how data sources work:

 * Create a data source with connection pool for PostgreSQL connections
 * @param url the JDBC URL to connect to. Must start with "jdbc:postgresql:"
 * @param user the username for the connection
 * @param password the password for the connection
 * @return a data source with the correct properties set
private static javax.sql.DataSource createDataSource(String url, String user, String password)
 /* use a data source with connection pooling */
 org.postgresql.ds.PGPoolingDataSource ds = new org.postgresql.ds.PGPoolingDataSource();
 /* the connection pool will have 10 to 20 connections */
 /* use SSL connections without checking server certificate */
 return ds;

Once you have created a data source by calling this function, you would use it like this:

/* get a connection from the connection pool */
java.sql.Connection conn = ds.getConnection();
/* do some work */
/* hand the connection back to the pool - it will not be closed */


In this page (written and validated by ) you learned about postgreSQL Connecting with javax sql DataSource using a connection pool . What's Next? If you are interested in completing postgreSQL tutorial, your next topic will be learning about: postgreSQL Data Types.

Incorrect info or code snippet? We take very seriously the accuracy of the information provided on our website. We also make sure to test all snippets and examples provided for each section. If you find any incorrect information, please send us an email about the issue:

Share On:

Mockstacks was launched to help beginners learn programming languages; the site is optimized with no Ads as, Ads might slow down the performance. We also don't track any personal information; we also don't collect any kind of data unless the user provided us a corrected information. Almost all examples have been tested. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. By using, you agree to have read and accepted our terms of use, cookies and privacy policy.