为什么我无法在Tomcat 8中使用JNDI DataSource?

时间:2016-10-24 14:21:18

标签: tomcat jndi

我处于这种情况,已经超过两天,但无法使用JNDI获得数据库连接。

我收到以下错误

  

org.apache.tomcat.dbcp.dbcp2.BasicDataSourceFactory.getObjectInstance   Name = myDatabase忽略未知属性:“描述”属性的“我的数据源新”值

我正在使用Tomcat-8 Java-1.8Spring-3Maven 3.2.5

我正在尝试在Tomcat中部署我的Web应用程序。我创建了war并放在TOMCAT / webapp目录中。

数据库的Tomcat / conf / server.xml文件配置

server.xml

<GlobalNamingResources>

<Resource name="jdbc/myDatabase"
              auth="Container"
              type="javax.sql.DataSource"
              factory="org.apache.tomcat.jdbc.pool.DataSourceFactory"
              driverClassName="oracle.jdbc.OracleDriver"
             url="jdbc:oracle:thin:@IpAddress:orcl"
              username="tomcat_dbrds"
              password="cmxinfa"
              initialSize="1"
              minIdle="1"
              maxIdle="1"
              maxActive="3"
              maxWait="1000"
              validationQuery="select 1 from dual"
              jdbcInterceptors="org.apache.tomcat.jdbc.pool.interceptor.ConnectionState;org.apache.tomcat.jdbc.pool.interceptor.StatementFinalizer;org.apache.tomcat.jdbc.pool.interceptor.SlowQueryReport(threshold=1500)"
            />
  </GlobalNamingResources>

Spring Application上下文内容

<jee:jndi-lookup id="monitoringDataSource" jndi-name="jdbc/myDatabase" expected-type="javax.sql.DataSource"/>   

web.xml 内容

  <resource-ref>
    <description>My DataSource new</description>
    <res-ref-name>jdbc/myDatabase</res-ref-name>
    <res-type>javax.sql.DataSource</res-type>
    <res-auth>Container</res-auth>
  </resource-ref>

我错过了什么?

我已尝试过所有可能的配置,但没有任何效果。

<Resource name="myDatabase" 
              global="monitords"
              factory="org.apache.tomcat.dbcp.dbcp2.BasicDataSourceFactory"
              auth="Container"
              type="javax.sql.DataSource"
              username="tomcat_dbrds"
              password="cmxinfa"
              driverClassName="oracle.jdbc.OracleDriver"
              url="jdbc:oracle:thin:@myipaddress:1521:orcl"
              initialSize="20"
              maxWaitMillis="15000"
              maxTotal="100"
              maxIdle="20"
              defaultAutoCommit="true"
              />


<Resource name="jdbc/myDatabase"
          auth="Container"
          factory="org.apache.tomcat.dbcp.dbcp2.BasicDataSourceFactory"
              type="javax.sql.DataSource"
              validationQuery="SELECT 1"
              validationInterval="30000"
              timeBetweenEvictionRunsMillis="30000"
              maxActive="20"
              maxIdle="10"
              minIdle="1"
              maxWait="20000"
              initialSize="20"
              removeAbandonedTimeout="60"
          removeAbandoned="true"
              logAbandoned="true"
              minEvictableIdleTimeMillis="30000"
              jmxEnabled="true"
              jdbcInterceptors="org.apache.tomcat.jdbc.pool.interceptor.ConnectionState;org.apache.tomcat.jdbc.pool.interceptor.StatementFinalizer"
              username="tomcat_dbrds"
              password="cmxinfa"
              driverClassName="oracle.jdbc.OracleDriver"
              url="jdbc:oracle:thin:@myipaddress:1521:orcl"/>




<Resource name="jdbc/myDatabase"
              auth="Container"
              type="javax.sql.DataSource"
              factory="org.apache.tomcat.jdbc.pool.DataSourceFactory"
              driverClassName="oracle.jdbc.OracleDriver"
             url="jdbc:oracle:thin:@myipaddress:1521:orcl"
              username="tomcat_dbrds"
              password="cmxinfa"
              initialSize="1"
              minIdle="1"
              maxIdle="1"
              maxActive="3"
              maxWait="1000"
              validationQuery="select 1 from dual"
              jdbcInterceptors="org.apache.tomcat.jdbc.pool.interceptor.ConnectionState;org.apache.tomcat.jdbc.pool.interceptor.StatementFinalizer;org.apache.tomcat.jdbc.pool.interceptor.SlowQueryReport(threshold=1500)"
            />

更新1:

我已从web.xml删除了描述标记,但我仍然遇到同样的错误。我刚检查了服务器日志,实际错误如下。 我该如何解决这个错误。

  

java.sql.SQLException:无法创建类''的JDBC驱动程序   连接网址'null'

更新2:

堆栈跟踪

Caused by: org.springframework.jdbc.CannotGetJdbcConnectionException: Could not get JDBC Connection; nested exception is java.sql.SQLException: Cannot create JDBC driver of class '' for connect URL 'null'
    at org.springframework.jdbc.datasource.DataSourceUtils.getConnection(DataSourceUtils.java:80)
    at org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:382)
    at org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:456)
    at org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:464)
    at org.springframework.jdbc.core.JdbcTemplate.queryForList(JdbcTemplate.java:497)
    at com.globalss.dnb.monitor.dao.ApplicationConfig.<init>(ApplicationConfig.java:24)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
    at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:148)
    ... 43 more
Caused by: java.sql.SQLException: Cannot create JDBC driver of class '' for connect URL 'null'
    at org.apache.tomcat.dbcp.dbcp2.BasicDataSource.createConnectionFactory(BasicDataSource.java:2160)
    at org.apache.tomcat.dbcp.dbcp2.BasicDataSource.createDataSource(BasicDataSource.java:2032)
    at org.apache.tomcat.dbcp.dbcp2.BasicDataSource.getConnection(BasicDataSource.java:1532)
    at org.springframework.jdbc.datasource.DataSourceUtils.doGetConnection(DataSourceUtils.java:111)
    at org.springframework.jdbc.datasource.DataSourceUtils.getConnection(DataSourceUtils.java:77)
    ... 53 more
Caused by: java.lang.NullPointerException
    at oracle.jdbc.driver.OracleDriver.acceptsURL(OracleDriver.java:620)
    at java.sql.DriverManager.getDriver(DriverManager.java:299)
    at org.apache.tomcat.dbcp.dbcp2.BasicDataSource.createConnectionFactory(BasicDataSource.java:2144)
    ... 57 more

2 个答案:

答案 0 :(得分:0)

tomcat_dir \ conf \ context.xml - 工作。使用eclipse-tomcat环境

答案 1 :(得分:-1)

将资源定义放在tomcat_dir\conf\context.xml中的上下文标记之间。

tomcat context configuration file