17

I'm trying to use a Connection Pool, but I don't understand it right. Who implements it? The software, the driver or the database?

How I do to have my program running using a Connection Pool? I'm using the native PostgreSQL driver.

I need an code example. I'm doing an webcrawler and it has many connections to the database.

1
  • What did you end up using? Commented May 23, 2016 at 21:18

3 Answers 3

20

There are several possibilities:

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

Pooling has many advantages:

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

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

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

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, man. Now I know a bit more abouts Connection Pooling. I'm trying the DBCP implementation. I take a look at PosgreSQL docs, but it advises that the implementation is not the best in the world. Currently the implementation with DBCP seems promising.
DBCP 1.4 is a wise choice, glad I could help.
1

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

Comments

1

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

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

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

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.