Problems using Tomcat JDBC Connection Pool in Tomcat 7
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:
开发者_JAVA百科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.
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.
精彩评论