Chapter 25 Managing Persistent Component State
EAServer supports object and query caching for EJB entity beans and entity components that use automatic persistence. Caching can improve performance by minimizing the number of database select queries required for ejbLoad operations, finder method invocations, and ejbSelect method invocations. Most database applications are governed by the 80:20 rule: 80% of users access 20% of the data. Object caching increases performance and scalability by allowing faster access to the most recently used data.
Assuming that the database access is the principal bottleneck, the expected performance gain falls in these ranges, depending on the ratio of update to read-only transactions:
Besides the transaction mix, the actual performance gain depends on:
Read-only methods
When using automatic persistence, the persistence engine detects
read-only method invocations. You do not need to set the Read Only
method property.
Object and query caching place an in-memory cache and a cache manager component in between component instances and the associated database. You can configure the object cache and cache manager used by each entity component. You can configure the query cache used by each finder and ejbSelect method. You can configure caches that are dedicated to a single component or query method or shared by multiple components and query methods.
The cache manager is a component that implements the CtsComponents::ObjectCache IDL interface. EAServer provides a built in component, CtsComponents/ObjectCache, which is a high-performance C++ implementation of this interface. You can provide custom implementations that implement this interface. The cache manager must be coded in C++ or Java, and the Instances/Bind Object property must be enabled for the component. Ideally, cache managers should be coded in C++ to avoid the overhead of Java garbage collection.
Object caching must be configured for each component, as described in "Enabling object caching". Object caching is disabled by default.
Query caching must be configured for each finder and ejbSelect method, on the Persistence/Query Mapping subtab in the Component Properties dialog box. See "Enabling query caching". Query caching is disabled by default.
When data is maintained in the object cache as well as the source database, you must take steps to ensure these transactional constraints are satisfied:
Read consistency Unless database change notification is enabled, transactional read consistency requires that EAServer have exclusive write access to the database tables for your entity components. You can configure your database to notify the cache manager of updates, inserts, and deletes. See "Enabling database change notification" for more information. The same notification technique is used for both object caching and query caching.
In addition, if the component is deployed in a cluster, you must take steps to ensure that row changes are propagated to the object caches for each server in the cluster. This can be achieved with two approaches:
Update consistency Transactional update consistency is ensured by:
For each entity component that uses automatic persistence, enable object caching on the Persistence/Object Cache subtab in the Component Properties dialog box. The settings are:
CtsComponents/ObjectCache
Syntax | To indicate |
---|---|
nMor nm |
n megabytes, for
example:
512M |
nKor nk |
n kilobytes, for
example:
1024K |
n |
n bytes, for example:
536870912 |
sync
property;
the inherited setting can be overridden by setting the component
property.
Option | Explanation |
---|---|
None | (The default.) Indicates no synchronization is performed. This value is appropriate if the component is not deployed in a cluster, or if the cache timeout provides adequate read consistency for transactions. |
Mirror | Replication without transactional consistency. This value is appropriate for entity components that maintain transient data (that is, the data is not saved to persistent database). If you use this option, you must configure mirror pairs for your cluster as described in "Cluster configuration for in-memory failover". |
Replicate | Replication with transactional consistency. For updates, the complete instance state is replicated between servers. |
Invalidate | Replication with transactional consistency.
For updates, the instance state is not replicated. Rather, updates
are propagated by refreshing the cache entry from the remote database.
This value may yield better performance than the replicate
option
if:
In this case, the overhead of replicating instance state may exceed that of refreshing each cache from the database. |
Cache synchronization requires a working message service
If using object caching in a cluster, make sure the EAServer
message service is configured and running on each server. The cache
manager uses the message service for cache synchronization between
servers. The EAServer System Administration Guide describes
how to run the message service.
Creating a named cache
If you want a cache to be shared by multiple components, finder methods or ejbSelect methods, you must create a named cache as follows:
com.sybase.jaguar.objectcache.size=size-value com.sybase.jaguar.objectcache.timeout=timeout-value com.sybase.jaguar.objectcache.sync=sync-method
Named cache property | Component property |
---|---|
com.sybase.jaguar.objectcache.size
|
Cache Size. |
com.sybase.jaguar.objectcache.
|
Cache Timeout |
com.sybase.jaguar.objectcache.sync
|
Cache Synchronization |
Query caching allows EAServer to cache the values returned by finder and ejbSelect method queries. When caching is enabled for a query, the key values returned by each invocation are cached in memory, with the method input parameter values serving as the cache key. Together with entity object caching, query caching can reduce the number of unnecessary database reads.
To enable caching for a finder or ejbSelect query,
append [cache]
to
the end of the Query Mapping property value that corresponds to
the method. For example:
[default][cache]
Or, for a query mapped to an EJB-QL query:
ejbQuery:[cache]
You can specify optional parameters with this syntax:
[cache cache-params]
Where cache-params is a list of parameters listed in Table 25-9, with each parameter separated from the next by white space, for example:
[default][cache size=1M timeout=10]
Parameter | To indicate |
---|---|
name=name | The cache name. Specifying a named cache
allows multiple queries to use one cache. The named cache must be
created and configured as described for named object caches in "Enabling object caching".
Only one of name or size may be specified. |
size=size | The cache size. Only one of name or size may
be specified. The value syntax is:
|
timeout=seconds | The cache timeout in seconds. A value of 0 indicates infinity. |
ignore insert | If database change notification is enabled, inserts do not invalidate the cache. |
ignore delete | If database change notification is enabled, updates do not invalidate the cache. |
ignore update | If database change notification is enabled, updates do not invalidate the cache. |
This feature allows the use of database triggers to notify EAServer's entity object cache of changes to the underlying table rows. The notification mechanism works as follows:
Enabling database change notification
com.sybase.jaguar.server.services
to
include the Message Service and Database Notify components, for
example:
CtsComponents/MessageService,CtsComponents/DatabaseNotify
dn.caches=SybaseCache,OracleCache
cms.cache
property
will be used.
sp_notify={call my_own_notify_proc ?,?}
{call sp_notify ?,?}
This sample script is for Sybase Adaptive Server Enterprise. Modifications are required for use on other databases:
use master go if not exists (select name from sysdatabases where name = "notifydb") begin create database notifydb exec sp_dboption notifydb, "trunc log on chkpt", "true" end go use notifydb go checkpoint go if not exists (select 1 from sysobjects where name="cms_notify" and type="U") begin create table cms_notify ( id numeric(16,0) identity primary key, type char(1) not null, name varchar(100) not null, message varchar(255) not null, options varchar(255) not null ) end go if not exists (select 1 from sysusers where name="guest") exec sp_adduser guest go use sybsystemprocs go if exists (select 1 from sysobjects where name="sp_notify" and type="P") drop proc sp_notify go create proc sp_notify (@from numeric(16,0), @last numeric(16,0)) as if @from <= @last delete from notifydb..cms_notify where id >= @from and id <= @last declare @loop int select @loop = 1 while @loop <= 60 begin declare @rows int select @rows = count(*) from notifydb..cms_notify if @rows > 0 begin set rowcount 100 select id, type, name, message, options from notifydb..cms_notify order by id return end waitfor delay "00:00:01" select @loop = @loop + 1 end go sp_procxmode sp_notify, anymode go grant execute on sp_notify to public go if exists (select 1 from sysobjects where name="sp_publish" and type="P") drop proc sp_publish go create proc sp_publish (@topic varchar(255), @message varchar(255), @options varchar(255)) as insert into notifydb..cms_notify (type, name, message, options) values ("T", @topic, @message, @options) go sp_procxmode sp_publish, anymode go grant execute on sp_publish to public go if exists (select 1 from sysobjects where name="sp_send" and type="P") drop proc sp_send go create proc sp_send (@topic varchar(255), @message varchar(255), @options varchar(255)) as insert into notifydb..cms_notify (type, name, message, options) values ("Q", @topic, @message, @options) go sp_procxmode sp_send, anymode go grant execute on sp_send to public go
The storage component responds to any suitably formatted messages that are published to the configured topic names for each mapped table. You can provide you own implementation of the stored procedures or the notification component.
To publish a change message, the Message Service 'text' property should be "insert", "delete" or "update", each key column should have a corresponding property (unless multiple rows were affected in which case key columns should be omitted). If using the Java Message Service (JMS) to publish the messages, use a TextMessage and use header properties for the key column values.
Copyright © 2002 Sybase, Inc. All rights reserved. |
![]() |