Program guide > Access data with client applications > Programming for transactions > Use locking
Configure the lock timeout value
The lock timeout value on a BackingMap instance is used to ensure that an application does not wait endlessly for a lock mode to be granted because of a deadlock condition that occurs due to an application error.
Before you begin
To configure the lock timeout value, the locking strategy must be set to either OPTIMISTIC or PESSIMISTIC. See Configure a locking strategy for more information.
When a LockTimeoutException exception occurs, the application must determine if the timeout is occurring because the application is running slower than expected, or if the timeout occurred because of a deadlock condition. If an actual deadlock condition occurred, then increasing the lock wait timeout value does not eliminate the exception. Increasing the timeout results in the exception taking longer to occur. However, if increasing the lock wait timeout value does eliminate the exception, then the problem occurred because the application was running slower than expected. The application in this case must determine why performance is slow.
To prevent deadlocks from occurring, the lock manager has a default timeout value of 15 seconds. If the timeout limit is exceeded, a LockTimeoutException exception occurs. If the system is heavily loaded, the default timeout value might cause the LockTimeoutException exceptions to occur when no deadlock exists. In this situation, you can increase the lock timeout value programmatically or in the ObjectGrid descriptor XML file.
Procedure
- Configure a lock timeout value programmatically on a BackingMap instance with the setLockTimeout method.
The following example illustrates how to set the lock wait timeout value for the map1 backing map to 60 seconds:
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("map1"); bm.setLockStrategy( LockStrategy.PESSIMISTIC ); bm.setLockTimeout( 60 );To avoid a java.lang.IllegalStateException exception, call both the setLockStrategy method and the setLockTimeout method before calling either the initialize or getSession methods on the ObjectGrid instance. The setLockTimeout method parameter is a Java™ primitive integer that specifies the number of seconds that eXtreme Scale waits for a lock mode to be granted. If a transaction waits longer than the lock wait timeout value configured for the BackingMap, a com.ibm.websphere.objectgrid.LockTimeoutException exception results.
- Configure the lock timeout value using the lockTimeout 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" lockTimeout="60"/> </objectGrid> </objectGrids> </objectGridConfig>
- Override the lock wait timeout for a single ObjectMap instance. Use the ObjectMap.setLockTimeout method to override the lock timeout value for a specific ObjectMap instance. The lock timeout value affects all transactions started after the new timeout value is set. This method can be useful when lock collisions are possible or expected in select transactions.
Parent topic:
Use locking
Related concepts
Locking performance best practices
Map entry locks with query and indexes
Related tasks
Implement exception handling in locking scenarios
Related reference