What is the difference between javax.sql.DataSource and javax.sql.ConnectionPoolDataSource? I don't know which resource type to use when I am creating a connection pool in GlassFish for MySQL.
-
1[This](http://www.coderanch.com/t/297658/JDBC/databases/Difference-DataSource-ConnectionPoolDataSource) might help. Quoting: `The latter of the two does support connection pooling, whereas the first doesn't. This practically means that the ConnectionPoolDataSource can provide you with a PooledConnection` – mwerschy May 25 '13 at 18:59
-
But why does the [MySQL manual](http://dev.mysql.com/doc/refman/5.7/en/connector-j-usagenotes-glassfish-config.html) use "javax.sql.DataSource" to create a connection pool instead of "javax.sql.ConnectionPoolDataSource"? I've seen articles using both **DataSource** and **ConnectionPoolDataSource** to create a connection pool. Why do they use DataSource when it doesn't support connection pooling? – user1911467 May 25 '13 at 19:10
-
see: http://stackoverflow.com/questions/10047207/making-datasource-in-glassfish – DannyMo May 25 '13 at 19:36
-
@mwerschy : That assumption is not true. Application servers use their own pooling with `javax.sql.DataSource` instead of using native pooling implemented by a JDBC driver such as `PooledConnection` as done by `javax.sql.ConnectionPoolDataSource`. – Tiny Jan 12 '16 at 23:33
-
Near duplicate of: [DataSource or ConnectionPoolDataSource for Application Server JDBC resources](http://stackoverflow.com/q/6506859/642706) – Basil Bourque May 21 '17 at 07:29
2 Answers
In the context of GlassFish and MySQL, you would typically use javax.sql.ConnectionPoolDataSource when setting up a connection pool because it's specifically designed for managing connection pools efficiently. GlassFish would handle the configuration and management of the connection pool using the ConnectionPoolDataSource implementation.
the key difference is that DataSource is a higher-level interface for obtaining database connections in a general application, while ConnectionPoolDataSource is a lower-level interface designed for use in connection pool management, especially in application servers or when you want fine-grained control over connection pooling behavior.

- 2,687
- 1
- 16
- 22
ConnectionPoolDataSource
is just a DataSource
(as it inherit's CommonDataSource
which is also inherited by DataSource
) with capability of Connection Pooling -
you asked : I don't know which resource type to use
It depend's on your application, many database drivers take a long time to create a new connection with database, If your application is going to create too many connection's (very frequently). use connection pooling.

- 44,509
- 17
- 89
- 111
-
3Your assertion is incorrect: A [`ConnectionPoolDataSource`](http://docs.oracle.com/javase/8/docs/api/javax/sql/ConnectionPoolDataSource.html) is *not* a [`DataSource`](http://docs.oracle.com/javase/8/docs/api/javax/sql/DataSource.html). You even say yourself (correctly) that both inherit from [`CommonDataSource`](http://docs.oracle.com/javase/8/docs/api/javax/sql/CommonDataSource.html) – which means they do *not* share an [‘is-a’ relationship](https://en.wikipedia.org/wiki/Is-a#Java). This Answer only complicates an already confusing situation raised by this Question, without adding any value. – Basil Bourque May 21 '17 at 07:25