Interface TransactionalMap<K,​V>

  • Type Parameters:
    K - type of the map key
    V - type of the map value
    All Superinterfaces:
    BaseMap<K,​V>, DistributedObject, TransactionalObject

    public interface TransactionalMap<K,​V>
    extends TransactionalObject, BaseMap<K,​V>
    Transactional implementation of BaseMap.

    MapStore Interaction

    When using MapStore, the call to any MapStore method is outside the transactional boundary. If you need to have an XATransaction spanning Hazelcast operations and one more other XAResources (such as a database), you should not use MapStore. Instead, enlist both resources in a transaction as shown below:
    
     HazelcastInstance client = HazelcastClient.newHazelcastClient();
    
     UserTransactionManager tm = new UserTransactionManager();
     tm.setTransactionTimeout(60);
     tm.begin();
    
     HazelcastXAResource xaResource = client.getXAResource();
     Transaction transaction = tm.getTransaction();
     transaction.enlistResource(xaResource);
    
     // you can enlist more resources here like a database XAResource
     try {
          TransactionContext context = xaResource.getTransactionContext()
          TransactionalMap map = context.getMap("map");
          map.put("key", "value");
          final TransactionalQueue queue = context.getQueue("queue");
          queue.offer("item");
    
          // you can do other resource operations like store/delete to a database
    
          transaction.delistResource(xaResource, XAResource.TMSUCCESS);
          tm.commit();
     } catch (TransactionException t) {
          t.printStackTrace();
          transaction.delistResource(xaResource, XAResource.TMFAIL);
          tm.rollback();
     }
     
    See Also:
    BaseMap, IMap
    • Method Detail

      • containsKey

        boolean containsKey​(java.lang.Object key)
        Transactional implementation of IMap.containsKey(Object).
        Specified by:
        containsKey in interface BaseMap<K,​V>
        Parameters:
        key - The specified key.
        Returns:
        true if this map contains an entry for the specified key.
        Throws:
        java.lang.NullPointerException - if the specified key is null
        See Also:
        IMap.containsKey(Object)
      • get

        V get​(java.lang.Object key)
        Transactional implementation of IMap.get(Object).
        Specified by:
        get in interface BaseMap<K,​V>
        Parameters:
        key - The specified key.
        Returns:
        The value for the specified key.
        Throws:
        java.lang.NullPointerException - if the specified key is null
        See Also:
        IMap.get(Object)
      • getForUpdate

        V getForUpdate​(java.lang.Object key)
        Locks the key and then gets and returns the value to which the specified key is mapped. The lock will be released at the end of the transaction (either commit or rollback).
        Throws:
        java.lang.NullPointerException - if the specified key is null
        TransactionTimedOutException - if the key could not be locked for update in the transaction timeout
        See Also:
        IMap.get(Object)
      • size

        int size()
        Transactional implementation of Map.size().
        Specified by:
        size in interface BaseMap<K,​V>
        Returns:
        the number of entries in this map.
        See Also:
        Map.size()
      • isEmpty

        boolean isEmpty()
        Transactional implementation of Map.isEmpty().
        Specified by:
        isEmpty in interface BaseMap<K,​V>
        Returns:
        true if this map contains no entries.
        See Also:
        Map.isEmpty()
      • put

        V put​(K key,
              V value)
        Transactional implementation of IMap.put(Object, Object).

        The object to be put will be accessible only in the current transaction context till transaction is committed.

        Specified by:
        put in interface BaseMap<K,​V>
        Parameters:
        key - The specified key.
        value - The value to associate with the key.
        Returns:
        Previous value associated with key or null if there was no mapping for key.
        Throws:
        java.lang.NullPointerException - if the specified key or value is null
        TransactionTimedOutException - if the key could not be locked for update in the transaction timeout
        See Also:
        IMap.put(Object, Object)
      • put

        V put​(K key,
              V value,
              long ttl,
              java.util.concurrent.TimeUnit timeunit)
        Transactional implementation of IMap.put(Object, Object, long, TimeUnit).

        The object to be put will be accessible only in the current transaction context till transaction is committed.

        Specified by:
        put in interface BaseMap<K,​V>
        Parameters:
        key - The specified key.
        value - The value to associate with the key.
        ttl - maximum time for this entry to stay in the map 0 means infinite.
        timeunit - time unit for the ttl
        Returns:
        Previous value associated with key or null if there was no mapping for key.
        Throws:
        java.lang.NullPointerException - if the specified key, value or timeunit is null
        TransactionTimedOutException - if the key could not be locked for update in the transaction timeout
        See Also:
        IMap.put(Object, Object, long, TimeUnit)
      • set

        void set​(K key,
                 V value)
        Transactional implementation of IMap.set(Object, Object).

        The object to be set will be accessible only in the current transaction context till transaction is committed.

        Specified by:
        set in interface BaseMap<K,​V>
        Parameters:
        key - The specified key.
        value - The value to associate with the key.
        Throws:
        java.lang.NullPointerException - if the specified key or value is null
        TransactionTimedOutException - if the key could not be locked for update in the transaction timeout
        See Also:
        IMap.set(Object, Object)
      • putIfAbsent

        V putIfAbsent​(K key,
                      V value)
        Transactional implementation of IMap.putIfAbsent(Object, Object).

        The object to be put will be accessible only in the current transaction context until the transaction is committed.

        Specified by:
        putIfAbsent in interface BaseMap<K,​V>
        Parameters:
        key - The specified key.
        value - The value to associate with the key when there is no previous value.
        Returns:
        The previous value associated with key, or null if there was no mapping for key.
        Throws:
        java.lang.NullPointerException - if the specified key or value is null
        TransactionTimedOutException - if the key could not be locked for update in the transaction timeout
        See Also:
        IMap.putIfAbsent(Object, Object)
      • replace

        V replace​(K key,
                  V value)
        Transactional implementation of IMap.replace(Object, Object).

        The object to be replaced will be accessible only in the current transaction context until the transaction is committed.

        Specified by:
        replace in interface BaseMap<K,​V>
        Parameters:
        key - The specified key.
        value - The value to replace the previous value.
        Returns:
        The previous value associated with key, or null if there was no mapping for key.
        Throws:
        java.lang.NullPointerException - if the specified key or null
        TransactionTimedOutException - if the key could not be locked for update in the transaction timeout
        See Also:
        IMap.replace(Object, Object)
      • replace

        boolean replace​(K key,
                        V oldValue,
                        V newValue)
        Transactional implementation of IMap.replace(Object, Object, Object).

        The object to be replaced will be accessible only in the current transaction context until the transaction is committed.

        Specified by:
        replace in interface BaseMap<K,​V>
        Parameters:
        key - The specified key.
        oldValue - Replace the key value if it is the old value.
        newValue - The new value to replace the old value.
        Returns:
        true if the value was replaced.
        Throws:
        java.lang.NullPointerException - if the specified key, oldValue or newValue is null
        TransactionTimedOutException - if the key could not be locked for update in the transaction timeout
        See Also:
        IMap.replace(Object, Object, Object)
      • remove

        V remove​(java.lang.Object key)
        Transactional implementation of IMap.remove(Object).

        The object to be removed will be removed from only the current transaction context until the transaction is committed.

        Specified by:
        remove in interface BaseMap<K,​V>
        Parameters:
        key - Remove the mapping for this key.
        Returns:
        The previous value associated with key, or null if there was no mapping for key.
        Throws:
        java.lang.NullPointerException - if the specified key is null
        TransactionTimedOutException - if the key could not be locked for update in the transaction timeout
        See Also:
        IMap.remove(Object)
      • delete

        void delete​(java.lang.Object key)
        Transactional implementation of IMap.delete(Object).

        The object to be deleted will be removed from only the current transaction context until the transaction is committed.

        Specified by:
        delete in interface BaseMap<K,​V>
        Parameters:
        key - Remove the mapping for this key.
        Throws:
        java.lang.NullPointerException - if the specified key is null
        TransactionTimedOutException - if the key could not be locked for update in the transaction timeout
        See Also:
        IMap.delete(Object)
      • remove

        boolean remove​(java.lang.Object key,
                       java.lang.Object value)
        Transactional implementation of IMap.remove(Object, Object).

        The object to be removed will be removed from only the current transaction context until the transaction is committed.

        Specified by:
        remove in interface BaseMap<K,​V>
        Parameters:
        key - The specified key.
        value - Remove the key if it has this value.
        Returns:
        true if the value was removed.
        Throws:
        java.lang.NullPointerException - if the specified key or value null
        TransactionTimedOutException - if the key could not be locked for update in the transaction timeout
        See Also:
        IMap.remove(Object, Object)
      • keySet

        java.util.Set<K> keySet()
        Transactional implementation of IMap.keySet().
        Specified by:
        keySet in interface BaseMap<K,​V>
        Returns:
        a set clone of the keys contained in this map.
        See Also:
        IMap.keySet()
      • values

        java.util.Collection<V> values()
        Transactional implementation of IMap.values().
        Specified by:
        values in interface BaseMap<K,​V>
        Returns:
        a collection clone of the values contained in this map
        See Also:
        IMap.values()