尝试使用在 Tomcat 下运行的 Spring 创建数据源时,我在数据库驱动程序类上遇到 ClassNotFoundException。但是,我能够直接从同一个数据访问组件(通过Class.forName
和getClass().getClassLoader().loadClass()
)加载驱动程序类。驱动程序 jar 仅安装在一处 ( $CATALINA_HOME/lib
)。
Spring 配置文件中的数据源定义:
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="com.microsoft.sqlserver.jdbc.SQLServerDriver"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</bean>
Spring 配置文件中的 DAO 定义:
<bean id="countryDao" class="com.mycompany.pmo.dao.CountryDao">
<constructor-arg ref="dataSource"/>
</bean>
DAO 本身:
public class CountryDao {
private NamedParameterJdbcTemplate jdbcTemplate;
public CountryDao(DataSource dataSource) {
jdbcTemplate = new NamedParameterJdbcTemplate(dataSource);
}
public List<Country> getCountries() {
//I can manually load the driver class here
String sql = "select * from ref.country";
Map<String, Object> namedParameters = new HashMap<String, Object>();
//next line is line 34, where the stack trace starts
return jdbcTemplate.query(sql, namedParameters,new CountryMapper());
}
}
堆栈跟踪:
Feb 20, 2013 2:18:35 PM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet [appServlet] in context with path [/PMO] threw exception [Request processing failed; nested exception is org.springframework.jdbc.CannotGetJdbcConnectionException: Could not get JDBC Connection; nested exception is org.apache.commons.dbcp.SQLNestedException: Cannot load JDBC driver class '"com.microsoft.sqlserver.jdbc.SQLServerDriver"'] with root cause
java.lang.ClassNotFoundException: "com.microsoft.sqlserver.jdbc.SQLServerDriver"
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1714)
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1559)
at org.apache.commons.dbcp.BasicDataSource.createConnectionFactory(BasicDataSource.java:1420)
at org.apache.commons.dbcp.BasicDataSource.createDataSource(BasicDataSource.java:1371)
at org.apache.commons.dbcp.BasicDataSource.getConnection(BasicDataSource.java:1044)
at org.springframework.jdbc.datasource.DataSourceUtils.doGetConnection(DataSourceUtils.java:111)
at org.springframework.jdbc.datasource.DataSourceUtils.getConnection(DataSourceUtils.java:77)
at org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:573)
at org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:637)
at org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:662)
at org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:702)
at org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate.query(NamedParameterJdbcTemplate.java:166)
at org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate.query(NamedParameterJdbcTemplate.java:172)
at com.mycompany.pmo.dao.CountryDao.getCountries(CountryDao.java:34)