[ACCEPTED]-How create an PostgreSQL connection pool using Java?-connection-pooling

Accepted answer
Score: 18

There are several possibilities:

No matter which option 17 you choose, in principle it always works 16 the same way: the client maintains a pool 15 of network connections to the database. Every 14 time you request new connection using DataSource, connection 13 pool will peek free connection and give 12 to you. When you think you are closing the 11 connection, it will actually be released 10 and put back into the pool. Other thread 9 may now use the same, already established 8 connection.

Pooling has many advantages:

  • there 7 is no overhead of TCP/IP connection, authorization, etc. - it 6 is only done once.

  • pool will take care of 5 broken connections, it may also test connection 4 before giving it to you

  • finally, the number 3 of active database connections is more stable, connection 2 pool should refuse returning connection 1 if you have already opened too much

Score: 1

The pooling itself is done by code that 16 sits between the application code and the 15 database driver.

Who puts that code there? Could 14 be anyone. It could be you - there are libraries 13 like DBCP that your code could use to put 12 a pool on top of the database. It could 11 be a J2EE container, like Tomcat or JBoss. It 10 could even be the database - as Tomasz points 9 out, PostgreSQL comes with pooling code.

It 8 sounds like you aren't using a J2EE container, in 7 which case it's down to you or the database. Personally, i'd 6 prefer a dedicated pooling implementation, like 5 DBCP, over one supplied by the database. The 4 database's programmers care most about the 3 database; the pool's programmers care most 2 about the pool.

So, get DBCP (IMHO, it's 1 better than the alternatives), and use that.

Score: 0

I think you are looking for connection pooling 6 done at client side that makes connection 5 to the database. The basic idea is that 4 the establishing a new connection is costly 3 as it involves setting up connection, setting 2 up DB side objects, authentication etc. and 1 hence reuse the connection objects.

More Related questions