Class DatabaseAwareBean.AbstractSQL

java.lang.Object
uk.ac.manchester.spinnaker.alloc.db.DatabaseAwareBean.AbstractSQL
All Implemented Interfaces:
AutoCloseable
Enclosing class:
DatabaseAwareBean

protected abstract class DatabaseAwareBean.AbstractSQL
extends Object
implements AutoCloseable
Encapsulation of a connection. Can either do the management itself or use a connection managed outside; the difference is important mainly during testing as tests often use in-memory DBs.
Author:
Donal Fellows
  • Field Details

  • Constructor Details

    • AbstractSQL

      protected AbstractSQL()
      Manage a connection ourselves.
    • AbstractSQL

      protected AbstractSQL​(DatabaseAPI.Connection conn)
      Use an existing connection. Caller looks after its management.
      Parameters:
      conn - The connection to piggy-back onto.
  • Method Details

    • transaction

      public final <T> T transaction​(DatabaseAPI.TransactedWithResult<T> action)
      A nestable transaction runner. If the action completes normally (and this isn't a nested use), the transaction commits. If a runtime exception is thrown, the transaction is rolled back (and the exception flows through). A write lock is used.
      Type Parameters:
      T - The type of the result of action
      Parameters:
      action - The code to run inside the transaction.
      Returns:
      Whatever the action returns.
    • transactionRead

      public final <T> T transactionRead​(DatabaseAPI.TransactedWithResult<T> action)
      A nestable transaction runner. If the action completes normally (and this isn't a nested use), the transaction commits. If a runtime exception is thrown, the transaction is rolled back (and the exception flows through). A read lock is used; multiple read locks may be held at once, but no UPDATEs may be performed as locks cannot be safely upgraded.
      Type Parameters:
      T - The type of the result of action
      Parameters:
      action - The code to run inside the transaction.
      Returns:
      Whatever the action returns.
    • transaction

      public final void transaction​(DatabaseAPI.Transacted action)
      A nestable transaction runner. If the action completes normally (and this isn't a nested use), the transaction commits. If a runtime exception is thrown, the transaction is rolled back (and the exception flows through). A write lock is used.
      Parameters:
      action - The code to run inside the transaction.
    • getConnection

      public DatabaseAPI.Connection getConnection()
      Returns:
      The encapsulated connection.
    • close

      public void close()
      Specified by:
      close in interface AutoCloseable