用Tomcat,最担心的就是配置JNDI的DataSource的时候出现问题,随着各大厂商发布自己的JDBC的实现,兼容性的问题也就越来也突出了...
(本文来源于图老师网站,更多请访问http://m.tulaoshi.com/java/)一般我们都是用Tomcat自带的DBCP来处理,配置如下:
<Resource name="jdbc/MyDS" auth="Container" type="javax.sql.DataSource" /
<ResourceParams name="jdbc/MyDS"
<parameter
<namedriverClassName</name
<valueoracle.jdbc.driver.OracleDriver</value
</parameter
<parameter
<namedriverName</name
<valuejdbc:oracle:thin:@server:port:sid</value
</parameter
<parameter
<nameuser</name
<value########</value
</parameter
<parameter
<namepassword</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
<namefactory</name
<valueoracle.jdbc.pool.OracleDataSourceFactory</value
</parameter
<parameter
<namedriverClassName</name
<valueoracle.jdbc.driver.OracleDriver</value
</parameter
<parameter
<nameurl</name
<valuejdbc:oracle:thin:@server:port:sid</value
</parameter
<parameter
<nameuser</name
<value########</value
</parameter
<parameter
<namepassword</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
<namefactory</name
<valuecom.microsoft.jdbcx.sqlserver.SQLServerDataSourceFactory</value
</parameter
<parameter
<namedriverClassName</name
<valuecom.micr