#2362 - ENH: Add lock(bean) method as convenience for DB pessimistic locking

This commit is contained in:
Rob Bygrave committed 2021-09-09 18:18:16 +12:00
1 parent b739326fca
commit 9fc2ec044d
5 files changed
+113 -11

No files matched your search

@@ -129,16 +129,7 @@ import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.time.Clock;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.Spliterator;
import java.util.*;
import java.util.concurrent.Callable;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.ReentrantLock;
@@ -930,6 +921,20 @@ public final class DefaultServer implements SpiServer, SpiEbeanServer {
executeInTrans((txn) -> persister.merge(desc, checkEntityBean(bean), options, txn), transaction);
}
@Override
public void lock(Object bean) {
BeanDescriptor<?> desc = descriptor(bean.getClass());
if (desc == null) {
throw new PersistenceException(bean.getClass() + " is NOT an Entity Bean registered with this server?");
}
Object id = desc.id(bean);
Objects.requireNonNull(id, "Bean missing an @Id value which is required to lock");
new DefaultOrmQuery<>(desc, this, expressionFactory)
.setId(id)
.withLock(Query.LockType.DEFAULT, Query.LockWait.NOWAIT)
.findOne();
}
@Override
public <T> Query<T> find(Class<T> beanType) {
return createQuery(beanType);