Skip to content

Optimistic Lock

Optimistic locking ensures data consistency in concurrent scenarios through a version number mechanism. When updating, the version field is automatically incremented and added to the WHERE clause. If the data has been modified by another transaction, an exception is thrown or 0 affected rows are returned.

Global Configuration

Configure optimistic lock behavior via OptimisticLockConfig:

OptionDefaultDescription
enabledfalseWhether to enable optimistic locking
fieldNameversionVersion field name
initialVersion0LDefault initial version value
throwOnFailuretrueWhether to throw an exception on optimistic lock failure
optimisticLockProcessorDefaultOptimistic lock processor to control which tables need processing

Enabling Example

java
import com.tang.kite.config.optimistic.OptimisticLockConfig;

OptimisticLockConfig.setEnabled(true);

@Version Annotation

Use the @Version annotation to mark the version field, which takes precedence over fieldName in global configuration.

AttributeTypeDescription
initialValueLongInitial version value, default 0L
throwOnFailureBooleanWhether to throw an exception on optimistic lock failure, default true
java
import com.tang.kite.annotation.optimistic.Version;

public class Account {

    private Long id;
    private String name;
    @Version(initialValue = 0L)
    private Long version;

}

Optimistic Lock Processor

Implement the OptimisticLockProcessor interface to customize the optimistic lock processor and control which tables need optimistic lock processing.

MethodDescriptionReturn Type
processable(tableClass: Class<*>)Determines whether a specific table entity class needs optimistic lock processing, default trueBoolean

Custom Processor

java
import com.tang.kite.config.optimistic.OptimisticLockProcessor;

public class AccountOptimisticLockProcessor implements OptimisticLockProcessor {

    @Override
    public boolean processable(Class<?> tableClass) {
        return tableClass == Account.class;
    }

}

Configuring Custom Processor

java
OptimisticLockConfig.setOptimisticLockProcessor(new AccountOptimisticLockProcessor());

Optimistic Lock Manager

OptimisticLockManager is used to temporarily enable or skip optimistic locking in specific scenarios.

Method List

MethodDescriptionReturn Value
withOptimisticLock(block: Runnable)Temporarily use optimistic lock to execute the code blockNone
withOptimisticLock(block: Supplier<T>): TTemporarily use optimistic lock to execute the code block and return the resultT the return value of the code block
withSkip(block: Runnable)Skip optimistic lock and execute the code blockNone
withSkip(block: Supplier<T>): TSkip optimistic lock and execute the code block, returning the resultT the return value of the code block

Usage Example

Temporarily Use Optimistic Lock

java
import com.tang.kite.optimistic.OptimisticLockManager;

OptimisticLockManager.withOptimisticLock(() -> {
    accountMapper.updateById(account);
});

Skip Optimistic Lock

java
import com.tang.kite.optimistic.OptimisticLockManager;

OptimisticLockManager.withSkip(() -> {
    accountMapper.updateById(account);
});

Released under the MIT License