Java Tutorial - Java Scipt : Editing Tomcat’s server.xml

Java Tutorial - Java Scipt :

Editing Tomcat’s server.xml

Although some people may find that the Admin utility is the easiest way to configure and manage a Tomcat server, others find it easiest to directly edit the server.xml file that Tomcat uses for its initialization. The configuration
file server.xml is found the in the conf directory within the Tomcat installation.
 It is a good idea to always create a backup of the server.xml file before you start editing it.

To add a Data Source, we need to add a resource to Tomcat by inserting the Resource element. The Resource element supports the attributes shown in Table 8.6.
We also need to add a Parameter element with a set of parameters that describes how to configure the resource. The Parameter element requires a name attribute that must match the name of the associated resource element. The following is an example of the XML for configuring an HSQLDB data source.

<ResourceParams name=”jdbc/hsqltest”>

The above is the bare minimum for configuring the connection. It does not provide any configuration parameters for the connection pool. The parameters for the connection pool can be included with the ResourceParams. According to the Tomcat documentation, the supported parameters for configuring the pool are:

maxActive . A number representing the maximum number of active instances that can be allocated from this pool at the same time.

maxIdle.  A number representing the maximum number of connections that can sit idle in this pool at the same time.

maxWait. The maximum number of milliseconds that the pool will wait (when there are no available connections) for a connection to be returned before throwing an exception.

validationQuery. A string representing a SQL query that can be used by the pool to validate connections before they are returned to the application. If specified, this query must be a SQL SELECT statement that at least one row.

For example:

<value>5000</value><!-- wait up to 5 seconds ‡
<value>SELECT * FROM CUSTOMER</value>

After you have created the Resource and Resource Parameters elements, you need to insert them into the server.xml file. There are three candidate locations:

·        Inside the Global Resources element

·        Inside a Default Context element

·        Inside the Context element for the Web application

If the new elements are placed inside the Global Resources element, then they can be made visible to all applications. To add a resource to the Global Reference section, search for the <GlobalResource> tag and then the matching </GlobalResource> end tag. Copy the XML section with the Resource and Resource parameter elements, as shown previously, just before the end tag. Each application that needs to access the resource should also add a <resource-ref> tag to the web.xml file for that application. An example of a <resource-ref> element is:

Resource for connecting to the mysqltest database

 A number of users of Tomcat 4.1.x have experienced problems initializing the database connection pool for DataSources configured in global resources. We recommend configuring DataSources in a default context instead to avoid these problems.

A<resource-ref> element is not necessary if the data source is configured in a default context. The default context establishes default values for all contexts contained in a Tomcat Host or Engine elements. The DefaultContext element is used to configure the default context values for automatically deployed Web applications. Data sources that are established here are visible to all application contexts supported and defined within the current host (or engine) context. This is the preferred means of establishing a data source that is to be shared across applications. To add the Resource and Resource Parameter elements to a default context, search for the <DefaultContext> tag and </Default- Context> end tag. If a default context is not defined, it can be added inside of a <Host> element or an <Engine> element. The example below shows a sample <DefaultContext> element.

<!-- place Resource elements here -->
<!-- place Resource Parameter elements here -->

The useNaming attribute of the default context should be set to “true” to enable a JNDI InitialContext. The Resource and Resource Parameter Elements should be placed inside the Default Context element, between the tags, as
shown by the comment markers.

A resource can also be added directly within a Context element if an application
has a context element within the server.xml file. In this case, the resource is specific to that application context and is not shared across application contexts.