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 Summary
Fields Modifier and Type Field Description protected DatabaseAPI.Connection
conn
The connection. -
Constructor Summary
Constructors Modifier Constructor Description protected
AbstractSQL()
Manage a connection ourselves.protected
AbstractSQL(DatabaseAPI.Connection conn)
Use an existing connection. -
Method Summary
Modifier and Type Method Description void
close()
DatabaseAPI.Connection
getConnection()
void
transaction(DatabaseAPI.Transacted action)
A nestable transaction runner.<T> T
transaction(DatabaseAPI.TransactedWithResult<T> action)
A nestable transaction runner.<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
-