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

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Igor Popov
  • 9,795
  • 7
  • 55
  • 68

2 Answers2

19

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();
Igor Popov
  • 9,795
  • 7
  • 55
  • 68
Angelo Fuchs
  • 9,825
  • 1
  • 35
  • 72
  • 2
    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... – Igor Popov Nov 28 '11 at 15:52
  • @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? – Harshvardhan Solanki Apr 26 '16 at 17:50
  • @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. – Angelo Fuchs Apr 27 '16 at 07:18
  • @IgorPopov sir i tried using this code on my config.groovy but i have a problem in instantiating InitialContext(). am i lacking some process? – mendz Apr 03 '18 at 03:45
  • @mendz 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. – Angelo Fuchs Apr 03 '18 at 08:27
  • @IgorPopov ok sir. my problem is a way to retrieve data from context.xml. i tried using your code but its not working. https://stackoverflow.com/questions/49609097/grails-config-get-data-from-context-xml – mendz Apr 03 '18 at 08:34
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");
tobiasbayer
  • 10,269
  • 4
  • 46
  • 64
  • 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: http://tomcat.apache.org/tomcat-6.0-doc/jndi-datasource-examples-howto.html – Igor Popov Nov 28 '11 at 15:49
  • 3
    In tomcat you should change "java:/comp/env" for "java:comp/env" – lmiguelmh Jul 17 '14 at 19:53