public class Transactor extends Object
Transactors save the trouble of writing blocks like
Connection connection = null;
try {
connection = getConnection();
// do something with the connection
connection.commit();
} catch (Exception ex){
connection.rollback();
throw ex;
} finally {
if (connection != null){
connection.close();
}
}
Instead, you just write the part in the middle where you do
something with the connection and the other parts are done for you.
Transactors will close the connection when done with their business.
Constructor and Description |
---|
Transactor(Connection connection)
Use this constructor for a use-once Transactor.
|
Transactor(Supplier<Connection> connectionSupplier)
Use this constructor with a connection source, so that the Transactor
can create a new connection each time it needs one.
|
Modifier and Type | Method and Description |
---|---|
void |
runAndCommit(Consumer<Connection> action)
Runs the specified action and either commits when complete or
rolls back on an exception.
|
<RESULT> RESULT |
runAndCommit(Function<Connection,RESULT> function)
Runs the specified function and either commits when complete or
rolls back on an exception.
|
public Transactor(Connection connection)
connection
- the connection to usepublic Transactor(Supplier<Connection> connectionSupplier)
connectionSupplier
- A source for connections.public void runAndCommit(Consumer<Connection> action)
action
- the action to performpublic <RESULT> RESULT runAndCommit(Function<Connection,RESULT> function)
RESULT
- the type of result the function returnsfunction
- the function to runCopyright © 2019. All rights reserved.