org.apache.commons.dbcp.SQLNestedException: Cannot load JDBC driver class

org.springframework.transaction.CannotCreateTransactionException: Could not open JDBC Connection for transaction; nested exception is org.apache.commons.dbcp.SQLNestedException: Cannot load JDBC driver class ‘${jdbc.driverClassName}‘
at org.springframework.jdbc.datasource.DataSourceTransactionManager.doBegin(DataSourceTransactionManager.java:240)
at org.springframework.transaction.support.AbstractPlatformTransactionManager.getTransaction(AbstractPlatformTransactionManager.java:371)
at org.springframework.test.context.transaction.TransactionalTestExecutionListener$TransactionContext.startTransaction(TransactionalTestExecutionListener.java:513)
at org.springframework.test.context.transaction.TransactionalTestExecutionListener.startNewTransaction(TransactionalTestExecutionListener.java:271)
at org.springframework.test.context.transaction.TransactionalTestExecutionListener.beforeTestMethod(TransactionalTestExecutionListener.java:164)
at org.springframework.test.context.TestContextManager.beforeTestMethod(TestContextManager.java:358)
at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:73)
at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:83)
at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:72)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:231)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:47)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222)
at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:71)
at org.junit.runners.ParentRunner.run(ParentRunner.java:300)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:174)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
Caused by: org.apache.commons.dbcp.SQLNestedException: Cannot load JDBC driver class ‘${jdbc.driverClassName}‘
at org.apache.commons.dbcp.BasicDataSource.createConnectionFactory(BasicDataSource.java:1429)
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.DataSourceTransactionManager.doBegin(DataSourceTransactionManager.java:202)
... 25 more
Caused by: java.lang.ClassNotFoundException: ${jdbc.driverClassName}
at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
at org.apache.commons.dbcp.BasicDataSource.createConnectionFactory(BasicDataSource.java:1420)
... 28 more

在spring里使用org.mybatis.spring.mapper.MapperScannerConfigurer 进行自动扫描的时候,设置了sqlSessionFactory 的话,可能会导致PropertyPlaceholderConfigurer失效,也就是用${jdbc.username}这样之类的表达式,将无法获取到properties文件里的内容。 导致这一原因是因为,MapperScannerConigurer实际是在解析加载bean定义阶段的,这个时候要是设置sqlSessionFactory的话,会导致提前初始化一些类,这个时候,PropertyPlaceholderConfigurer还没来得及替换定义中的变量,导致把表达式当作字符串复制了。 但如果不设置sqlSessionFactory 属性的话,就必须要保证sessionFactory在spring中名称一定要是sqlSessionFactory ,否则就无法自动注入。又或者直接定义 MapperFactoryBean ,再或者放弃自动代理接口方式。

就是这个org.mybatis.spring.mapper.MapperScannerConfigurer中配置的bean下有属性sqlSessionFactory,如果配置了,就去掉这个属性,变成如下:

Xml代码  

    1. <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
    2. <property value="com.hyq.showcase.mapper,com.hyq.common.core.mapper"
    3. name="basePackage" />
    4. </bean>
    5. 以上的解决方案比较一般,在3.1.1版本中配置的时候提供了以下: 

      Xml代码  

      1. <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
      2. <property name="basePackage" value="com.xxxx.dal.mapper" />
      3. <property name="sqlSessionFactoryBeanName" value="ysSqlSessionFactory" />
      4. <!-- <property name="sqlSessionFactory" ref="ysSqlSessionFactory"></property> -->
      5. </bean>

      改用sqlSessionFactoryBeanName注入就没有问题(不要使用sqlSessionFactory属性注入,使用sqlSessionFactoryBeanName注入),因为这时不会立即初始化sqlSessionFactory,传入的只是名字,非bean,所以不会引发提前初始化问题。。 
      以上供参考!

    6. 引自:http://songjianyong.iteye.com/blog/1663170
时间: 2024-10-13 01:52:07

org.apache.commons.dbcp.SQLNestedException: Cannot load JDBC driver class的相关文章

org.apache.commons.dbcp.SQLNestedException: Cannot load JDBC driver class &#39;com.microsoft.sqlserver.jdbc.SQLServerDriver &#39;

问题摘要: 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 ' 原因:配置文件drive

spring+mybatis整合,org.apache.commons.dbcp.SQLNestedException: Cannot load JDBC driver class &#39;${jdbc.driverClassName}

在使用spring+mybatis时会出现Cannot load JDBC driver class ${jdbc.driverClassName}之类的出错. 原因是在spring里使用org.mybatis.spring.mapper.MapperScannerConfigurer 进行自动扫描的时候,设置了sqlSessionFactory 的话,可能会导致PropertyPlaceholderConfigurer失效,也就是用${jdbc.username}这样之类的表达式,将无法获取到

Could not get JDBC Connection; nested exception is org.apache.commons.dbcp.SQLNestedException:

七月 17, 2014 4:56:01 下午 org.apache.catalina.core.StandardWrapperValve invoke SEVERE: Servlet.service() for servlet [dispatcher] in context with path [] threw exception [Request processing failed; nested exception is org.mybatis.spring.MyBatisSystemExc

jdbc连接oracle时报错 Cause: org.springframework.jdbc.CannotGetJdbcConnectionException: Could not get JDBC Connection; nested exception is org.apache.commons.dbcp.SQLNestedException: Cannot create PoolableC

错误: Cause: org.springframework.jdbc.CannotGetJdbcConnectionException: Could not get JDBC Connection; nested exception is org.apache.commons.dbcp.SQLNestedException: Cannot create PoolableConnectionFactory (Communications link failure Last packet sent

Could not open JDBC Connection for transaction; nested exception is org.apache.commons.dbcp.SQLNestedException: Cannot create PoolableConnectionFactory (Could not create connection to database server.

报错信息:Could not open JDBC Connection for transaction; nested exception is org.apache.commons.dbcp.SQLNestedException: Cannot create PoolableConnectionFactory (Could not create connection to database server.) 问题来源mysql-connecter-java 版本过低 如果你mysql的版本是m

org.apache.commons.dbcp.SQLNestedException: Cannot create PoolableConnectionFactory (Access denied for user &#39;root&#39;@&#39;localhost&#39; (using password:

tationProcessor' to allow for resolving potential circular referencesDEBUG 2018-05-28 11:32:35,016 org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory: Finished creating instance of bean 'org.springframework.context.annotatio

Hibernate连接数据库问题org.apache.commons.dbcp.SQLNestedException: Cannot create PoolableConnectionFactory

一.使用hibernate无法连接数据库的问题: ERROR: HHH000231: Schema export unsuccessful (Access denied for user 'root'@'192.168.1.109' (using password: NO)) at org.apache.commons.dbcp.BasicDataSource.createPoolableConnectionFactory(BasicDataSource.java:1549) at org.ap

SQLNestedException: Cannot create JDBC driver of class &#39;&#39; for connect URL &#39;null&#39; 解决办法

当跑jndi项目时抛出:org.apache.tomcat.dbcp.dbcp.SQLNestedException: Cannot create JDBC driver of class '' for connect URL 'null'异常 解决办法: 1.在Tomcat 6.0\lib下加入oracle驱动包ojdbc14_g-10.2.0.1.0.jar 2.在Tomcat 6.0\conf下的context.xml文件中加入 [javascript] view plaincopy <R

【DAO】基于apache.commons.dbcp的DAO数据层的代码

  ************************************************************************   ****原文:blog.csdn.net/clark_xu 徐长亮的专栏   ************************************************************************ DAO的代码主要是基于oracle默认用户scott用户的 dept(部门列表)emp(员工信息列表)进行查询,增加等.其