0

To connect to derby database using tomcat as the server initially I added the following to conf/context.xml of Tomcat :

<Resource name="jdbc/PollDatasource" auth="Container" type="javax.sql.DataSource"
 driverClassName="org.apache.derby.jdbc.ClientDriver"
 url="jdbc:derby://localhost:1527/poll database;create=true"
 username="suhail" password="suhail"
 maxActive="20" maxIdle="10" maxWait="-1" />

and the Resource-ref tag in web.xml of the WEB-INF/web.xml of the project.

<resource-ref>
<description>my connection</description>
<res-ref-name>jdbc/PollDatasource</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>

But when I ran a servlet that had to connect to the database,the statement :

connection = dataSource.getConnection();

caused an exception :

org.apache.tomcat.dbcp.dbcp.SQLNestedException: Cannot create JDBC driver of class '' for connect URL 'null'

I haven't understood the reason for this exception yet.

After the exception I removed the Resource tag from the conf/context.xml of the Tomcat and placed it in the META-INF/context.xml of my project.

When I tried the servlet again,it worked without any exception !

What could be the reason I am getting the exception when I place the Resource tag inside the global context.xml file (i.e inside conf/context.xml) but do not get the exception when I place it inside the context.xml specific to my application ?. (i.e inside META-INF/context.xml)

alain.janinm
  • 19,951
  • 10
  • 65
  • 112
Suhail Gupta
  • 22,386
  • 64
  • 200
  • 328

1 Answers1

0

Adding <Resource> to conf/context.xml will make a copy of that resource available for every webapp you deploy on your server -- probably not what you want to do. If you want a <Resource> to be globally available, it's more appropriate to put it into conf/server.xml under <GlobalNamiongResources>.

I would expect that the error you are getting is because you don't have your JDBC driver in the right place. Defining the <Resource> in conf/context.xml may result in a different ClassLoader being used to load your <Resource>.

Christopher Schultz
  • 20,221
  • 9
  • 60
  • 77
  • what do you actually mean by `JDBC` driver ? I have the driver to connect to the derby database inside the _lib_ folder of _tomcat_. – Suhail Gupta Jul 22 '12 at 17:15
  • I mean the JAR file for the JDBC driver. See your other question you posted that is nearly identical to this one. – Christopher Schultz Jul 22 '12 at 21:25
  • The drivers needed to run have been in their place : inside the _lib_ folder of tomcat – Suhail Gupta Jul 23 '12 at 05:29
  • you didn't tell the reason for the exception when the drivers are there in the right place .... – Suhail Gupta Jul 24 '12 at 07:51
  • I'm sorry I can't inspect your entire configuration. Every time this error has occurred, checking the JDBC driver and, if necessary, completely re-writing the `` definition has fixed the problem. I have also seen slightly mismatched JNDI names cause this kind of problem. – Christopher Schultz Jul 24 '12 at 17:38