Class JpaTransactionManagerImpl

    • 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()
      Throws IllegalStateException 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 the EntityManager for the current request.
      javax.persistence.EntityManager getStandaloneEntityManager()
      Returns a long-lived EntityManager 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()
      Returns true 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.
      <T> T transactWithoutBackup​(java.util.function.Supplier<T> work)
      Execute the work in a transaction without recording the transaction for replay to datastore.
      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.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • 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 interface JpaTransactionManager
      • getStandaloneEntityManager

        public javax.persistence.EntityManager getStandaloneEntityManager()
        Description copied from interface: JpaTransactionManager
        Returns a long-lived EntityManager not bound to a particular transaction.

        Caller is responsible for closing the returned instance.

        Specified by:
        getStandaloneEntityManager in interface JpaTransactionManager
      • getEntityManager

        public javax.persistence.EntityManager getEntityManager()
        Description copied from interface: JpaTransactionManager
        Returns the EntityManager for the current request.

        The returned instance is closed when the current transaction completes.

        Specified by:
        getEntityManager in interface JpaTransactionManager
      • 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 interface JpaTransactionManager
        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 interface JpaTransactionManager
      • 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 interface JpaTransactionManager
      • 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 interface JpaTransactionManager
      • inTransaction

        public boolean inTransaction()
        Description copied from interface: TransactionManager
        Returns true 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 interface TransactionManager
      • assertInTransaction

        public void assertInTransaction()
        Description copied from interface: TransactionManager
        Throws IllegalStateException 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 interface TransactionManager
      • 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 interface TransactionManager
      • transactWithoutBackup

        public <T> T transactWithoutBackup​(java.util.function.Supplier<T> work)
        Description copied from interface: JpaTransactionManager
        Execute the work in a transaction without recording the transaction for replay to datastore.
        Specified by:
        transactWithoutBackup in interface JpaTransactionManager
      • 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 interface JpaTransactionManager
      • 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 interface TransactionManager
      • 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 interface TransactionManager
      • 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 interface TransactionManager
      • 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 interface TransactionManager
      • 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 interface TransactionManager
      • 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 interface TransactionManager
      • 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 interface TransactionManager
      • 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 interface TransactionManager
      • 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 interface TransactionManager
      • 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 interface TransactionManager
      • 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 interface TransactionManager
      • 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 interface TransactionManager
      • 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 interface TransactionManager
      • 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 interface TransactionManager
      • 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 interface TransactionManager
      • 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 interface TransactionManager
      • 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 interface TransactionManager