public class TransactionAwareGate extends java.lang.Object implements SwitchGate
SwitchGate that pins the current connection at transaction boundaries.
Pinning rules (a subset applies depending on the constructor flags, to match legacy behavior):
/*@keep* / routing hint (when honorKeepHint);pinOnAutoCommitOff) — the next statement
would implicitly start a transaction;pinOnAutoCommitOff) — an unknown
state is treated like a disabled one, since the gate cannot rule out an implicit
transaction.The autocommit-off pin never blocks a switch to the TargetRole.WRITER. When control
reaches that check there is no transaction in progress yet (the in-progress check above already
returned), so autocommit-off only means the next statement would implicitly start one.
That next statement is the write being routed here, and a write cannot run on a read-only reader:
pinning it to the reader guarantees failure (e.g. MySQL --super-read-only, error 1290).
The implicit transaction physically starts when the write executes on the writer, after which
the in-progress check pins any following statements. Reads are still pinned so a read-first
implicit transaction stays put.
The legacy setReadOnly(false)-in-transaction throw is role-specific (it depends on
whether the current host is the writer) and is therefore enforced by the plugin orchestration,
which owns the role check; this gate only decides pinning.
| Constructor and Description |
|---|
TransactionAwareGate()
Classic gate: pins only while a transaction is in progress.
|
TransactionAwareGate(boolean honorKeepHint,
boolean pinOnAutoCommitOff) |
| Modifier and Type | Method and Description |
|---|---|
boolean |
canSwitch(RwSplitContext ctx,
TargetRole desired)
Returns whether a switch to
desired is allowed. |
public TransactionAwareGate()
public TransactionAwareGate(boolean honorKeepHint,
boolean pinOnAutoCommitOff)
public boolean canSwitch(RwSplitContext ctx, TargetRole desired) throws java.sql.SQLException
SwitchGatedesired is allowed. When it returns false the
current connection is pinned (no switch is performed). Implementations may throw to reject an
illegal transition (e.g. setReadOnly(false) inside an active transaction).canSwitch in interface SwitchGatectx - the read/write splitting contextdesired - the desired target roletrue if switching is allowed, false to pin the current connectionjava.sql.SQLException