在 Tomcat 7 中使用 Tomcat JDBC 连接池的问题
在 server.xml 中:
<GlobalNamingResources>
<Resource name="jdbc/ArchiveDB"
auth="Container"
type="javax.sql.DataSource"
driverClassName="oracle.jdbc.OracleDriver"
factory="org.apache.tomcat.jdbc.pool.DataSourceFactory"
...etc
在 web.xml 中:
<resource-ref>
<description>Archive Database</description>
<res-ref-name>jdbc/ArchiveDB</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
我的代码:
Context ic = new InitialContext();
DataSource ds = (DataSource) ic.lookup( "java:/comp/env/jdbc/ArchiveDB" );
我收到以下异常:
java.lang.ClassCastException: org.apache.tomcat.dbcp.dbcp.BasicDataSource cannot be cast to org.apache.tomcat.jdbc.pool.DataSource
知道我出了什么问题吗?似乎资源中的工厂字段没有被使用,但我不知道如何找出原因。我有什么想法可以进步吗?
更新 1。深入研究源代码,在 ResourceFactory.java 中发现以下内容
if (ref.getClassName().equals("javax.sql.DataSource")) {
String javaxSqlDataSourceFactoryClassName =
System.getProperty("javax.sql.DataSource.Factory",
Constants.DBCP_DATASOURCE_FACTORY);
我想我必须设置该系统属性,这样它就不会恢复为默认值。
更新2。 现在已为启动设置以下内容:
-Djavax.sql.DataSource.Factory=org.apache.tomcat.jdbc.pool.DataSourceFactory
并收到不同的错误:
09-Jun-2011 14:48:20 org.apache.naming.NamingContext lookup
WARNING: Unexpected exception resolving reference
java.sql.SQLException
at org.apache.tomcat.jdbc.pool.PooledConnection.connectUsingDriver(PooledConnection.java:243)
Caused by: java.lang.NullPointerException
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Unknown Source)
at org.apache.tomcat.jdbc.pool.PooledConnection.connectUsingDriver(PooledConnection.java:236)
... 57 more
javax.naming.NamingException
at org.apache.naming.NamingContext.lookup(NamingContext.java:843)
我猜它无法获取我指定的驱动程序信息。
In server.xml:
<GlobalNamingResources>
<Resource name="jdbc/ArchiveDB"
auth="Container"
type="javax.sql.DataSource"
driverClassName="oracle.jdbc.OracleDriver"
factory="org.apache.tomcat.jdbc.pool.DataSourceFactory"
...etc
In web.xml:
<resource-ref>
<description>Archive Database</description>
<res-ref-name>jdbc/ArchiveDB</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
My Code:
Context ic = new InitialContext();
DataSource ds = (DataSource) ic.lookup( "java:/comp/env/jdbc/ArchiveDB" );
I am getting the following exception:
java.lang.ClassCastException: org.apache.tomcat.dbcp.dbcp.BasicDataSource cannot be cast to org.apache.tomcat.jdbc.pool.DataSource
Any idea what I have got wrong? It seems that the factory field in the resource is not being used, but I have no idea how to find out why. Any ideas how I can progress?
Update 1. Delving into source code, found the following in ResourceFactory.java
if (ref.getClassName().equals("javax.sql.DataSource")) {
String javaxSqlDataSourceFactoryClassName =
System.getProperty("javax.sql.DataSource.Factory",
Constants.DBCP_DATASOURCE_FACTORY);
I guess I have to set that system property so it doesn't revert to default.
Update 2.
Have now set the following for startup:
-Djavax.sql.DataSource.Factory=org.apache.tomcat.jdbc.pool.DataSourceFactory
And getting different error:
09-Jun-2011 14:48:20 org.apache.naming.NamingContext lookup
WARNING: Unexpected exception resolving reference
java.sql.SQLException
at org.apache.tomcat.jdbc.pool.PooledConnection.connectUsingDriver(PooledConnection.java:243)
Caused by: java.lang.NullPointerException
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Unknown Source)
at org.apache.tomcat.jdbc.pool.PooledConnection.connectUsingDriver(PooledConnection.java:236)
... 57 more
javax.naming.NamingException
at org.apache.naming.NamingContext.lookup(NamingContext.java:843)
I guess it is not able to pick up the driver info I specified.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
看来现在正在工作。我认为这是我在 Eclipse 下调试的问题,所以我没有使用我认为我正在使用的 server.xml 。 Eclipse将其复制到tomcat目录下。解决方案是删除并在Eclipse下重新创建以使server.xml中的更改生效。
Seems working now. I think this was a problem that I was debugging under Eclipse, so I wasn't using the server.xml that I thought I was using. Eclipse copies the one under the tomcat directory. Solution is to delete and the recreate under Eclipse to get changes in server.xml to be effective.