1

I have searched on google and stackoverflow for this problem there are several questions but not resulting to particular solution and reason behind the cause. I am doing a project in JSF with context for my project defined in C:\apache-tomcat-6.0.32\conf\Catalina\localhost\.

Also my in my web.xml I have following configuration:

<context-param> <description>Database server name</description> <param-name>DATABASE</param-name> <param-value>MYSQL</param-value> </context-param> <resource-ref> <description>DB Connection</description> <res-ref-name>jdbc/MySqlDS</res-ref-name> <res-type>javax.sql.DataSource</res-type> <res-auth>Container</res-auth> </resource-ref> 

Now The context file for this project is as follows:

<Context path="/SMS" debug="5" reloadable="true" crossContext="true"> <Resource name="jdbc/MySqlDS" auth="Container" type="javax.sql.DataSource" removeAbandoned="true" removeAbandonedTimeout="30" maxActive="100" maxIdle="30" maxWait="10000" username="root" password="" driverClassName="com.mysql.jdbc.Driver" url="jdbc:mysql://localhost:3306/rosebud"/> </Context> 

The code in my controller is :

InitialContext initialContext = new InitialContext(); Context envContext = (Context)initialContext.lookup("java:/comp/env"); javax.sql.DataSource ds = (javax.sql.DataSource) envContext.lookup("jdbc/MySqlDS"); java.sql.Connection conn = ds.getConnection(); 

Here is my stacktrace:

org.apache.tomcat.dbcp.dbcp.SQLNestedException: Cannot create JDBC driver of class '' for connect URL 'null' at org.apache.tomcat.dbcp.dbcp.BasicDataSource.createConnectionFactory(BasicDataSource.java:1452) at org.apache.tomcat.dbcp.dbcp.BasicDataSource.createDataSource(BasicDataSource.java:1371) at org.apache.tomcat.dbcp.dbcp.BasicDataSource.getConnection(BasicDataSource.java:1044) at com.isys.common.navigation.dao.mysql.TmpNavDAOMySql.getConnection(TmpNavDAOMySql.java:91) at com.isys.common.navigation.dao.mysql.TmpNavDAOMySql.populateModels(TmpNavDAOMySql.java:114) at com.isys.common.navigation.dao.mysql.TmpNavDAOMySql.populateNavModules(TmpNavDAOMySql.java:19) at com.isys.common.navigation.dao.mysql.TmpNavDAOMySqlImpl.populateNavModules(TmpNavDAOMySqlImpl.java:11) at com.isys.common.navigation.controller.TmpNavGenerator.init(TmpNavGenerator.java:12) at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1173) at org.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:993) at org.apache.catalina.core.StandardContext.loadOnStartup(StandardContext.java:4420) at org.apache.catalina.core.StandardContext.start(StandardContext.java:4733) at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1053) at org.apache.catalina.core.StandardHost.start(StandardHost.java:840) at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1053) at org.apache.catalina.core.StandardEngine.start(StandardEngine.java:463) at org.apache.catalina.core.StandardService.start(StandardService.java:525) at org.apache.catalina.core.StandardServer.start(StandardServer.java:754) at org.apache.catalina.startup.Catalina.start(Catalina.java:595) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) at java.lang.reflect.Method.invoke(Unknown Source) at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:289) at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:414) Caused by: java.lang.NullPointerException at sun.jdbc.odbc.JdbcOdbcDriver.getProtocol(Unknown Source) at sun.jdbc.odbc.JdbcOdbcDriver.knownURL(Unknown Source) at sun.jdbc.odbc.JdbcOdbcDriver.acceptsURL(Unknown Source) at java.sql.DriverManager.getDriver(Unknown Source) at org.apache.tomcat.dbcp.dbcp.BasicDataSource.createConnectionFactory(BasicDataSource.java:1437) ... 24 more

The configuration works fine if I run tomcat from outside eclipse but fails when ran from inside eclipse. What might be the cause?

1
  • Now when I changed server setting in eclipse all my project was deleted :( Commented Jun 9, 2011 at 13:09

1 Answer 1

2

Look at the root cause of the exception:

Caused by: java.lang.NullPointerException at sun.jdbc.odbc.JdbcOdbcDriver.getProtocol(Unknown Source) at sun.jdbc.odbc.JdbcOdbcDriver.knownURL(Unknown Source) at sun.jdbc.odbc.JdbcOdbcDriver.acceptsURL(Unknown Source) at java.sql.DriverManager.getDriver(Unknown Source) at org.apache.tomcat.dbcp.dbcp.BasicDataSource.createConnectionFactory(BasicDataSource.java:1437) ... 24 more 

This NPE is clearly a bug in the Sun JDBC ODBC bridge driver. It should have handled it more gracefully or have thrown a more self-explaining exception.

But it is at its own a bigger problem that the Tomcat datasource manager is using the Sun JDBC ODBC bridge driver instead of the MySQL JDBC driver which you specified in the driverClassName of the <Resource>! This means that the <Resource> is not correctly been found/interpreted by Tomcat at all. You should be placing the context.xml file in the /META-INF folder of your webapp. Or you should be putting the <Resource> inside the <Context> of the tomcat/conf/context.xml file.

Sign up to request clarification or add additional context in comments.

5 Comments

Actually The project has it's separate "myapp.xml" I as mentioned in apache-tomcat-6.0.32\conf\Catalina\localhost\ which works if tomcat is deployed not from eclipse i.e. if i deploy application from outside eclipse IDE but not from within IDE. When I started TOMCAT from eclipse it deleted the config file in the folder and now it deleted the project too. I would be glad if you could provide some clear insite into it .Since I looked into your previous solution in other threads too but could not understand :(.
As I answered, it has to go in webapp's /META-INF/context.xml. You can just create the file and folder yourself if it doesn't exist.
yea! I got it but why is it working when deployed from outside IDE ?
Because Eclipse uses by default the workspace metadata space to deploy the webapp to. You would need to reconfigure Eclipse to use Tomcat installation location instead. Doubleclick Tomcat in Eclipse to get the server config, in the Server Locations section you should select Use Tomcat Installation.
When using workspace metadata you should rather edit the files in Eclipse's Servers project, not the ones in Tomcat itself. Eclipse namely won't use them. For that you should be configuring Eclipse to use the Tomcat installation, as described in the previous comment.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.