简体   繁体   中英

Placing the Resource tag inside conf/context.xml gets an exception but is okay when I place the same inside META-INF/context.xml : Why is that?

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 (ie inside conf/context.xml) but do not get the exception when I place it inside the context.xml specific to my application ?. (ie inside META-INF/context.xml)

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> .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM