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 ?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With