A question about Transactions in adba
Douglas Surber
douglas.surber at oracle.com
Mon Nov 26 17:52:59 UTC 2018
Session.transactionCompletion does not start a transaction. It returns a new instance of TransactionCompletion. Nothing more. It does not do a round trip.
A TransactionOutcome indicates the outcome of a transaction completion. It is an enum.
A TransactionCompletion is used to signal to a endTransaction Operation how to complete the current active transaction. It is basically a mutable flag. Operations themselves are immutable once submitted. A TransactionCompletion is a mutable flag that an endTransaction Operation is configured with. After the endTransaction Operation is submitted the mutable TransactionCompletion can be modified to cause a rollback instead of the default commit.
Each call to Session.transactionCompletion returns a new instance. Multiple calls are fine.
Example:
try (Session session = ds.getSession()) {
TransactionCompletion completion = session.transactionCompletion();
session.rowCountOperation(updateSql)
.set(param, value)
.onError( t -> completion.setRollbackOnly() )
.submit();
session.endTransaction(completion);
}
By default a TransactionCompletion signals to end the transaction with a commit. So if there is no error the endTransaction Operation will commit the active transaction. If the UPDATE throws an error the TransactionCompletion will be flipped to signal a rollback, then when the endTransaction Operation is executed it will end the active transaction with a rollback.
Douglas
> On Nov 24, 2018, at 11:27 AM, Alexander Kjäll <alexander.kjall at gmail.com> wrote:
>
> Hi
>
> I have some questions about the Session.transactionCompletion() method
> and TransactionOutcome class.
>
> Based on the example in Examples.transaction and the javadoc it looks
> like an transaction should be started when transactionCompletion() is
> called, this returns an TransactionOutcome object that can later be
> used to complete the transaction.
>
> Questions:
> 1) Why isn't the transactionCompletion() method an operation on the
> same format as the other methods, starting an transaction requires
> network communication for postgresql and there is need of an mechanism
> to handle errors.
> 2) Is it required that it's the same TransactionOutcome object that is
> returned by starting the transaction that is used to commit/rollback
> it?
> 3) What should happen if transactionCompletion() is called multiple
> times without an commit/rollback, should the same object be returned
> or is it allowed to return new ones?
>
> best regards
> Alexander Kjäll
More information about the jdbc-spec-discuss
mailing list