在 Spring Tomcat 应用程序中无法为连接 URL 'null 创建类 '' 的 JDBC 驱动程序

问题描述 投票:0回答:2

通过 jndi 获取数据源时出现以下异常:

原因:org.springframework.jdbc.CannotGetJdbcConnectionException:
     无法获取 JDBC 连接;嵌套异常是
     org.apache.tomcat.dbcp.dbcp.SQLNestedException:无法为连接 URL 'null 创建类 '' 的 JDBC 驱动程序

Tomcat 的 server.xml :

     <Resource name="jdbc/testdb" auth="Container" type="javax.sql.DataSource"
          username="test" password="test1234"
          url="*********"
          driverClass="oracle.jdbc.driver.OracleDriver"
          initialSize="5" maxWait="5000"
          maxActive="120" maxIdle="5"
          validationQuery="select 1"
          poolPreparedStatements="true"/>    

Tomcat 的 Context.xml :

<ResourceLink name="jdbc/testdb" global="jdbc/testdb"
type="javax.sql.DataSource" />     

Spring 的 servlet xml :

<jee:jndi-lookup id="dataSource" jndi-name="jdbc/testdb"  resource-ref="true" />

和最终的 Web.xml :

   <resource-ref>
       <description>Resource reference to database</description>
       <res-ref-name>jdbc/testdb</res-ref-name>
       <res-type>javax.sql.DataSource</res-type>
       <res-auth>Container</res-auth>
       <res-sharing-scope>Shareable</res-sharing-scope>
</resource-ref>

有人可以帮助我吗?

错误堆栈跟踪:

  Error querying database.  Cause: org.springframework.jdbc.CannotGetJdbcConnectionException: Could not get JDBC Connection; nested exception is org.apache.tomcat.dbcp.dbcp.SQLNestedException: Cannot create JDBC driver of class '' for connect URL 'null'
java spring spring-jdbc
2个回答
0
投票

尝试用

driverClassName
代替
driverClass

driverClassName="oracle.jdbc.driver.OracleDriver"

0
投票

我正在使用 Eclipse STS 3.9.3、tomcat 8.5 和 Spring Boot 2.0.0.RELEASE

我花了整个上午的时间来思考这个问题。 所以,我做了很多事情来解决:

  1. 从我的 pom 中删除 jdbc 依赖。

        <!-- dependency>
           <groupId>org.postgresql</groupId>
           <artifactId>postgresql</artifactId>
           <scope>runtime</scope>
        </dependency-->
    
  2. 声明资源 TOMCAT_HOME/CONF/server.xml 工厂:

    <GlobalNamingResources>
     ...
       <Resource name="jdbc/postgres_jndi"
                  auth="Container"
                  type="javax.sql.DataSource"
                  driverClassName="org.postgresql.Driver"
                  url="jdbc:postgresql://xxx.xxx.xx.xxx:5432/db"
                  factory="org.apache.tomcat.jdbc.pool.DataSourceFactory"
                  removeAbandonedOnBorrow="true"
                  removeAbandonedOnMaintenance="true"
                  timeBetweenEvictionRunsMillis="10000"
                  removeAbandonedTimeout="60"
                  logAbandoned="true"
                  username="xxx"
                  password="xxxxxx"
                  maxTotal="20"
                  maxIdle="10"
                  maxWaitMillis="-1"/>
     ...
    </GlobalNamingResources>
    

    我意识到我正在使用 Eclipse STS。仅当我将资源声明放入服务器项目的“Tomcat v8.5 Server at localhost-config”文件夹内的 server.xml 中后,server.xml 上的 tomcat 资源才起作用。不适用于 server.xml 文件中的 TOMCAT_HOME/conf 文件夹。

  3. 将我的 application.yml 更改为:

    spring:
      datasource:
        platform: postgres
        jndi-name: java:comp/env/jdbc/postgres_jndi
        type: javax.sql.DataSource
        driver-class-name: org.postgresql.Driver
      jpa:
        hibernate:
          ddl-auto: validate
        database-platform: org.hibernate.dialect.PostgreSQL9Dialect
        database: POSTGRESQL
        show-sql: true
        #Method org.postgresql.jdbc.PgConnection.createClob() is not yet implemented.
        properties:
          hibernate:
            temp:
              use_jdbc_metadata_defaults: false
    

我认为你错过了 java:comp/env/ 你的会是这样的:

<jee:jndi-lookup id="dataSource" jndi-name="java:comp/env/jdbc/testdb" resource-ref="true" />

application.properties 将类似于:

    spring.datasource.platform= postgres
    spring.datasource.jndi-name= java:comp/env/jdbc/postgres_jndi
    spring.datasource.type= javax.sql.DataSource
    spring.datasource.driver-class-name= org.postgresql.Driver
    spring.jpa.hibernate.ddl-auto= validate
    spring.jpa.hibernatedatabase-platform= org.hibernate.dialect.PostgreSQL9Dialect
    spring.jpa.database= POSTGRESQL
    spring.jpa.show-sql= true
    spring.jpa.properties.hibernate.temp.use_jdbc_metadata_defaults=false
  1. 在 src/main/webapp/META-INF/context.xml 中定义一个 context.xml 文件,其定义为:

    <?xml version="1.0" encoding="UTF-8"?>
    <Context path="/myapp">
        <ResourceLink global="jdbc/postgres_jndi" name="jdbc/postgres_jni" type="javax.sql.DataSource"/>
    </Context>
    
  2. 将文件 postgresql-42.2.2.jar 放入 TOMCAT_HOME/lib

© www.soinside.com 2019 - 2024. All rights reserved.