What is c3p0 in more deeper way?
Asked Answered
D

3

7

I've using Hibernate ORM for a year now and c3p0 about a couple of months. I've read on web, c3p0 is a JDBC connection and statement pooling library, that enhances JDBC drivers.

But, I wants to know, where exactly does it sit?

For reference, here is the schematic of JDBC-ODBC bridge.

enter image description here

Now, c3p0 replaces the JDBC API?

or it works along with JDBC API using some enhanced classes rather than traditional JDBC API classes?

If neither, than what it does & where it belongs in this schematic or even out of this schematic?

Please elaborate more about C3P0, that you know!

Dizzy answered 17/4, 2014 at 18:17 Comment(2)
Documentation not clear enough? mchange.com/projects/c3p0Tabithatablature
Yes, I've read the intro part of it. And, I've mentioned in the question also. But, I am not able to understand physical presence of c3p0 in the jdbc architecture.Dizzy
T
14

The picture you have posted pertains to the Type-1 JDBC drivers, which is mostly legacy today. More generally, the picture explains the lower-level details which are not relevant to understanding C3P0.

C3P0 is simply an implementation of the DataSource API, which conceptually sits just above the raw JDBC DriverManager. The main function of a DataSource is acquiring a standard JDBC Connection from it and releasing it in the end.

Tab answered 17/4, 2014 at 18:28 Comment(2)
"Sits just above the raw JDBC DriverManager". So, it replaces JDBC API or just enhances some part of JDBC API like getting connection for a database? And, JDBC API still performs other operations?Dizzy
Yes, the connection is retrieved from the DataSource, but from then on it's all standard JDBC.Tab
T
2

It does not replace JDBC. It rather manages connections. Creating a connection to the database is a time expensive operation. From the application's point of view, individual connections are mostly interchangeble when in a "neutral" state (no open cursors, transactions, custom variables, locale, whatever the database may set for a connection).
Therefore it's better to open several connections and re-use them.

I guess you know this. Now, C3P0 uses the JDBC API to get the connection when requested by the application. The application typically has some other API to ask the pool for a connection. There are certain requirements for the connection, defined by the API. C3P0, as mentioned in the other answer, implements DataSource API, but I believe it has adapters for other commonly used interfaces, too.

The returned JDBC connection is typically a wrapper object which delegates the JDBC API calls to the underlying database's JDBC driver.

An stacktrace is worth thousand words:

! at org.postgresql.core.v3.QueryExecutorImpl.readStartupMessages(QueryExecutorImpl.java:2566)
! at org.postgresql.core.v3.QueryExecutorImpl.<init>(QueryExecutorImpl.java:131)
! at org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:210)
! at org.postgresql.core.ConnectionFactory.openConnection(ConnectionFactory.java:49)
! at org.postgresql.jdbc.PgConnection.<init>(PgConnection.java:195)
! at org.postgresql.Driver.makeConnection(Driver.java:452)
! at org.postgresql.Driver.connect(Driver.java:254)
! at com.mchange.v2.c3p0.DriverManagerDataSource.getConnection(DriverManagerDataSource.java:175)
! at com.mchange.v2.c3p0.WrapperConnectionPoolDataSource.getPooledConnection(WrapperConnectionPoolDataSource.java:220)
! at com.mchange.v2.c3p0.WrapperConnectionPoolDataSource.getPooledConnection(WrapperConnectionPoolDataSource.java:206)
! at com.mchange.v2.c3p0.impl.C3P0PooledConnectionPool$1PooledConnectionResourcePoolManager.acquireResource(C3P0PooledConnectionPool.java:203)
! at com.mchange.v2.resourcepool.BasicResourcePool.doAcquire(BasicResourcePool.java:1138)
! at com.mchange.v2.resourcepool.BasicResourcePool.doAcquireAndDecrementPendingAcquiresWithinLockOnSuccess(BasicResourcePool.java:1125)
! at com.mchange.v2.resourcepool.BasicResourcePool.access$700(BasicResourcePool.java:44)
! at com.mchange.v2.resourcepool.BasicResourcePool$ScatteredAcquireTask.run(BasicResourcePool.java:1870)
! at com.mchange.v2.async.ThreadPoolAsynchronousRunner$PoolThread.run(ThreadPoolAsynchronousRunner.java:696)
Thrive answered 3/7, 2018 at 23:59 Comment(0)
D
1

Actually c3p0 is a JAR file which we uses in Hibernate.

When we run our Hibernate application, we can see in the console that there is a line "Connection Pool Size 20". That is the number of connection objects available. If we uses only 5 connections, then 15 connection objects will be a waste, and in industry "Connection Objects" are very costly resources. That's why, to define the number of connection objects that may be needed in our application, we use the c3p0 JAR file.

Dambrosio answered 29/4, 2015 at 18:21 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.