当Tomcat遭遇JBDC Driver,出现 'Cannot create resource instance' !

用Tomcat,最担心的就是配置JNDI的DataSource的时候出现问题,随着各大厂商发布自己的JDBC的实现,兼容性的问题也就越来也突出了...

一般我们都是用Tomcat自带的DBCP来处理,配置如下:
<Resource name="jdbc/MyDS" auth="Container" type="javax.sql.DataSource" />
<ResourceParams name="jdbc/MyDS">
 <parameter>
  <name>driverClassName</name>
  <value>oracle.jdbc.driver.OracleDriver</value>
 </parameter>
 <parameter>
  <name>driverName</name>
  <value>jdbc:oracle:thin:@server:port:sid</value>
 </parameter>
 <parameter>
  <name>user</name>
  <value>########</value>
 </parameter>
 <parameter>
  <name>password</name>
  <value>########</value>
 </parameter>
</ResourceParams>


Oracle JDBC Driver for JDK 1.4 or JDK 1.5
http://www.oracle.com/technology/software/tech/java/sqlj_jdbc/htdocs/jdbc_10201.html

不过这个写法,如果在JBuilderX中,跑Tomcat 4.1,加上最新版的 Oracle JDBC Driver会存在问题,系统抛出的错误为:

javax.naming.NamingException: Cannot create resource instance
 at org.apache.naming.factory.ResourceFactory.getObjectInstance(ResourceFactory.java:189)
 at javax.naming.spi.NamingManager.getObjectInstance(NamingManager.java:301)
 at org.apache.naming.NamingContext.lookup(NamingContext.java:834)
 at org.apache.naming.NamingContext.lookup(NamingContext.java:181)
 at org.apache.naming.NamingContext.lookup(NamingContext.java:822)
 at org.apache.naming.NamingContext.lookup(NamingContext.java:181)
 at org.apache.naming.NamingContext.lookup(NamingContext.java:822)
 at org.apache.naming.NamingContext.lookup(NamingContext.java:181)
 at org.apache.naming.NamingContext.lookup(NamingContext.java:822)
 at org.apache.naming.NamingContext.lookup(NamingContext.java:194)
 at org.apache.naming.SelectorContext.lookup(SelectorContext.java:183)
 at javax.naming.InitialContext.lookup(InitialContext.java:347)
看来DBCP针对java.sql.DataSource相关的实现与ojdbc14.jar存在兼容性的问题,需要按照下面的写法才能OK:
<Resource name="jdbc/MyDS" type="oracle.jdbc.pool.OracleConnectionCacheImpl"/>
<ResourceParams name="jdbc/MyDS">
 <parameter>
  <name>factory</name>
  <value>oracle.jdbc.pool.OracleDataSourceFactory</value>
 </parameter>
 <parameter>
  <name>driverClassName</name>
  <value>oracle.jdbc.driver.OracleDriver</value>
 </parameter>
 <parameter>
  <name>url</name>
  <value>jdbc:oracle:thin:@server:port:sid</value>
 </parameter>
 <parameter>
  <name>user</name>
  <value>########</value>
 </parameter>
 <parameter>
  <name>password</name>
  <value>########</value>
 </parameter>
</ResourceParams>

原来Oracle自己实现了DataSource的工厂类来管理DataSource的建立!
同样发现微软的SQL Server 2000 Driver for JDBC Service Pack 3也存在这个问题!
http://www.microsoft.com/downloads/details.aspx?familyid=07287b11-0502-461a-b138-2aa54bfdc03a&displaylang=en

需要如下配置:
<Resource name="jdbc/MyDS" auth="Container" type="com.microsoft.jdbcx.sqlserver.SQLServerDataSource"/>
<ResourceParams name="jdbc/MyDS">
  <parameter>
    <name>factory</name>
    <value>com.microsoft.jdbcx.sqlserver.SQLServerDataSourceFactory</value>
  </parameter>
  <parameter>
    <name>driverClassName</name>
    <value>com.microsoft.jdbcx.sqlserver.SQLServerDataSource</value>
  </parameter>
  <parameter>
    <name>driverName</name>
    <value>SQLServer</value>
  </parameter>
  <parameter>
    <name>description</name>
    <value>My DataSource</value>
  </parameter>
  <parameter>
    <name>serverName</name>
    <value>########</value>
  </parameter>
  <parameter>
    <name>portNumber</name>
    <value>1433</value>
  </parameter>
  <parameter>
    <name>selectMethod</name>
    <value>cursor</value>
  </parameter>
  <parameter>
    <name>databaseName</name>
    <value>########</value>
  </parameter>
  <parameter>
    <name>user</name>
    <value>########</value>
  </parameter>
  <parameter>
    <name>password</name>
    <value>########</value>
  </parameter>
  <parameter>
    <name>loginTimeout</name>
    <value>3000</value>
  </parameter>
</ResourceParams>

你可能感兴趣的:(jdk,oracle,sql,tomcat,jdbc,server,server)