通过jndi获取数据源时出现以下异常:
Caused by: 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
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'
答案 0 :(得分:0)
尝试使用driverClassName
代替driverClass
:
driverClassName="oracle.jdbc.driver.OracleDriver"
答案 1 :(得分:0)
我正在使用Eclipse STS 3.9.3,tomcat 8.5和Spring Boot 2.0.0.RELEASE
我整个上午都在讨论这个问题。 所以,我做了很多事情要解决:
从我的pom中删除jdbc依赖。
<!-- dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<scope>runtime</scope>
</dependency-->
声明资源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>
我意识到我正在使用Eclipse STS。 server.xml上的tomcat资源只有在我将资源声明放在server.xml中的“tomcat v8.5 Server at localhost-config”文件夹中的server.xml之后才能工作。无法在server.xml文件中的TOMCAT_HOME / conf文件夹上工作。
将我的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
使用定义在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>
将文件postgresql-42.2.2.jar放入TOMCAT_HOME / lib