Ascendant
Ascendant

Reputation: 827

What does Tomcat do under the hood to give you an implementation of DataSource

From JNDI Resources HOW-TO

<Context ...>
  ...
  <Resource name="jdbc/EmployeeDB"
            auth="Container"
            type="javax.sql.DataSource"
            username="dbusername"
            password="dbpassword"
            driverClassName="org.hsql.jdbcDriver"
            url="jdbc:HypersonicSQL:database"
            maxActive="8"
            maxIdle="4"/>
  ...
</Context>

The type is javax.sql.DataSource and this is an interface.

The code below retrieves an instance of DataSource and get a connection out of it.

Context initCtx = new InitialContext();
Context envCtx = (Context) initCtx.lookup("java:comp/env");
DataSource ds = (DataSource)
  envCtx.lookup("jdbc/EmployeeDB");

Connection conn = ds.getConnection();
... use this connection to access the database ...
conn.close();

From this Stackoverflow answer, it says the actual implementation of DataSource is up to the database vendor.

So in this example, does Tomcat use driverClassName="org.hsql.jdbcDriver" to return an implementation of DataSource ?

Upvotes: 3

Views: 252

Answers (1)

JB Nizet
JB Nizet

Reputation: 691765

Tomcat has a connection pool implementation. This connection pool has an implementation of the interface DataSource. And this implementation uses the specified driver class (org.hsql.jdbcDriver) in order to open connections to the database.

Upvotes: 4

Related Questions