Class CacheAdapter


  • public class CacheAdapter
    extends Base
    CacheAdapter encapsulates the operations neccessary to communicate with Coherence™ resource adapter (coherence-tx.rar). The Coherence resource adapter, in turn, is a gateway into the Coherence clustered services, so some methods closely resemble methods at CacheFactory class.

    In a simplest form a client code would look like:

    
       Context ctx = new InitialContext();
    
       CacheAdapter adapter = new CacheAdapter(ctx, "tangosol.coherenceTx",
           CacheAdapter.CONCUR_OPTIMISTIC, CacheAdapter.TRANSACTION_REPEATABLE_GET, 0);
       try
           {
           NamedCache map = adapter.getNamedCache("MyCache", getClass().getClassLoader(),
               new SimpleValidator(), false);
    
           // perform operations on the map
           }
       finally
           {
           adapter.close();
           }
     
    Note: the CacheAdapter is intended to be used by one and only one thread at the time and is not thread safe.

    Since:
    Coherence 1.2
    Author:
    gg 2002.04.14
    • Constructor Detail

      • CacheAdapter

        public CacheAdapter()
        Construct the adapter using a default InitialContext, standard JNDI name, "optimistic" concurrency, "committed" isolation level an no timeout.
      • CacheAdapter

        public CacheAdapter​(Context ctx,
                            String sJndiName,
                            int nConcur,
                            int nIsolation,
                            int nTimeout)
        Construct the adapter using the specified Context and JNDI name
        Parameters:
        ctx - the Context object to use for lookup operation; if null, the default InitialiContext is used
        sJndiName - the JNDI name of the Coherence resource adapter; if null, the default name ("tangosol.coherenceTx") is used
        nConcur - the default concurrency value
        nIsolation - the default transaction isolation value
        nTimeout - the default transaction timeout value (in seconds)
    • Method Detail

      • getReplicatedCache

        public NamedCache getReplicatedCache​(String sName,
                                             ClassLoader loader)
        Obtain an instance of a ReplicatedCache.

        When called within a transaction, the returned cache will be transactional, controlled by the container's transaction coordinator. When called outside of a transaction, the returned cache will be non-transactional, which is equivalent to obtaining the instance directly from CacheFactory.

        Parameters:
        sName - cache name (unique across the cluster). If the NamedCache with the specified name already exists, a reference to the same object will be returned in the same transactional context
        loader - ClassLoader that should be used to deserialize objects inserted in the map by other members of the cluster
        Returns:
        NamedCache interface of the ReplicatedCache service
        Throws:
        IllegalStateException - if the adapter is already connected to a different service
      • getDistributedCache

        public NamedCache getDistributedCache​(String sName,
                                              ClassLoader loader)
        Obtain an instance of a DistributedCache.

        When called within a transaction, the returned cache will be transactional, controlled by the container's transaction coordinator. When called outside of a transaction, the returned cache will be non-transactional, which is equivalent to obtaining the instance directly from CacheFactory.

        Parameters:
        sName - cache name (unique across the cluster). If the NamedCache with the specified name already exists, a reference to the same object will be returned in the same transactional context
        loader - ClassLoader that should be used to deserialize objects inserted in the map by other members of the cluster
        Returns:
        NamedCache interface of the DistributedCache service
        Throws:
        IllegalStateException - if the adapter is already connected to a different service
      • connect

        public void connect​(String sServiceName,
                            String sUserName,
                            String sPassword)
        Connect the adapter to the specified clustered service. The service with the specified name must be running for this call to succeed.
        Parameters:
        sServiceName - service name (unique across the cluster)
        sUserName - name of the user establishing a connection (optional)
        sPassword - password for the user establishing a connection (optional)
        Throws:
        IllegalArgumentException - if the specified service is not running
        IllegalStateException - if the adapter is already connected or
      • connect

        public void connect​(String sServiceName,
                            String sServiceType,
                            String sUserName,
                            String sPassword)
        Connect the adapter to the specified clustered service. If the service with the specified name is not running an attempt will be made to start that service [default configuration]. If the name and type are not specified, a ConfigurableCacheFactory will be used to obtain the NamedCache instances.
        Parameters:
        sServiceName - service name (unique across the cluster)
        sServiceType - service type (i.e. CacheService.TYPE_REPLICATED or CacheService.TYPE_DISTRIBUTED)
        sUserName - name of the user establishing a connection (optional)
        sPassword - password for the user establishing a connection (optional)
        Throws:
        IllegalArgumentException - if the specified service cannot be found
        IllegalStateException - if the adapter is already connected
      • getNamedCache

        public NamedCache getNamedCache​(String sName,
                                        ClassLoader loader)
        Obtain an instance of a NamedCache. The NamedCache will be retrieved from the current ConfigurableCacheFactory using CacheFactory.getCache(sName, loader) unless the adapter is already connected to a specific cache service, in which case the corresponding NamedCache will be retrieved using service.ensureCache(sName, loader).

        When called within a transaction, the returned cache will be transactional, controlled by the container's transaction coordinator. When called outside of a transaction, the returned cache will be non-transactional, which is equivalent to obtaining the instance directly from CacheFactory.

        Parameters:
        sName - cache name (unique across the cluster). If the NamedCache with the specified name already exists, a reference to the same object will be returned in the same transactional context
        loader - ClassLoader that should be used to deserialize objects inserted in the map by other members of the cluster
        Returns:
        NamedCache object
      • getNamedCache

        public NamedCache getNamedCache​(String sName,
                                        ClassLoader loader,
                                        TransactionMap.Validator validator,
                                        boolean fImmutable)
        Obtain an instance of a NamedCache. The NamedCache will be retrieved from the current ConfigurableCacheFactory using CacheFactory.getCache(sName, loader) unless the adapter is already connected to a specific cache service, in which case the corresponding NamedCache will be retrieved using service.ensureCache(sName, loader).

        When called within a transaction, the returned cache will be transactional, controlled by the container's transaction coordinator. When called outside of a transaction, the returned cache will be non-transactional, which is equivalent to obtaining the instance directly from CacheFactory.

        Parameters:
        sName - cache name (unique across the cluster). If the NamedCache with the specified name already exists, a reference to the same object will be returned in the same transactional context
        loader - ClassLoader that should be used to deserialize objects inserted in the map by other members of the cluster
        validator - the Validator object to be used to enlist and validate transactional resources; this parameter is only used within transactional context and only for optimistic concurrency
        fImmutable - specifies whether or not the values kept in this cache are known to be immutable; this parameter is only used within transactional context
        Returns:
        NamedCache object
        Since:
        Coherence 2.3
        See Also:
        TransactionMap.setValidator(), TransactionMap.setValuesImmutable()
      • close

        public void close()
        Close the connection associated with this adapter.
      • isConnected

        protected boolean isConnected()
        Return the connection status.
        Returns:
        true if the adapter is currently connected; false otherwise.
      • toString

        public String toString()
        Return a human readable description of the CacheAdapter.
        Overrides:
        toString in class Object
        Returns:
        a String representation of the CacheAdapter
      • finalize

        protected void finalize()
        Perform cleanup during garbage collection.
        Overrides:
        finalize in class Object