0

To connect to derby database while using Tomcat, I downloaded the jar of core Apache Derby database engine (version : 10.9.1.0). I kept this jar file under the lib folder in Tomcat.

enter image description here

Now I was told to add the following in context.xml of Tomcat.

<Resource name="jdbc/PollDatasource" auth="Container" type="javax.sql.DataSource"
    driverClassName="org.apache.derby.jdbc.EmbeddedDriver" 
    url="jdbc:derby://localhost:1527/polldatabase;create=true"
    username="suhail" password="suhail"
    maxActive="20" maxIdle="10" maxWait="-1" />
  • What does this tag do ? I mean what it is meant for ?

  • Though the Jar file i downloaded contains the pattern org.apache.derby.jdbc.EmbeddedDriver where in this tag I mention the jar that I downloaded? Isn't there a need to add the name of archive in the tag ?

Suhail Gupta
  • 22,386
  • 64
  • 200
  • 328
  • The **java** executable must have on its "classpath" all the jars it uses. Trying to load a .class then walks all those jars and looks for org.apache.derby.jdbc.EmbeddedDriver. (Of course this is done more efficiently, a jar can have an index under META-INF, and so on.) – Joop Eggen Jul 17 '12 at 09:37
  • @Joop Eggen how to check if this jar is in my classpath. I just downloaded and dropped that jar under the lib folder. – Suhail Gupta Jul 17 '12 at 09:40
  • This is really a general Tomcat/JavaEE question about class paths in a web application, and isn't at all specific to Derby. So you should re-tag it to remove the 'database' and 'derby' tags, I think. – Bryan Pendleton Jul 17 '12 at 17:55

1 Answers1

3

Once you put a jar under the Tomcat lib folder, Tomcat will automatically load it and put it in the classpath so all applications running on Tomcat know about this jar.

The definition in the XML simply means you defined a datasource. A datasource is used in application servers to manage DB connection pools so you don't have to, it is the preffered way instead of using plain JDBC.

In the xml you defined : driverClassName="org.apache.derby.jdbc.EmbeddedDriver" and since you put the driver jar that contains this class, in the lib folder, it will know where to look for it without you needing to tell it where the jar is.

Please note that putting the jar under Tomcat lib is not always the best solution since, like i said, all applications under tomcat will know of this jar, and if there is an application that already uses this jar with a different version, it might cause conflicts.

A better solution might be to put the jar under WEB-INF/lib and that way only this application knows of the jar.

Tomer
  • 17,787
  • 15
  • 78
  • 137