How to get resource from the context.xml file in tomcat webapp?
Asked Answered
S

2

12

This is my context.xml file:

...
<Resource auth="Container"
          driverClass="net.sourceforge.jtds.jdbc.Driver"
          type="com.jolbox.bonecp.BoneCPDataSource"
          idleMaxAge="240"
          idleConnectionTestPeriod="60"
          partitionCount="3"
          acquireIncrement="1"
          maxConnectionsPerPartition="10"
          minConnectionsPerPartition="3"
          statementsCacheSize="50"
          releaseHelperThreads="4"

          name="jdbc/MyDatasource"
          username="my_username"
          password="my_password"
          factory="org.apache.naming.factory.BeanFactory"
          jdbcUrl="jdbc:jtds:sqlserver://localhost:12345/my_database"
/>
...

I already tried using ServletContext.getResource(java.lang.String) with the name of the resource ("jdbc/MyDatasource"), but Tomcat complains that the name doesn't begin with a '/'. I also tried with "/jdbc/MyDatasource", but this time it returns null.

I mainly need the jdbcUrl to perform a connection check with the database server (see if the server is online and operational).

Subtreasury answered 28/11, 2011 at 15:29 Comment(0)
S
20

Keyword is: JNDI. The resources in the context.xml are not 'System Resources' but JNDI Resources. Try this:

InitialContext ic = new InitialContext();
// that's everything from the context.xml and from the global configuration
Context xmlContext = (Context) ic.lookup("java:comp/env");
DataSource myDatasource = (DataSource) xmlContext.lookup("jdbc/MyDatasource");

// now get a connection to see if everything is fine.
Connection con = ds.getConnection();
// reaching this point means everything is fine.
con.close();
Snorter answered 28/11, 2011 at 15:41 Comment(6)
Thank you for your answer. I don't know how to proceed with accepting the answer since both of you helped me very much... I think I'll accept your answer for encouraging you (since you are a newer user) to keep contributing to this community...Subtreasury
@IgorPopov Well I have the same question but just that I want to read other parameters from context.xml: like maxTotal,maxIdle etc. How can I actually read those as that I can print them in console? I am using the same approach. Is there even any way to read these parameters from context.xml?Chanel
@HarshvardhanSolanki Not that I know of. But you should ask follow up questions as own questions. Be sure to link to existing questions if they do not answer your question, so answers see you have tried to solve it yourself.Snorter
@IgorPopov sir i tried using this code on my config.groovy but i have a problem in instantiating InitialContext(). am i lacking some process?Tisiphone
@Tisiphone Please do ask new questions as new questions, not in comments below other questions. That way everyone who has the same problem as you have can find it far easier. Comments are not well indexed by search engines etc. You can drop a link to your new question here in the comments so that one can find from one to the other.Snorter
@IgorPopov ok sir. my problem is a way to retrieve data from context.xml. i tried using your code but its not working. #49609597Tisiphone
W
11

You should be able to access the datasource with the following code:

Context initialContext = new InitialContext();
Context envContext  = (Context)initialContext.lookup("java:/comp/env");
DataSource ds = (DataSource)envContext.lookup("jdbc/MyDatasource");
Wrecker answered 28/11, 2011 at 15:41 Comment(2)
Thank you very much for your help :) It seemed that I was searching in the wrong place the first time. I found a code sample in the tomcat docs: tomcat.apache.org/tomcat-6.0-doc/…Subtreasury
In tomcat you should change "java:/comp/env" for "java:comp/env"Ticktacktoe

© 2022 - 2024 — McMap. All rights reserved.