Skip Headers
Oracle® Database JDBC Developer's Guide and Reference,
11g Release 1 (11.1)

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

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

20 Statement and Resultset Caching

This chapter describes the benefits and use of statement caching, an Oracle Java Database Connectivity (JDBC) extension.

This chapter contains the following sections:

Note:

In Oracle9i Database 9.2.0 and later releases, Oracle JDBC provides a new statement cache interface and implementation, replacing the application programming interface (API) supported in Oracle9i Database release 1 (9.0.1). The previous API is now deprecated.

About Statement Caching

Statement caching improves performance by caching executable statements that are used repeatedly, such as in a loop or in a method that is called repeatedly. Starting from JDBC 3.0, JDBC standards define a statement-caching interface.

Statement caching can:

This section covers the following topics:

Note:

Oracle strongly recommends you to use the implicit Statement cache. Oracle JDBC drivers are designed on the assumption that the implicit statement cache is enabled. So, not using the Statement cache will have a negative impact on performance.

Basics of Statement Caching

Applications use the statement cache to cache statements associated with a particular physical connection. The cache is associated with an OracleConnection object. OracleConnection includes methods to enable statement caching. When you enable statement caching, a statement object is cached when you call the close method.

Because each physical connection has its own cache, multiple caches can exist if you enable statement caching for multiple physical connections. When you enable statement caching on a connection cache, the logical connections benefit from the statement caching that is enabled on the underlying physical connection. If you try to enable statement caching on a logical connection held by a connection cache, then this will throw an exception.

There are two types of statement caching: implicit and explicit. Each type of statement cache can be enabled or disabled independent of the other. You can have either, neither, or both in effect. Both types of statement caching share a single cache per connection.

Implicit Statement Caching

When you enable implicit statement caching, JDBC automatically caches the prepared or callable statement when you call the close method of this statement object. The prepared and callable statements are cached and retrieved using standard connection object and statement object methods.

Plain statements are not implicitly cached, because implicit statement caching uses a SQL string as a key and plain statements are created without a SQL string. Therefore, implicit statement caching applies only to the OraclePreparedStatement and OracleCallableStatement objects, which are created with a SQL string. You cannot use implicit statement caching with OracleStatement. When you create an OraclePreparedStatement or OracleCallableStatement, the JDBC driver automatically searches the cache for a matching statement. The match criteria are the following:

  • The SQL string in the statement must be identical to one in the cache.

  • The statement type must be the same, that is, prepared or callable.

  • The scrollable type of result sets produced by the statement must be the same, that is, forward-only or scrollable.

If a match is found during the cache search, then the cached statement is returned. If a match is not found, then a new statement is created and returned. In either case, the statement, along with its cursor and state are cached when you call the close method of the statement object.

When a cached OraclePreparedStatement or OracleCallableStatement object is retrieved, the state and data information are automatically re-initialized and reset to default values, while metadata is saved. Statements are removed from the cache to conform to the maximum size using an Least Recently Used (LRU) algorithm.

Note:

The JDBC driver does not clear metadata. However, although metadata is saved for performance reasons, it has no semantic impact. A statement that comes from the implicit cache appears as if it were newly created.

You can prevent a particular statement from being implicitly cached.

Explicit Statement Caching

Explicit statement caching enables you to cache and retrieve selected prepared and callable statements. Explicit statement caching relies on a key, an arbitrary Java String that you provide.

Note:

Plain statements cannot be cached.

Because explicit statement caching retains statement data and state as well as metadata, it has a performance edge over implicit statement caching, which retains only metadata. However, you must be cautious when using this type of caching, because explicit statement caching saves all three types of information for reuse and you may not be aware of what data and state are retained from prior use of the statements.

Implicit and explicit statement caching can be differentiated on the following points:

  • Retrieving statements

    In the case of implicit statement caching, you take no special action to retrieve statements from a cache. Instead, whenever you call prepareStatement or prepareCall, JDBC automatically checks the cache for a matching statement and returns it if found. However, in the case of explicit statement caching, you use specialized Oracle WithKey methods to cache and retrieve statement objects.

  • Providing key

    Implicit statement caching uses the SQL string of a prepared or callable statement as the key, requiring no action on your part. In contrast, explicit statement caching requires you to provide a Java String, which it uses as the key.

  • Returning statements

    During implicit statement caching, if the JDBC driver cannot find a statement in cache, then it will automatically create one. However, during explicit statement caching, if the JDBC driver cannot find a matching statement in cache, then it will return a null value.

Table 20-1 compares the different methods employed in implicit and explicit statement caching.

Table 20-1 Comparing Methods Used in Statement Caching


Allocate Insert Into Cache Retrieve From Cache

Implicit

prepareStatement prepareCall

close

prepareStatement prepareCall

Explicit

createStatement prepareStatement prepareCall

closeWithKey

getStatementWithKey getCallWithKey


Using Statement Caching

This section discusses the following topics:

Enabling and Disabling Statement Caching

When using the OracleConnection API, implicit and explicit statement caching can be enabled or disabled independent of one other. You can have either, neither, or both in effect.

Enabling Implicit Statement Caching

Enable implicit statement caching in one of the following two ways:

  • Call setImplicitCachingEnabled(true) on the connection

  • Call OracleDataSource.getConnection with the ImplicitCachingEnabled property set to true. You set ImplicitCachingEnabled by calling OracleDataSource.setImplicitCachingEnabled(true)

In addition to calling one of these methods, you also need to call OracleConnection.setStatementCacheSize on the physical connection. The argument you supply is the maximum number of statements in the cache. An argument of 0 specifies no caching.

To determine whether implicit caching is enabled, call getImplicitCachingEnabled, which returns true if implicit caching is enabled, false otherwise.

Note:

Enabling statement caching enables both implicit and explicit statement caching.

Disabling Implicit Statement Caching

Disable implicit statement caching by calling setImplicitCachingEnabled(false) on the connection or by setting the ImplicitCachingEnabled property to false.

Enabling Explicit Statement Caching

To enable explicit statement caching you must first set the statement cache size. For setting the cache size, call OracleConnection.setStatementCacheSize method on the physical connection. The argument you supply is the maximum number of statements in the cache. An argument of 0 specifies no caching. To check the cache size, use the getStatementCacheSize method in the following way:

System.out.println("Stmt Cache size is " +
   ((OracleConnection)conn).getStatementCacheSize());

The following code specifies a cache size of ten statements:

((OracleConnection)conn).setStatementCacheSize(10);

Enable explicit statement caching by calling setExplicitCachingEnabled(true) on the connection.

To determine whether explicit caching is enabled, call getExplicitCachingEnabled, which returns true if explicit caching is enabled, false otherwise.

Note:

  • You enable implicit and explicit caching for a particular physical connection independently. Therefore, it is possible to do statement caching both implicitly and explicitly during the same session.

  • Implicit and explicit statement caching share the same cache. Remember this when you set the statement cache size.

Disabling Explicit Statement Caching

Disable explicit statement caching by calling setExplicitCachingEnabled(false). Disabling caching or closing the cache purges the cache. The following example disables explicit statement caching:

((OracleConnection)conn).setExplicitCachingEnabled(false);

Closing a Cached Statement

Perform the following to close a Satement and assure that it is not returned to the cache:

  • Disable caching for that statement

    stmt.setDisableStatementCaching(true);
    
  • Call the close method of the statement object

    stmt.close();
    

Physically Closing a Cached Statement

With implicit statement caching enabled, you cannot physically close statements manually. The close method of a statement object caches the statement instead of closing it. The statement is physically closed automatically under one of following three conditions:

  • When the associated connection is closed

  • When the cache reaches its size limit and the least recently used statement object is preempted from cache by the LRU scheme

  • If you call the close method on a statement for which statement caching is disabled

Using Implicit Statement Caching

Once you enable implicit statement caching, by default, all prepared and callable statements are automatically cached. Implicit statement caching includes the following steps:

  1. Enable implicit statement caching.

  2. Allocate a statement using one of the standard methods.

  3. Disable implicit statement caching for any particular statement you do not want to cache. This is an optional step.

  4. Cache the statement using the close method.

  5. Retrieve the implicitly cached statement by calling the appropriate standard prepare method.

Allocating a Statement for Implicit Caching

To allocate a statement for implicit statement caching, use either the prepareStatement or prepareCall method as you would typically.

The following code allocates a new statement object called pstmt:

PreparedStatement pstmt = conn.prepareStatement    ("UPDATE emp SET ename = ? WHERE rowid = ?");

Disabling Implicit Statement Caching for a Particular Statement

With implicit statement caching enabled for a connection, by default, all callable and prepared statements of that connection are automatically cached. To prevent a particular callable or prepared statement from being implicitly cached, use the setDisableStatementCaching method of the statement object. You can manage cache space by calling the setDisableStatementCaching method on any infrequently used statement.

The following code disables implicit statement caching for pstmt:

PreparedStatement pstmt = conn.prepareStatement("SELECT 1 from DUAL");
((OraclePreparedStatement)pstmt).setDisableStmtCaching(true);
pstmt.close ();

Note:

If you are using JSE 6, then you can disable Statement caching by using the standard JDBC 4.0 method setPoolable:
PreparedStatement.setPoolable(false);

Use the following to check whether the Statement object is poolable:

Statement.isPoolable();

Implicitly Caching a Statement

To cache an allocated statement, call the close method of the statement object. When you call the close method on an OraclePreparedStatement or OracleCallableStatement object, the JDBC driver automatically puts this statement in cache, unless you have disabled caching for this statement.

The following code caches the pstmt statement:

((OraclePreparedStatement)pstmt).close ();

Retrieving an Implicitly Cached Statement

To retrieve an implicitly cached statement, call either the prepareStatement or prepareCall method, depending on the statement type.

The following code retrieves pstmt from cache using the prepareStatement method:

pstmt = conn.prepareStatement ("UPDATE emp SET ename = ? WHERE rowid = ?");

Table 20-2 describes the methods used to allocate statements and retrieve implicitly cached statements.

Table 20-2 Methods Used in Statement Allocation and Implicit Statement Caching

Method Functionality for Implicit Statement Caching

prepareStatement

Performs a cache search that either finds and returns the desired cached OraclePreparedStatement object or allocates a new OraclePreparedStatement object if a match is not found

prepareCall

Performs a cache search that either finds and returns the desired cached OracleCallableStatement object or allocates a new OracleCallableStatement object if a match is not found


Using Explicit Statement Caching

A prepared or callable statement can be explicitly cached when you enable explicit statement caching. Explicit statement caching includes the following steps:

  1. Enable explicit statement caching.

  2. Allocate a statement using one of the standard methods.

  3. Explicitly cache the statement by closing it with a key, using the closeWithKey method.

  4. Retrieve the explicitly cached statement by calling the appropriate Oracle WithKey method, specifying the appropriate key.

  5. Re-cache an open, explicitly cached statement by closing it again with the closeWithKey method. Each time a cached statement is closed, it is re-cached with its key.

Allocating a Statement for Explicit Caching

To allocate a statement for explicit statement caching, use either the createStatement, prepareStatement, or prepareCall method as you would typically.

The following code allocates a new statement object called pstmt:

PreparedStatement pstmt =    conn.prepareStatement ("UPDATE emp SET ename = ? WHERE rowid = ?");

Explicitly Caching a Statement

To explicitly cache an allocated statement, call the closeWithKey method of the statement object, specifying a key. The key is an arbitrary Java String that you provide. The closeWithKey method caches a statement as is. This means the data, state, and metadata are retained and not cleared.

The following code caches the pstmt statement with the key "mykey":

((OraclePreparedStatement)pstmt).closeWithKey ("mykey");

Retrieving an Explicitly Cached Statement

To recall an explicitly cached statement, call either the getStatementWithKey or getCallWithKey methods depending on the statement type.

If you retrieve a statement with a specified key, then the JDBC driver searches the cache for the statement, based on the specified key. If a match is found, then the matching statement is returned along with its state, data, and metadata. This information is as it was when the statement was last closed. If a match is not found, then the JDBC driver returns null.

The following code recalls pstmt from cache using the "mykey" key with the getStatementWithKey method. Recall that the pstmt statement object was cached with the "mykey" key.

pstmt = ((OracleConnection)conn).getStatementWithKey ("mykey");

If you call the creationState method on the pstmt statement object, then the method returns EXPLICIT.

Important:

When you retrieve an explicitly cached statement, ensure that you use the method that is appropriate for your statement type when specifying the key. For example, if you used the prepareStatement method to allocate a statement, then use the getStatementWithKey method to retrieve that statement from cache. The JDBC driver does not verify the type of statement it is returning.

Table 20-3 describes the methods used to retrieve explicitly cached statements.

Table 20-3 Methods Used to Retrieve Explicitly Cached Statements

Method Functionality for Explicit Statement Caching

getStatementWithKey

Specifies the key needed to retrieve a prepared statement from cache

getCallWithKey

Specifies the key needed to retrieve a callable statement from cache


Reusing Statements Objects

The JDBC 3.0 specification introduces the feature of statement pooling that allows an application to reuse a PreparedStatement object in the same way as it uses a Connection object. The PreparedStatement objects can be reused by multiple logical connections in a transparent manner.

This section covers the following topics:

Using a Pooled Statement

An application can find out whether a data source supports statement pooling by calling the isPoolable method from the Statement interface. If the return value is true, then the application knows that the PreparedStatement object is being pooled. The application can also request a statement to be pooled or not pooled by using the setPoolable method from the Statement interface.

Reusing of pooled statement should be completely transparent to the application, that is, the application code should remain the same whether a PreparedStatement object participates in statement pooling or not. If an application closes a PreparedStatement object, it must still call Connection.prepareStatement method in order to reuse it.

Note:

An application has no direct control over how statements are pooled. A pool of statements is associated with a PooledConnection object, whose behavior is determined by the properties of the ConnectionPoolDataSource object that produced it.

Closing a Pooled Statement

An application closes a pooled statement exactly the same way it closes a nonpooled statement. Once a statement is closed, whether is it pooled or nonpooled, it is no longer available for use by the application and an attempt to reuse it causes an exception to be thrown. The only difference visible is that an application cannot directly close a physical statement that is being pooled. This is done by the pool manager. The method PooledConnection.closeAll closes all of the statements open on a given physical connection, which releases the resources associated with those statements.

The following methods can close a pooled statement:

  • close

    This java.sql.Statement interface method is called by an application. If the statement is being pooled, then it closes the logical statement used by the application but does not close the physical statement being pooled

  • close

    This java.sql.Connection interface method is called by an application. This method acts differently depending upon whether the connection using the statement is being pooled or not:

    • Nonpooled connection

      This method closes the physical connection and all statements created by that connection. This is necessary because the garbage collection mechanism is unable to detect when externally managed resources can be released.

    • Pooled connection

      This method closes the logical connection and the logical statements it returned, but leaves open the underlying PooledConnection object and any associated pooled statements

  • PooledConnection.closeAll

    This method is called by the connection pool manager to close all of the physical statements being pooled by the PooledConnection object

Resultset Caching

Your applications sometime require to retrieve huge data from the database, using JDBC, in operations like searching data. When you request for a search, the application might return the whole result set at once. This process takes lot of time and has an impact on performance. This problem can be solved in the following ways:

Server-side Cache

Oracle Database 11g Release 1 (11.1) provides support for server-side resultset caching for both JDBC types. The server-side result cache is used to cache the results of the current query or query fragment in memory and then to use the cached results in future executions of the query or query fragment. The cached results reside in the result cache memory portion of the SGA. A cached result is automatically invalidated whenever a database object used in its creation is successfully modified.

See Also:

Oracle Database Performance Tuning Guide for information about using server-side cache

Client Query Cache

Oracle Database 11g Release 1 (11.1) provides support for client result cache for JDBC OCI client. The client query cache improves performance of applications by caching query result sets in a way that subsequent query executions can access the cached result set without fetching rows from the server. This eliminates some round-trips to the server and reduces CPU usage on the server. This allows significant improvements in response time for frequent client SQL query executions and for fetching rows. The scalability on the server is increased since it expends less CPU time.

See Also:

Oracle Call Interface Programmer's Guide for more information about client query cache.