Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why we need a connection pooling for JDBC? [closed]

Tags:

  • What are the benefits of using a JDBC connection pooling tool like DBCP or c3p0 ?

  • in case of a small CRUD application with one user, can we just create one connection session as a singleton ?

PS: I'm building a small javafx application back-ended with tiny h2 database (5 tables).

like image 335
Salah Eddine Taouririt Avatar asked Apr 18 '13 21:04

Salah Eddine Taouririt


People also ask

Do we need to close connection in connection pool?

Yes, certainly you need to close the pooled connection as well. It's actually a wrapper around the actual connection. It wil under the covers release the actual connection back to the pool.

Why do we need connection pool?

Using connection pools helps to both alleviate connection management overhead and decrease development tasks for data access. Each time an application attempts to access a backend store (such as a database), it requires resources to create, maintain, and release a connection to that datastore.

Why should JDBC connection be closed?

At the end of your JDBC program, it is required explicitly to close all the connections to the database to end each database session. However, if you forget, Java's garbage collector will close the connection when it cleans up stale objects.

What will happens if JDBC connection is not closed?

If you don't close it, it leaks, and ties up server resources. @EJP The connection itself might be thread-safe (required by JDBC), but the applications use of the connection is probably not threadsafe. Think of things like different transaction isolation, boundaries (commit/rollback/autocommit) etc.


1 Answers

From Jon Skeet's answer to What is the benefit of Connection and Statement Pooling?:

Creating a network connection to a database server is (relatively) expensive. Likewise asking the server to prepare a SQL statement is (relatively) expensive.

Using a connection/statement pool, you can reuse existing connections/prepared statements, avoiding the cost of initiating a connection, parsing SQL etc.

And the following, from Kent Boogaart's answer:

I am not familiar with c3p0, but the benefits of pooling connections and statements include:

  1. Performance. Connecting to the database is expensive and slow. Pooled connections can be left physically connected to the database, and shared amongst the various components that need database access. That way the connection cost is paid for once and amortized across all the consuming components.

  2. Diagnostics. If you have one sub-system responsible for connecting to the database, it becomes easier to diagnose and analyze database connection usage.

  3. Maintainability. Again, if you have one sub-system responsible for handing out database connections, your code will be easier to maintain than if each component connected to the database itself.

like image 52
0x6C38 Avatar answered Oct 04 '22 21:10

0x6C38