Tomcat 在幕后做了什么来为您提供 DataSource 的实现

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

来自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>

类型是javax.sql.DataSource,这是一个接口。

下面的代码检索 DataSource 的实例并从中获取连接。

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();

来自 this Whosebug answer,它说 DataSource 的实际实现取决于数据库供应商。

所以在这个例子中,Tomcat 是否使用 driverClassName="org.hsql.jdbcDriver" 到 return DataSource 的实现?

Tomcat 有一个 connection pool 实现。此连接池具有接口 DataSource 的实现。并且此实现使用指定的驱动程序 class (org.hsql.jdbcDriver) 以打开与数据库的连接。