Class DatabaseAwareBean

java.lang.Object
uk.ac.manchester.spinnaker.alloc.db.SQLQueries
uk.ac.manchester.spinnaker.alloc.db.DatabaseAwareBean
Direct Known Subclasses:
AdminControllerImpl, AllocatorTask, BlacklistStore, BMPController, LocalAuthProviderImpl, MachineDefinitionLoader, MachineStateControl, QuotaManager, Spalloc, UserControl

public abstract class DatabaseAwareBean
extends SQLQueries
A support class to make accessing the database a bit simpler. It factors out some common patterns.
Author:
Donal Fellows
  • Constructor Details

  • Method Details

    • getConnection

      @MustBeClosed protected final DatabaseAPI.Connection getConnection()
      Get a connection to the application database. Connections may be shared, but might not be (especially in the case of testing databases).
      Returns:
      Database connection. Requires closing.
    • getHistoricalConnection

      @MustBeClosed protected final DatabaseAPI.Connection getHistoricalConnection()
      Get a connection to the historical database. Connections may be shared, but might not be (especially in the case of testing databases).
      Returns:
      Database connection. Requires closing.
    • isHistoricalDBAvailable

      protected final boolean isHistoricalDBAvailable()
      Determine if the historical database can be used.
      Returns:
      Whether the historical database can be used.
    • execute

      protected <T> T execute​(DatabaseAPI.ConnectedWithResult<T> operation)
      A connection manager and nestable transaction runner. If the operation completes normally (and this isn't a nested use), the transaction commits. If an exception is thrown, the transaction is rolled back. The connection is closed up in any case. A write lock is used.

      It is the caller's responsibility to ensure that the correct transaction type is used.

      Type Parameters:
      T - The type of the result of operation
      Parameters:
      operation - The operation to run
      Returns:
      the value returned by operation
      Throws:
      RuntimeException - If something goes wrong with the contained code.
    • executeRead

      protected <T> T executeRead​(DatabaseAPI.ConnectedWithResult<T> operation)
      A connection manager and nestable read-only transaction runner (it is an error to do an UPDATE using this transaction). If the operation completes normally (and this isn't a nested use), the transaction commits. If an exception is thrown, the transaction is rolled back. The connection is closed up in any case. A read lock (i.e., shared) is used.

      It is the caller's responsibility to ensure that the correct transaction type is used; read locks may not be upgraded to write locks (due to deadlock risk).

      Type Parameters:
      T - The type of the result of operation
      Parameters:
      operation - The operation to run
      Returns:
      the value returned by operation
      Throws:
      RuntimeException - If something goes wrong with the contained code.