HTTP 500 - Cannot create JDBC driver of class '' for connect URL 'null'

血红的双手。 提交于 2019-12-01 01:13:45

I think your tomcat installation is not using your context.xml. Please create fresh tomcat, configure context.xml and libs and run Tomcat directly from system (without Eclipse etc).


Question was updated, answer below is obsolete :/

The important part is:

Cannot create JDBC driver of class '' for connect URL 'null'

Your driver class is null and connection URL is null.

Application configuration requires configured resource jdbc/TestDB on server:

<resource-ref>
    <description>DB Connection</description>
    <res-ref-name>jdbc/TestDB</res-ref-name>
    <res-type>javax.sql.DataSource</res-type>
    <res-auth>Container</res-auth>
</resource-ref>

and

  <jee:jndi-lookup id="dataSource"
    jndi-name="jdbc/TestDB"
    expected-type="javax.sql.DataSource"
    resource-ref="true"/>   

But your Tomcat configuration Servers/tomcat-config/context.xml is wrong. It looks like spring application configuration.

<web-app id="WebApp_ID" version="2.4"
    xmlns="http://java.sun.com/xml/ns/j2ee" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 
                        http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

 ...

Standard Tomcat context.xml looks like this:

<?xml version='1.0' encoding='utf-8'?>
<Context>
<Resource name="jdbc/TestDB" auth="Container" type="javax.sql.DataSource" 
    username="user" password="pass" 
    driverClassName="com.mysql.jdbc.Driver" 
    url="jdbc:mysql://localhost:3306/testDB?characterEncoding=utf8" maxActive="8"/>

 </Context>

I've eventually found out the solution and I'll share it with those who are encountering the same problem.

Create the META-INF folder under src/main/webapp as WEB-INF sibling Move the Tomcat-config/context.xml file to the META-INF folder. You'll see in the pom.xml that this file is not going to be part of the WAR file.

META-INF/context.xml

<?xml version="1.0" encoding="UTF-8"?>
<Context>

    <!-- Default set of monitored resources -->
    <WatchedResource>WEB-INF/web.xml</WatchedResource>

 <Resource  name="jdbc/TestDB" 
            username="xxxx" 
            password="xxxxxxxxxx" 
            driverClassName="com.mysql.jdbc.Driver"
            url="jdbc:mysql://localhost:3306/TestDB"
            auth="Container" 
            type="javax.sql.DataSource"
            maxActive="100" 
            maxIdle="30" 
            maxWait="10000"
            initialSize="1"/>
</Context>

Adapt the pom.xml

<plugin>
            <groupId>org.apache.tomcat.maven</groupId>
            <artifactId>tomcat7-maven-plugin</artifactId>
            <version>2.2</version>
            <configuration>
                <url>http://127.0.0.1:8080/manager/text</url>
                <server>TomcatServer</server> <!-- user + password defined in the Maven/conf/settings.xml-->
                <path>/${project.build.finalName}</path>
                <port>8080</port>
                <charset>UTF-8</charset>
                <finalName>${project.build.finalName}</finalName>
                <contextFile>${project.basedir}/src/main/webapp/META-INF/context.xml</contextFile> <!--The path of the Tomcat context XML file. This is not used for war deployment mode.-->
            </configuration>
            <dependencies>
                <dependency>
                    <groupId>mysql</groupId>
                    <artifactId>mysql-connector-java</artifactId> <!-- driver mysql -->
                    <version>5.1.27</version>
                </dependency>
            </dependencies>
        </plugin>

MAVEN_HOME/conf/settings.xml The id is referenced in the pom.xml. The username and the password are declared in the tomcat-users.xml

<server>
    <id>TomcatServer</id>
    <username>xxxxx</username>
    <password>xxxxxxxxx</password>
</server>

Eclipse / Run - Run configurations ...

  • create new launch configuration
  • Base direrctory ${workspace_loc:/your project name}
  • Goals clean tomcat7:run

  • Apply

  • Run

Next time you can click on the arrow near the Run As... button in the ToolBar and select the configuration you want to run. The Stop and Relaunch buttons are very useful.

I am using Eclipse STS 3.9.3, tomcat 8.5 and Spring Boot 2.0.0.RELEASE

I spent my entire morning about this problem. So, I did a lot of things to solve:

  1. Remove jdbc dependence from my pom.

        <!-- dependency>
           <groupId>org.postgresql</groupId>
           <artifactId>postgresql</artifactId>
           <scope>runtime</scope>
        </dependency-->
    
  2. Declaration resource TOMCAT_HOME/CONF/server.xml whit factory:

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

    I realized that I was using Eclipse STS. The tomcat resource on server.xml only works after I put the resource declaration in server.xml inside of "Tomcat v8.5 Server at localhost-config" folder in my Server project. Did not work on TOMCAT_HOME/conf folder in server.xml file.

  3. Change my application.yml to:

    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
    

A application.properties will be like:

    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. Define a context.xml file in src/main/webapp/META-INF/context.xml with definition:

    <?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. Put the file postgresql-42.2.2.jar in the TOMCAT_HOME/lib

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!