Interface TransactionManager
-
- All Known Subinterfaces:
JpaTransactionManager
- All Known Implementing Classes:
JpaTransactionManagerImpl
public interface TransactionManager
This interface defines the methods to execute database operations with or without a transaction.
-
-
Method Summary
All Methods Instance Methods Abstract Methods Modifier and Type Method Description void
assertInTransaction()
ThrowsIllegalStateException
if the caller is not in a transaction.<T> QueryComposer<T>
createQueryComposer(java.lang.Class<T> entity)
Returns a QueryComposer which can be used to perform queries against the current database.void
delete(VKey<?> key)
Deletes the entity by its id.void
delete(java.lang.Iterable<? extends VKey<?>> keys)
Deletes the set of entities by their key id.<T> T
delete(T entity)
Deletes the given entity from the database.<T> boolean
exists(VKey<T> key)
Returns whether the entity of given key exists.boolean
exists(java.lang.Object entity)
Returns whether the given entity with same ID exists.org.joda.time.DateTime
getTransactionTime()
Returns the time associated with the start of this particular transaction attempt.void
insert(java.lang.Object entity)
Persists a new entity in the database, throws exception if the entity already exists.void
insertAll(com.google.common.collect.ImmutableCollection<?> entities)
Persists all new entities in the database, throws exception if any entity already exists.void
insertAll(ImmutableObject... entities)
Persists all new entities in the database, throws exception if any entity already exists.boolean
inTransaction()
Returnstrue
if the caller is in a transaction.<T> com.google.common.collect.ImmutableList<T>
loadAllOf(java.lang.Class<T> clazz)
Returns a list of all entities of the given type that exist in the database.<T> java.util.stream.Stream<T>
loadAllOfStream(java.lang.Class<T> clazz)
Returns a stream of all entities of the given type that exist in the database.<T> com.google.common.collect.ImmutableList<T>
loadByEntities(java.lang.Iterable<T> entities)
Loads all given entities from the database.<T> com.google.common.collect.ImmutableList<T>
loadByEntitiesIfPresent(java.lang.Iterable<T> entities)
Loads all given entities from the database if possible.<T> T
loadByEntity(T entity)
Loads the given entity from the database.<T> T
loadByKey(VKey<T> key)
Loads the entity by its key.<T> java.util.Optional<T>
loadByKeyIfPresent(VKey<T> key)
Loads the entity by its key, returns empty if the entity doesn't exist.<T> com.google.common.collect.ImmutableMap<VKey<? extends T>,T>
loadByKeys(java.lang.Iterable<? extends VKey<? extends T>> keys)
Loads the set of entities by their keys.<T> com.google.common.collect.ImmutableMap<VKey<? extends T>,T>
loadByKeysIfPresent(java.lang.Iterable<? extends VKey<? extends T>> keys)
Loads the set of entities by their keys.<T> java.util.Optional<T>
loadSingleton(java.lang.Class<T> clazz)
Loads the only instance of this particular class, or empty if none exists.void
put(java.lang.Object entity)
Persists a new entity or update the existing entity in the database.void
putAll(com.google.common.collect.ImmutableCollection<?> entities)
Persists all new entities or updates the existing entities in the database.void
putAll(ImmutableObject... entities)
Persists all new entities or updates the existing entities in the database.void
transact(java.lang.Runnable work)
Executes the work in a transaction.<T> T
transact(java.util.function.Supplier<T> work)
Executes the work in a transaction and returns the result.void
update(java.lang.Object entity)
Updates an entity in the database, throws exception if the entity does not exist.void
updateAll(com.google.common.collect.ImmutableCollection<?> entities)
Updates all entities in the database, throws exception if any entity does not exist.void
updateAll(ImmutableObject... entities)
Updates all entities in the database, throws exception if any entity does not exist.
-
-
-
Method Detail
-
inTransaction
boolean inTransaction()
Returnstrue
if the caller is in a transaction.Note that this function is kept for backward compatibility. We will review the use case later when adding the cloud sql implementation.
-
assertInTransaction
void assertInTransaction()
ThrowsIllegalStateException
if the caller is not in a transaction.Note that this function is kept for backward compatibility. We will review the use case later when adding the cloud sql implementation.
-
transact
<T> T transact(java.util.function.Supplier<T> work)
Executes the work in a transaction and returns the result.
-
transact
void transact(java.lang.Runnable work)
Executes the work in a transaction.
-
getTransactionTime
org.joda.time.DateTime getTransactionTime()
Returns the time associated with the start of this particular transaction attempt.
-
insert
void insert(java.lang.Object entity)
Persists a new entity in the database, throws exception if the entity already exists.
-
insertAll
void insertAll(com.google.common.collect.ImmutableCollection<?> entities)
Persists all new entities in the database, throws exception if any entity already exists.
-
insertAll
void insertAll(ImmutableObject... entities)
Persists all new entities in the database, throws exception if any entity already exists.
-
put
void put(java.lang.Object entity)
Persists a new entity or update the existing entity in the database.
-
putAll
void putAll(ImmutableObject... entities)
Persists all new entities or updates the existing entities in the database.
-
putAll
void putAll(com.google.common.collect.ImmutableCollection<?> entities)
Persists all new entities or updates the existing entities in the database.
-
update
void update(java.lang.Object entity)
Updates an entity in the database, throws exception if the entity does not exist.
-
updateAll
void updateAll(com.google.common.collect.ImmutableCollection<?> entities)
Updates all entities in the database, throws exception if any entity does not exist.
-
updateAll
void updateAll(ImmutableObject... entities)
Updates all entities in the database, throws exception if any entity does not exist.
-
exists
boolean exists(java.lang.Object entity)
Returns whether the given entity with same ID exists.
-
exists
<T> boolean exists(VKey<T> key)
Returns whether the entity of given key exists.
-
loadByKeyIfPresent
<T> java.util.Optional<T> loadByKeyIfPresent(VKey<T> key)
Loads the entity by its key, returns empty if the entity doesn't exist.
-
loadByKeysIfPresent
<T> com.google.common.collect.ImmutableMap<VKey<? extends T>,T> loadByKeysIfPresent(java.lang.Iterable<? extends VKey<? extends T>> keys)
Loads the set of entities by their keys.Nonexistent keys / entities are absent from the resulting map, but no
NoSuchElementException
will be thrown.
-
loadByEntitiesIfPresent
<T> com.google.common.collect.ImmutableList<T> loadByEntitiesIfPresent(java.lang.Iterable<T> entities)
Loads all given entities from the database if possible.Nonexistent entities are absent from the resulting list, but no
NoSuchElementException
will be thrown.
-
loadByKey
<T> T loadByKey(VKey<T> key)
Loads the entity by its key.- Throws:
java.util.NoSuchElementException
- if this key does not correspond to an existing entity.
-
loadByKeys
<T> com.google.common.collect.ImmutableMap<VKey<? extends T>,T> loadByKeys(java.lang.Iterable<? extends VKey<? extends T>> keys)
Loads the set of entities by their keys.- Throws:
java.util.NoSuchElementException
- if any of the keys do not correspond to an existing entity.
-
loadByEntity
<T> T loadByEntity(T entity)
Loads the given entity from the database.- Throws:
java.util.NoSuchElementException
- if the entity does not exist in the database.
-
loadByEntities
<T> com.google.common.collect.ImmutableList<T> loadByEntities(java.lang.Iterable<T> entities)
Loads all given entities from the database.- Throws:
java.util.NoSuchElementException
- if any of the entities do not exist in the database.
-
loadAllOf
<T> com.google.common.collect.ImmutableList<T> loadAllOf(java.lang.Class<T> clazz)
Returns a list of all entities of the given type that exist in the database.The resulting list is empty if there are no entities of this type.
-
loadAllOfStream
<T> java.util.stream.Stream<T> loadAllOfStream(java.lang.Class<T> clazz)
Returns a stream of all entities of the given type that exist in the database.The resulting stream is empty if there are no entities of this type.
-
loadSingleton
<T> java.util.Optional<T> loadSingleton(java.lang.Class<T> clazz)
Loads the only instance of this particular class, or empty if none exists.Throws an exception if there is more than one element in the table.
-
delete
void delete(VKey<?> key)
Deletes the entity by its id.
-
delete
void delete(java.lang.Iterable<? extends VKey<?>> keys)
Deletes the set of entities by their key id.
-
delete
<T> T delete(T entity)
Deletes the given entity from the database.This returns the deleted entity, which may not necessarily be the same as the original entity passed in, as it may be a) converted to a different type of object more appropriate to the database type or b) merged with an object managed by the database entity manager.
-
createQueryComposer
<T> QueryComposer<T> createQueryComposer(java.lang.Class<T> entity)
Returns a QueryComposer which can be used to perform queries against the current database.
-
-