Administration guide > Configure the deployment environment > Configuring data grids
Configure a locking strategy
You can define an optimistic, a pessimistic, or no locking strategy on each BackingMap in the WebSphere eXtreme Scale configuration.
Each BackingMap instance can be configured to use one of the following locking strategies:
- Optimistic locking mode
- Pessimistic locking mode
- None
The default lock strategy is OPTIMISTIC. Use optimistic locking when data is changed infrequently. Locks are only held for a short duration while data is being read from the cache and copied to the transaction. When the transaction cache is synchronized with the main cache, any cache objects that have been updated are checked against the original version. If the check fails, then the transaction is rolled back and an OptimisticCollisionException exception results.
The PESSIMISTIC lock strategy acquires locks for cache entries and should be used when data is changed frequently. Any time a cache entry is read, a lock is acquired and conditionally held until the transaction completes. The duration of some locks can be tuned using transaction isolation levels for the session.
If locking is not required because the data is never updated or is only updated during quiet periods, you can disable locking by using the NONE lock strategy. This strategy is very fast because a lock manager is not required. The NONE lock strategy is ideal for look-up tables or read-only maps. For more information about locking strategies, see Locking strategies .
You can specify a locking strategy programmatically or with XML. For more information about locking, see Locking strategies.
Procedure
- Configure an optimistic locking strategy
- Programmatically using the setLockStrategy method:
import com.ibm.websphere.objectgrid.BackingMap; import com.ibm.websphere.objectgrid.LockStrategy; import com.ibm.websphere.objectgrid.ObjectGrid; import com.ibm.websphere.objectgrid.ObjectGridManagerFactory; ... ObjectGrid og = ObjectGridManagerFactory.getObjectGridManager().createObjectGrid("test"); BackingMap bm = og.defineMap("optimisticMap"); bm.setLockStrategy( LockStrategy.OPTIMISTIC );
- Use the lockStrategy attribute in the ObjectGrid descriptor XML file:
<?xml version="1.0" encoding="UTF-8"?> <objectGridConfig xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://ibm.com/ws/objectgrid/config ../objectGrid.xsd" xmlns="http://ibm.com/ws/objectgrid/config"> <objectGrids> <objectGrid name="test"> <backingMap name="optimisticMap" lockStrategy="OPTIMISTIC"/> </objectGrid> </objectGrids> </objectGridConfig>
- Configure a pessimistic locking strategy
- Programmatically using the setLockStrategy method:
specify pessimistic strategy programmatically import com.ibm.websphere.objectgrid.BackingMap; import com.ibm.websphere.objectgrid.LockStrategy; import com.ibm.websphere.objectgrid.ObjectGrid; import com.ibm.websphere.objectgrid.ObjectGridManagerFactory; ... ObjectGrid og = ObjectGridManagerFactory.getObjectGridManager().createObjectGrid("test"); BackingMap bm = og.defineMap("pessimisticMap"); bm.setLockStrategy( LockStrategy.PESSIMISTIC);
- Use the lockStrategy attribute in the ObjectGrid descriptor XML file:
specify pessimistic strategy using XML <?xml version="1.0" encoding="UTF-8"?> <objectGridConfig xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://ibm.com/ws/objectgrid/config ../objectGrid.xsd" xmlns="http://ibm.com/ws/objectgrid/config"> <objectGrids> <objectGrid name="test"> <backingMap name="pessimisticMap" lockStrategy="PESSIMISTIC"/> </objectGrid> </objectGrids> </objectGridConfig>
- Configure a no locking strategy
- Programmatically using the setLockStrategy method:
import com.ibm.websphere.objectgrid.BackingMap; import com.ibm.websphere.objectgrid.LockStrategy; import com.ibm.websphere.objectgrid.ObjectGrid; import com.ibm.websphere.objectgrid.ObjectGridManagerFactory; ... ObjectGrid og = ObjectGridManagerFactory.getObjectGridManager().createObjectGrid("test"); BackingMap bm = og.defineMap("noLockingMap"); bm.setLockStrategy( LockStrategy.NONE);
- Use the lockStrategy attribute in the ObjectGrid descriptor XML file:
<?xml version="1.0" encoding="UTF-8"?> <objectGridConfig xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://ibm.com/ws/objectgrid/config ../objectGrid.xsd" xmlns="http://ibm.com/ws/objectgrid/config"> <objectGrids> <objectGrid name="test"> <backingMap name="noLockingMap" lockStrategy="NONE"/> </objectGrid> </objectGrids> </objectGridConfig>
What to do next
To avoid a java.lang.IllegalStateException exception, call the setLockStrategy method before calling the initialize or getSession methods on the ObjectGrid instance.
Parent topic:
Configure data grids
Related concepts
Configure write-behind loader support
Related tasks
Configure peer-to-peer replication with JMS
Related reference
ObjectGrid descriptor XML file