Skip Headers
Oracle® Containers for J2EE Servlet Developer's Guide
10g (10.1.3.1.0)

Part Number B28959-01
Go to Documentation Home
Home
Go to Book List
Book List
Go to Table of Contents
Contents
Go to Index
Index
Go to Feedback page
Contact Us

Go to previous page
Previous
Go to next page
Next
View PDF

9 Best Practices and Performance

This chapter provides programming tips for how to maximize the efficiency and performance of your servlets—general suggestions as well as suggestions regarding sessions, security, and thread models. There is also an introduction to the Oracle Dynamic Monitoring Service (DMS) to monitor performance. The following topics are covered:

Best Practices for Sessions

This section discusses considerations when using sessions:

See the Oracle Containers for J2EE Developer's Guide for information about clustering in OC4J.

Best Practices for Security

The following are considerations for the security of your Web application running in the OC4J servlet container:

For additional information, also see the reference documentation under "Elements and Attributes of orion-web.xml, global-web-application.xml".

Considerations for Thread Models

For a servlet in a nondistributable environment, a servlet container uses only one servlet instance for each servlet declaration. In a distributable environment, a container uses one servlet instance for each servlet declaration in each JVM. Therefore, a servlet container, including the OC4J servlet container, generally processes concurrent requests to a servlet by using multiple threads for multiple concurrent executions of the central service() method of the servlet.

Servlet developers must keep this in mind, making provisions for simultaneous processing through multiple threads and designing their servlets so that access to shared resources is somehow synchronized or coordinated. Shared resources fall into two main areas:

One option is to synchronize the service() method as a whole; however, this may adversely affect performance.

A better approach is to selectively protect instance or class fields, or access to external resources, through synchronization blocks.

As perhaps a last resort, the servlet specification supports a single-thread model. If a servlet implements the javax.servlet.SingleThreadModel interface, the servlet container must guarantee that there is never more than one request thread at a time in the service() method of any instance of the servlet. OC4J typically accomplishes this by creating a pool of servlet instances, with a separate instance handling each concurrent request. This process has significant performance impact on the servlet container, however, and should be avoided if at all possible. Furthermore, the SingleThreadModel interface will be deprecated in version 2.4 of the servlet specification.

For general information about multithreading, see the Sun Microsystems Java Tutorial on Multithreaded Programming at the following Web site:

http://java.sun.com/docs/books/tutorial/essential/threads/multithreaded.html

Custom Thread Pool

You can create one or more custom thread pools for selected applications to use instead of the default thread pool.

You create a custom thread pool in the server.xml file and then you make it available for applications to use by referring to it in one or more *-web-site.xml files.

Creating a Custom Thread Pool

To create a custom thread pool, add a <custom-thread-pool> element to the server.xml file. The <custom-thread-pool> element is a sub-element of the <application-server> element.

The name attribute is required, all other attributes are optional. The <custom-thread-pool> element has the same attributes as the other thread pool elements discussed in Chapter 10, "Task Manager and Thread Pool Configuration", of the Oracle Containers for J2EE Configuration and Administration Guide. The server.xml file is discussed in the "Overview of the OC4J Server Configuration File (server.xml)" section of Appendix B, "Configuration Files Used in OC4J" of the Oracle Containers for J2EE Configuration and Administration Guide.

The following example creates a custom thread pool named mypool in the server.xml file. The example specifies the following for mypool:

  • The minimum number of threads to create in the pool is 10.

  • The maximum number of threads that can be created in the pool is 100.

  • The number of requests outstanding in each queue can be 50 requests.

  • Idle threads are kept alive for 700 seconds.

<application-server ...>
...
   <custom-thread-pool name="mypool" min="10" max="100" 
    queue="50" keepAlive="700000" debug="true"/>
...
</application-server>

Assigning a Custom Thread Pool to Applications

To instruct applications to use a custom thread pool instead of the default thread pool, add a reference to that custom thread pool in the custom-thread-pool attribute in the <web-site> element in one or more *-web-site.xml files.

Each web application assigned to the web site and port specified in the *-web-site.xml file by being named in a <web-app> element will use the custom thread pool named in the custom-thread-pool attribute.

The *-web-site.xml file is discussed in the "Overview of the Web Site Configuration Files (*-web-site.xml)" section of Appendix B, "Configuration Files Used in OC4J" of the Oracle Containers for J2EE Configuration and Administration Guide.

The following example shows the custom-thread-pool attribute used to name the mypool custom thread pool to be used by all applications named in the <web-app> elements of the srdemo-web-site.xml file:

<?xml version="1.0" ?> 
- <web-site xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:noNamespaceSchemaLocation="http://xmlns.oracle.com/oracleas/schema/web-site-10_0.xsd" 
port="12501" protocol="ajp13" display-name="SRDEMO Web Site" custom-thread-pool="mypool" schema-major-version="10" schema-minor-version="0">
  <default-web-app application="default" name="defaultWebApp" root="/j2ee" /> 
  <web-app application="system" name="dms" root="/dmsoc4j" access-log="false" /> 
  <web-app application="system" name="JMXSoapAdapter-web" root="/JMXSoapAdapter" /> 
  <web-app application="default" name="jmsrouter_web" load-on-startup="true" root="/jmsrouter" /> 
  <web-app application="ascontrol" name="ascontrol" load-on-startup="true" root="/em" /> 
  <web-app application="bc4j" name="webapp" load-on-startup="true" root="/webapp" /> 
  <web-app application="SRDEMO" name="SRDEMO-WEB" load-on-startup="true" root="/SRDEMO" /> 
  <access-log path="../log/default-web-access.log" split="day" /> 
  </web-site>

Best Practices for Performance

This section summarizes issues, mostly documented elsewhere in this manual, that may impact performance:

For further information, also see the reference documentation under "Elements and Attributes of orion-web.xml, global-web-application.xml".

Monitoring Performance

This section includes information about monitoring the performance of servlets.

Oracle Application Server Dynamic Monitoring Service

In an Oracle Application Server environment, the Dynamic Monitoring Service (DMS) adds performance-monitoring features to several components, including OC4J. The goal of DMS is to provide information about runtime behavior through built-in performance measurements so that users can diagnose, analyze, and debug any performance problems. DMS provides this information in a package that can be used at any time, including during live deployment. Data are published through HTTP and can be viewed with a browser.

Standard configuration for DMS modules is reflected in the OC4J system-application.xml file and the default-web-site.xml file. In system-application.xml, the Web module dms and the path to its WAR file is specified. The default-web-site.xml file specifies that this Web module is deployed to the OC4J default application and binds it to its context path. Do not directly alter any of these DMS configurations.

Use Application Server Control to access DMS, display DMS information, and, if appropriate, alter DMS configuration.

Refer to the Oracle Application Server Performance Guide for information about DMS, the Oracle Containers for J2EE Developer's Guide for information about the global application.xml file (which uses the same specification as the orion-application.xml file), and the Oracle Containers for J2EE Configuration and Administration Guide for information about Web site XML files.