Class JpaTransactionManagerImpl
- java.lang.Object
-
- google.registry.persistence.transaction.JpaTransactionManagerImpl
-
- All Implemented Interfaces:
JpaTransactionManager
,TransactionManager
public class JpaTransactionManagerImpl extends java.lang.Object implements JpaTransactionManager
Implementation ofJpaTransactionManager
for JPA compatible database.
-
-
Constructor Summary
Constructors Constructor Description JpaTransactionManagerImpl(javax.persistence.EntityManagerFactory emf, Clock clock)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description <T> void
assertDelete(VKey<T> key)
Deletes the entity by its id, throws exception if the entity is not deleted.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.<T> javax.persistence.TypedQuery<T>
criteriaQuery(javax.persistence.criteria.CriteriaQuery<T> criteriaQuery)
Creates a JPA SQL query for the given criteria query.void
delete(VKey<?> key)
Deletes the entity by its id.void
delete(java.lang.Iterable<? extends VKey<?>> vKeys)
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.javax.persistence.EntityManager
getEntityManager()
Returns theEntityManager
for the current request.javax.persistence.EntityManager
getStandaloneEntityManager()
Returns a long-livedEntityManager
not bound to a particular transaction.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.javax.persistence.Query
query(java.lang.String sqlString)
Creates a JPA SQL query for the given query string.<T> javax.persistence.TypedQuery<T>
query(java.lang.String sqlString, java.lang.Class<T> resultClass)
Creates a JPA SQL query for the given query string and result class.JpaTransactionManager
setDatabaseSnapshot(java.lang.String snapshotId)
Specifies a database snapshot exported by another transaction to use in the current transaction.void
teardown()
Releases all resources and shuts down.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
transactNoRetry(java.lang.Runnable work)
Executes the work in a transaction with no retries.<T> T
transactNoRetry(java.util.function.Supplier<T> work)
Executes the work in a transaction with no retries 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.
-
-
-
Constructor Detail
-
JpaTransactionManagerImpl
public JpaTransactionManagerImpl(javax.persistence.EntityManagerFactory emf, Clock clock)
-
-
Method Detail
-
teardown
public void teardown()
Description copied from interface:JpaTransactionManager
Releases all resources and shuts down.The errorprone check forbids injection of
Closeable
resources.- Specified by:
teardown
in interfaceJpaTransactionManager
-
getStandaloneEntityManager
public javax.persistence.EntityManager getStandaloneEntityManager()
Description copied from interface:JpaTransactionManager
Returns a long-livedEntityManager
not bound to a particular transaction.Caller is responsible for closing the returned instance.
- Specified by:
getStandaloneEntityManager
in interfaceJpaTransactionManager
-
getEntityManager
public javax.persistence.EntityManager getEntityManager()
Description copied from interface:JpaTransactionManager
Returns theEntityManager
for the current request.The returned instance is closed when the current transaction completes.
- Specified by:
getEntityManager
in interfaceJpaTransactionManager
-
setDatabaseSnapshot
public JpaTransactionManager setDatabaseSnapshot(java.lang.String snapshotId)
Description copied from interface:JpaTransactionManager
Specifies a database snapshot exported by another transaction to use in the current transaction.This is a Postgresql-specific feature. This method must be called before any other SQL commands in a transaction.
To support large queries, transaction isolation level is fixed at the REPEATABLE_READ to avoid exhausting predicate locks at the SERIALIZABLE level.
- Specified by:
setDatabaseSnapshot
in interfaceJpaTransactionManager
- See Also:
DatabaseSnapshot
-
query
public <T> javax.persistence.TypedQuery<T> query(java.lang.String sqlString, java.lang.Class<T> resultClass)
Description copied from interface:JpaTransactionManager
Creates a JPA SQL query for the given query string and result class.This is a convenience method for the longer
jpaTm().getEntityManager().createQuery(...)
.- Specified by:
query
in interfaceJpaTransactionManager
-
criteriaQuery
public <T> javax.persistence.TypedQuery<T> criteriaQuery(javax.persistence.criteria.CriteriaQuery<T> criteriaQuery)
Description copied from interface:JpaTransactionManager
Creates a JPA SQL query for the given criteria query.- Specified by:
criteriaQuery
in interfaceJpaTransactionManager
-
query
public javax.persistence.Query query(java.lang.String sqlString)
Description copied from interface:JpaTransactionManager
Creates a JPA SQL query for the given query string.This is a convenience method for the longer
jpaTm().getEntityManager().createQuery(...)
.Note that while this method can legally be used for queries that return results, it should not be, as it does not correctly detach entities as must be done for nomulus model objects.
- Specified by:
query
in interfaceJpaTransactionManager
-
inTransaction
public boolean inTransaction()
Description copied from interface:TransactionManager
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.
- Specified by:
inTransaction
in interfaceTransactionManager
-
assertInTransaction
public void assertInTransaction()
Description copied from interface:TransactionManager
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.
- Specified by:
assertInTransaction
in interfaceTransactionManager
-
transact
public <T> T transact(java.util.function.Supplier<T> work)
Description copied from interface:TransactionManager
Executes the work in a transaction and returns the result.- Specified by:
transact
in interfaceTransactionManager
-
transactNoRetry
public <T> T transactNoRetry(java.util.function.Supplier<T> work)
Description copied from interface:JpaTransactionManager
Executes the work in a transaction with no retries and returns the result.- Specified by:
transactNoRetry
in interfaceJpaTransactionManager
-
transact
public void transact(java.lang.Runnable work)
Description copied from interface:TransactionManager
Executes the work in a transaction.- Specified by:
transact
in interfaceTransactionManager
-
transactNoRetry
public void transactNoRetry(java.lang.Runnable work)
Description copied from interface:JpaTransactionManager
Executes the work in a transaction with no retries.- Specified by:
transactNoRetry
in interfaceJpaTransactionManager
-
getTransactionTime
public org.joda.time.DateTime getTransactionTime()
Description copied from interface:TransactionManager
Returns the time associated with the start of this particular transaction attempt.- Specified by:
getTransactionTime
in interfaceTransactionManager
-
insert
public void insert(java.lang.Object entity)
Description copied from interface:TransactionManager
Persists a new entity in the database, throws exception if the entity already exists.- Specified by:
insert
in interfaceTransactionManager
-
insertAll
public void insertAll(com.google.common.collect.ImmutableCollection<?> entities)
Description copied from interface:TransactionManager
Persists all new entities in the database, throws exception if any entity already exists.- Specified by:
insertAll
in interfaceTransactionManager
-
insertAll
public void insertAll(ImmutableObject... entities)
Description copied from interface:TransactionManager
Persists all new entities in the database, throws exception if any entity already exists.- Specified by:
insertAll
in interfaceTransactionManager
-
put
public void put(java.lang.Object entity)
Description copied from interface:TransactionManager
Persists a new entity or update the existing entity in the database.- Specified by:
put
in interfaceTransactionManager
-
putAll
public void putAll(ImmutableObject... entities)
Description copied from interface:TransactionManager
Persists all new entities or updates the existing entities in the database.- Specified by:
putAll
in interfaceTransactionManager
-
putAll
public void putAll(com.google.common.collect.ImmutableCollection<?> entities)
Description copied from interface:TransactionManager
Persists all new entities or updates the existing entities in the database.- Specified by:
putAll
in interfaceTransactionManager
-
update
public void update(java.lang.Object entity)
Description copied from interface:TransactionManager
Updates an entity in the database, throws exception if the entity does not exist.- Specified by:
update
in interfaceTransactionManager
-
updateAll
public void updateAll(com.google.common.collect.ImmutableCollection<?> entities)
Description copied from interface:TransactionManager
Updates all entities in the database, throws exception if any entity does not exist.- Specified by:
updateAll
in interfaceTransactionManager
-
updateAll
public void updateAll(ImmutableObject... entities)
Description copied from interface:TransactionManager
Updates all entities in the database, throws exception if any entity does not exist.- Specified by:
updateAll
in interfaceTransactionManager
-
exists
public <T> boolean exists(VKey<T> key)
Description copied from interface:TransactionManager
Returns whether the entity of given key exists.- Specified by:
exists
in interfaceTransactionManager
-
exists
public boolean exists(java.lang.Object entity)
Description copied from interface:TransactionManager
Returns whether the given entity with same ID exists.- Specified by:
exists
in interfaceTransactionManager
-
loadByKeyIfPresent
public <T> java.util.Optional<T> loadByKeyIfPresent(VKey<T> key)
Description copied from interface:TransactionManager
Loads the entity by its key, returns empty if the entity doesn't exist.- Specified by:
loadByKeyIfPresent
in interfaceTransactionManager
-
loadByKeysIfPresent
public <T> com.google.common.collect.ImmutableMap<VKey<? extends T>,T> loadByKeysIfPresent(java.lang.Iterable<? extends VKey<? extends T>> keys)
Description copied from interface:TransactionManager
Loads the set of entities by their keys.Nonexistent keys / entities are absent from the resulting map, but no
NoSuchElementException
will be thrown.- Specified by:
loadByKeysIfPresent
in interfaceTransactionManager
-
loadByEntitiesIfPresent
public <T> com.google.common.collect.ImmutableList<T> loadByEntitiesIfPresent(java.lang.Iterable<T> entities)
Description copied from interface:TransactionManager
Loads all given entities from the database if possible.Nonexistent entities are absent from the resulting list, but no
NoSuchElementException
will be thrown.- Specified by:
loadByEntitiesIfPresent
in interfaceTransactionManager
-
loadByKey
public <T> T loadByKey(VKey<T> key)
Description copied from interface:TransactionManager
Loads the entity by its key.- Specified by:
loadByKey
in interfaceTransactionManager
-
loadByKeys
public <T> com.google.common.collect.ImmutableMap<VKey<? extends T>,T> loadByKeys(java.lang.Iterable<? extends VKey<? extends T>> keys)
Description copied from interface:TransactionManager
Loads the set of entities by their keys.- Specified by:
loadByKeys
in interfaceTransactionManager
-
loadByEntity
public <T> T loadByEntity(T entity)
Description copied from interface:TransactionManager
Loads the given entity from the database.- Specified by:
loadByEntity
in interfaceTransactionManager
-
loadByEntities
public <T> com.google.common.collect.ImmutableList<T> loadByEntities(java.lang.Iterable<T> entities)
Description copied from interface:TransactionManager
Loads all given entities from the database.- Specified by:
loadByEntities
in interfaceTransactionManager
-
loadAllOf
public <T> com.google.common.collect.ImmutableList<T> loadAllOf(java.lang.Class<T> clazz)
Description copied from interface:TransactionManager
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.
- Specified by:
loadAllOf
in interfaceTransactionManager
-
loadAllOfStream
public <T> java.util.stream.Stream<T> loadAllOfStream(java.lang.Class<T> clazz)
Description copied from interface:TransactionManager
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.
- Specified by:
loadAllOfStream
in interfaceTransactionManager
-
loadSingleton
public <T> java.util.Optional<T> loadSingleton(java.lang.Class<T> clazz)
Description copied from interface:TransactionManager
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.
- Specified by:
loadSingleton
in interfaceTransactionManager
-
delete
public void delete(VKey<?> key)
Description copied from interface:TransactionManager
Deletes the entity by its id.- Specified by:
delete
in interfaceTransactionManager
-
delete
public void delete(java.lang.Iterable<? extends VKey<?>> vKeys)
Description copied from interface:TransactionManager
Deletes the set of entities by their key id.- Specified by:
delete
in interfaceTransactionManager
-
delete
public <T> T delete(T entity)
Description copied from interface:TransactionManager
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.
- Specified by:
delete
in interfaceTransactionManager
-
createQueryComposer
public <T> QueryComposer<T> createQueryComposer(java.lang.Class<T> entity)
Description copied from interface:TransactionManager
Returns a QueryComposer which can be used to perform queries against the current database.- Specified by:
createQueryComposer
in interfaceTransactionManager
-
assertDelete
public <T> void assertDelete(VKey<T> key)
Description copied from interface:JpaTransactionManager
Deletes the entity by its id, throws exception if the entity is not deleted.- Specified by:
assertDelete
in interfaceJpaTransactionManager
-
-