Class DatabaseAwareBean.AbstractSQL
java.lang.Object
uk.ac.manchester.spinnaker.alloc.db.DatabaseAwareBean.AbstractSQL
- All Implemented Interfaces:
AutoCloseable
- Enclosing class:
- DatabaseAwareBean
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 Summary
Fields -
Constructor Summary
ConstructorsModifierConstructorDescriptionprotected
Manage a connection ourselves.protected
Use an existing connection. -
Method Summary
Modifier and TypeMethodDescriptionvoid
close()
final void
transaction
(DatabaseAPI.Transacted action) A nestable transaction runner.final <T> T
transaction
(DatabaseAPI.TransactedWithResult<T> action) A nestable transaction runner.final <T> T
transactionRead
(DatabaseAPI.TransactedWithResult<T> action) A nestable transaction runner.
-
Field Details
-
conn
The connection.
-
-
Constructor Details
-
AbstractSQL
protected AbstractSQL()Manage a connection ourselves. -
AbstractSQL
Use an existing connection. Caller looks after its management.- Parameters:
conn
- The connection to piggy-back onto.
-
-
Method Details
-
transaction
A nestable transaction runner. If theaction
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 ofaction
- Parameters:
action
- The code to run inside the transaction.- Returns:
- Whatever the
action
returns.
-
transactionRead
A nestable transaction runner. If theaction
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 noUPDATE
s may be performed as locks cannot be safely upgraded.- Type Parameters:
T
- The type of the result ofaction
- Parameters:
action
- The code to run inside the transaction.- Returns:
- Whatever the
action
returns.
-
transaction
A nestable transaction runner. If theaction
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
- Returns:
- The encapsulated connection.
-
close
public void close()- Specified by:
close
in interfaceAutoCloseable
-