Reputation: 49087
The preferred way of obtaining a connection is through a DataSource from what I have read. There are different interfaces such as DataSource
and ConnectionPoolDataSource
. Let say we use PostgreSQL driver and want to use connection pooling on a Glassfish server.
In your application code you invoke getConnection()
on a property of type DataSource
. How is this possible? Haven't Glassfish created a datasource of type ConnectionPoolDataSource
(or more correctly an implementing class) and bound it to a JNDI name and when you get a datasource by using the JNDI name you get an object of ConnectionPoolDataSource
not DataSource
?? ConnectionPoolDataSource
does not have a getConnection()
method. I don't understand this server magic.
Could someone explain how all this fits together?
Upvotes: 2
Views: 793
Reputation: 109034
The DataSource
, Driver
or ConnectionPoolDataSource
that you can select in the Glassfish config is not exposed to your application directly, instead the Application Server has its own DataSource
that maintains a connection pool, this datasource uses the configured DataSource
, Driver
or ConnectionPoolDataSource
as the factory for the connections it will keep in its pool.
So when you configure Glassfish with a ConnectionPoolDataSource
, it uses the ConnectionPoolDataSource
to create the physical connections (PooledConnection
objects) for a connection pool. This connection pool is kept by the application server DataSource
implementation. Your application then access that connection pool using this DataSource
. The DataSource hands out logical Connection
objects from the connection pool.
The exact inner workings of a logical and physical connection are implementation dependent, but these logical connections are usually some kind of proxy or wrapper around the physical connection. When you obtain the logical connection, the physical connection is checked out from the connection pool. When you close the logical connection, the connection pool receives a signal that the physical connection is available again and returns it to the connection pool.
Upvotes: 4