Interface TransactionManager
-
- All Known Subinterfaces:
JpaTransactionManager
- All Known Implementing Classes:
DatastoreTransactionManager
,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.void
clearSessionCache()
Clears the session cache if the underlying database is Datastore, otherwise it is a no-op.<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.void
deleteIgnoringReadOnlyWithoutBackup(VKey<?> key)
Performs the delete ignoring any read-only restrictions or backup, for use only in replay.void
deleteWithoutBackup(VKey<?> key)
Deletes the entity by its id without writing commit logs if the underlying database is Datastore.void
deleteWithoutBackup(java.lang.Iterable<? extends VKey<?>> keys)
Deletes the set of entities by their key id without writing commit logs if the underlying database is Datastore.void
deleteWithoutBackup(java.lang.Object entity)
Deletes the given entity from the database without writing commit logs if the underlying database is Datastore.<R> R
doTransactionless(java.util.function.Supplier<R> work)
Executes the work in a transactionless context.<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.void
insertAllWithoutBackup(com.google.common.collect.ImmutableCollection<?> entities)
Persists all new entities in the database without writing any backup if the underlying database is Datastore.void
insertWithoutBackup(ImmutableObject entity)
Persists a new entity in the database without writing any backup if the underlying database is Datastore.boolean
inTransaction()
Returnstrue
if the caller is in a transaction.boolean
isOfy()
Returns true if the transaction manager is DatastoreTransactionManager, false otherwise.<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
putAllWithoutBackup(com.google.common.collect.ImmutableCollection<?> entities)
Persists all new entities or update the existing entities in the database without writing commit logs if the underlying database is Datastore.void
putIgnoringReadOnlyWithoutBackup(java.lang.Object entity)
Performs the write ignoring any read-only restrictions or backup, for use only in replay.void
putWithoutBackup(ImmutableObject entity)
Persists a new entity or update the existing entity in the database without writing commit logs if the underlying database is Datastore.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
transactNew(java.lang.Runnable work)
Pauses the current transaction (if any) and executes the work in a new transaction.<T> T
transactNew(java.util.function.Supplier<T> work)
Pauses the current transaction (if any), executes the work in a new transaction and returns the result.void
transactNewReadOnly(java.lang.Runnable work)
Executes the work in a read-only transaction.<R> R
transactNewReadOnly(java.util.function.Supplier<R> work)
Executes the work in a read-only 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.void
updateAllWithoutBackup(com.google.common.collect.ImmutableCollection<?> entities)
Updates all entities in the database without writing any backup if the underlying database is Datastore.void
updateWithoutBackup(ImmutableObject entity)
Updates an entity in the database without writing commit logs if the underlying database is Datastore.
-
-
-
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.
-
transactNew
<T> T transactNew(java.util.function.Supplier<T> work)
Pauses the current transaction (if any), executes the work in a new transaction and returns the result.Note that this function is kept for backward compatibility. We will review the use case later when adding the cloud sql implementation.
-
transactNew
void transactNew(java.lang.Runnable work)
Pauses the current transaction (if any) and executes the work in a new transaction.Note that this function is kept for backward compatibility. We will review the use case later when adding the cloud sql implementation.
-
transactNewReadOnly
<R> R transactNewReadOnly(java.util.function.Supplier<R> work)
Executes the work in a read-only transaction and returns the result.Note that this function is kept for backward compatibility. We will review the use case later when adding the cloud sql implementation.
-
transactNewReadOnly
void transactNewReadOnly(java.lang.Runnable work)
Executes the work in a read-only transaction.Note that this function is kept for backward compatibility. We will review the use case later when adding the cloud sql implementation.
-
doTransactionless
<R> R doTransactionless(java.util.function.Supplier<R> work)
Executes the work in a transactionless context.
-
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.
-
insertWithoutBackup
void insertWithoutBackup(ImmutableObject entity)
Persists a new entity in the database without writing any backup if the underlying database is Datastore.This method is for the sake of keeping a single code path when replacing ofy() with tm() in the application code. When the method is invoked with Datastore, it won't write the commit log backup; when invoked with Cloud SQL, it behaves the same as the method which doesn't have "WithoutBackup" in its method name because it is not necessary to have the backup mechanism in SQL.
-
insertAllWithoutBackup
void insertAllWithoutBackup(com.google.common.collect.ImmutableCollection<?> entities)
Persists all new entities in the database without writing any backup if the underlying database is Datastore.This method is for the sake of keeping a single code path when replacing ofy() with tm() in the application code. When the method is invoked with Datastore, it won't write the commit log backup; when invoked with Cloud SQL, it behaves the same as the method which doesn't have "WithoutBackup" in its method name because it is not necessary to have the backup mechanism in SQL.
-
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.
-
putWithoutBackup
void putWithoutBackup(ImmutableObject entity)
Persists a new entity or update the existing entity in the database without writing commit logs if the underlying database is Datastore.This method is for the sake of keeping a single code path when replacing ofy() with tm() in the application code. When the method is invoked with Datastore, it won't write the commit log backup; when invoked with Cloud SQL, it behaves the same as the method which doesn't have "WithoutBackup" in its method name because it is not necessary to have the backup mechanism in SQL.
-
putAllWithoutBackup
void putAllWithoutBackup(com.google.common.collect.ImmutableCollection<?> entities)
Persists all new entities or update the existing entities in the database without writing commit logs if the underlying database is Datastore.This method is for the sake of keeping a single code path when replacing ofy() with tm() in the application code. When the method is invoked with Datastore, it won't write the commit log backup; when invoked with Cloud SQL, it behaves the same as the method which doesn't have "WithoutBackup" in its method name because it is not necessary to have the backup mechanism in SQL.
-
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.
-
updateWithoutBackup
void updateWithoutBackup(ImmutableObject entity)
Updates an entity in the database without writing commit logs if the underlying database is Datastore.This method is for the sake of keeping a single code path when replacing ofy calls with tm() in the application code. When the method is invoked with Datastore, it won't write the commit log backup; when invoked with Cloud SQL, it behaves the same as the method which doesn't have "WithoutBackup" in its method name because it is not necessary to have the backup mechanism in SQL.
-
updateAllWithoutBackup
void updateAllWithoutBackup(com.google.common.collect.ImmutableCollection<?> entities)
Updates all entities in the database without writing any backup if the underlying database is Datastore.This method is for the sake of keeping a single code path when replacing ofy calls with tm() in the application code. When the method is invoked with Datastore, it won't write the commit log backup; when invoked with Cloud SQL, it behaves the same as the method which doesn't have "WithoutBackup" in its method name because it is not necessary to have the backup mechanism in SQL.
-
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. In Datastore mode, if the class is a member of the cross-TLD entity group (i.e. if it has the
InCrossTld
annotation, then the correct ancestor query will automatically be applied.
-
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. In Datastore mode, if the class is a member of the cross-TLD entity group (i.e. if it has the
InCrossTld
annotation, then the correct ancestor query will automatically be applied.
-
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.
-
deleteWithoutBackup
void deleteWithoutBackup(VKey<?> key)
Deletes the entity by its id without writing commit logs if the underlying database is Datastore.
-
deleteWithoutBackup
void deleteWithoutBackup(java.lang.Iterable<? extends VKey<?>> keys)
Deletes the set of entities by their key id without writing commit logs if the underlying database is Datastore.
-
deleteWithoutBackup
void deleteWithoutBackup(java.lang.Object entity)
Deletes the given entity from the database without writing commit logs if the underlying database is Datastore.
-
createQueryComposer
<T> QueryComposer<T> createQueryComposer(java.lang.Class<T> entity)
Returns a QueryComposer which can be used to perform queries against the current database.
-
clearSessionCache
void clearSessionCache()
Clears the session cache if the underlying database is Datastore, otherwise it is a no-op.
-
isOfy
boolean isOfy()
Returns true if the transaction manager is DatastoreTransactionManager, false otherwise.
-
putIgnoringReadOnlyWithoutBackup
void putIgnoringReadOnlyWithoutBackup(java.lang.Object entity)
Performs the write ignoring any read-only restrictions or backup, for use only in replay.
-
deleteIgnoringReadOnlyWithoutBackup
void deleteIgnoringReadOnlyWithoutBackup(VKey<?> key)
Performs the delete ignoring any read-only restrictions or backup, for use only in replay.
-
-