Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 651
Igor Popov Avatar asked Nov 28 '11 15:11

Igor Popov


People also ask

How do I set DataSource in Tomcat context xml?

Application context. xml - This is the easiest way to configure DataSource, all we need is a context. xml file in META-INF directory. We have to define Resource element in the context file and container will take care of loading and configuring it.

Where is Tomcat context xml?

context. xml file is the application deployment descriptor for the Apache Tomcat server. In a deployed application, this file is located in the META-INF folder of the web application directory or the WAR file, for example, tomcat/webapps/app-core/META-INF/context. xml .

What is resource ref in web xml?

<resource-ref> introduces another layer of indirection: you specify the name you want to use in the web. xml, and, depending on the container, provide a binding in a container-specific configuration file. Finally, it returns the object registered under the name of jdbc/PrimaryDBInTheContainer .

Where is Tomcat context path?

The context path refers to the location relative to the server's address which represents the name of the web application. By default, Tomcat derives it from the name of the deployed war-file. So if we deploy a file ExampleApp. war, it will be available at http://localhost:8080/ExampleApp.


1 Answers

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();
like image 89
Angelo Fuchs Avatar answered Oct 18 '22 09:10

Angelo Fuchs