从Tomcat资源访问oracle,为什么我会收到NamingException?

时间:2016-01-14 15:08:33

标签: java oracle tomcat jndi

尝试通过tomcat Context.xml公开Oracle连接,我得到一个NamingException。我已经关注了几个教程,但我还是得到了例外。救命啊!

爪哇:

String environmentLookup = "java:comp/env/jdbc/ENTERPRISE_DATABASE";
OracleDataSource ods = InitialContext.doLookup(environmentLookup);
Connection connection = ods.getConnection();

App的web.xml:

<resource-ref>
  <description>Oracle Datasource example</description>
  <res-ref-name>jdbc/ENTERPRISE_DATABASE</res-ref-name>
  <res-type>javax.sql.DataSource</res-type>
  <res-auth>Container</res-auth>

Tomcat的context.xml:

<Resource name="jdbc/ENTERPRISE_DATABASE" auth="Container"
    type="javax.sql.Datasource" driverClassName="oracle.jdbc.OracleDriver"
    url="jdbc:oracle:thin:@enterprise.db.company.com:1524:schema1234"
    username="ENTERPRISE_USER" password="ENTERPRISE_PASSWORD" maxActive="20"
    maxIdle="10" maxWait="-1" />

例外:

javax.naming.NamingException: Cannot create resource instance
at org.apache.naming.factory.ResourceFactory.getObjectInstance(ResourceFactory.java:144)
at javax.naming.spi.NamingManager.getObjectInstance(NamingManager.java:304)
at org.apache.naming.NamingContext.lookup(NamingContext.java:842)
at org.apache.naming.NamingContext.lookup(NamingContext.java:153)

1 个答案:

答案 0 :(得分:0)

type="javax.sql.Datasource"

应该是

type="javax.sql.DataSource"