Best unofficial Apache Server developers community
Username
Forgot password?
Sign in with Twitter account
Sign in with Facebook account

How to configure jndi in tomcat regarding my errors?

0

54 views

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/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??

asked June 9, 2011 5:18 am CDT
posted via StackOverflow

1 Answers

1
Best answer
 

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.

answered June 9, 2011 5:52 pm CDT

Your answer

Join with account you already have


Sign in with Twitter account
Sign in with Facebook account
Sign in with Google Friend Connect

Preview
Similar questions