Sessions, transactions, and session pinning
In a database cluster group for Aurora with routing enabled, your client holds a session on the routing endpoint. The routing layer opens a member cluster session on each member cluster that your session reaches, as your member cluster user. Each statement and each transaction runs on one member cluster, and the way that your session reaches it decides which SQL you can run and how you name your data.
Connection options
The following table compares the ways to reach a member cluster. A session on the routing endpoint moves between query routing mode and a pinned session without reconnecting.
| Connection modes | How to use it | Behavior |
|---|---|---|
| Query routing mode | The default for a connection to the routing endpoint | Each statement goes to the member cluster that holds the objects that it names, by router name. This way supports the narrowest set of SQL. |
| Pinned session | SET SESSION router_cluster_pin on a connection to the routing endpoint |
Every statement passes through to one member cluster, by cluster name. Anything that the member cluster accepts runs there. |
| Direct connection | A connection to the endpoint of a member cluster | The full SQL of that member cluster, without the routing layer. |
For when to use each way, see Connecting to the routing endpoint.
Query routing mode
In query routing mode, your application reaches every member cluster through one endpoint, by router name. Every object that one statement names must resolve to the same member cluster. Qualify each table name with its database or schema.
Router names are the only names that query routing mode accepts. A cluster name that no data mapping registers as a router name isn't found, and the statement fails before it reaches a member cluster. A statement that the routing layer answers itself, such as SELECT 1, and a read of dbclustergroup_sys.data_mappings reach no member cluster, so they don't test routing. To test routing, or the grants of a new user, query a mapped table by its router name. A permission error or a read-only error arrives when the statement runs on the member cluster, not when you connect.
The routing layer refuses some statements in query routing mode instead of routing them:
-
Data definition statements, except the
SCALEOUTstatements and the account management statements, which you run through the routing endpoint. -
A write or a read that names objects on more than one member cluster.
Run a refused statement in a pinned session or on a direct connection. For the statements that each way supports, see Statement compatibility.
The routing layer applies your session settings on each member cluster that the session reaches. On Aurora MySQL, a session that doesn't set sql_mode, time_zone, autocommit, or transaction_isolation uses the routing endpoint parameter value, or the routing layer's default when the parameter isn't set. For example, routed statements use the time zone +00:00 when neither the session nor the routing endpoint parameter sets one. The routing layer also sets the other variables in the starting-value table in Session variables the routing layer holds and forwards, and sets the connection character set variables from the collation that the client sends. Any other variable that the session doesn't set takes each member cluster's own value. Keep the parameters that the routing layer doesn't set, for example innodb_lock_wait_timeout and max_execution_time, the same on every member cluster. Set session parameters and a statement time limit at the start of the session.
The routing layer refuses a data definition statement, and a read or a write over two member clusters, with error 1235 (SQLSTATE 42000). It also refuses XA statements, and START TRANSACTION WITH CONSISTENT SNAPSHOT before a transaction binds to a member cluster. It refuses SET ROLE with error 1235 (SQLSTATE 42000) and SET DEFAULT ROLE with error 1064 (SQLSTATE 42000). For the error text, see Error codes for database cluster groups for Aurora.
A session can change its database with USE at any point, and the routing layer resolves router database names as part of routing. The metadata SHOW statements that drivers, object-relational mappers, and schema tools use return the database names that your data mappings define.
An invalid value for time_zone, and for some other session variables, fails on the SET itself.
To stop a running statement and keep the session, run KILL QUERY with the connection ID. To have the member cluster stop long statements, set max_execution_time at the start of the session.
A scaleout table has no table of its own on a member cluster. To run SHOW CREATE TABLE, SHOW COLUMNS, or SHOW INDEX on it, name one of its member tables first.
To name the member table for a metadata statement
-
At the routing endpoint, set
scaleout_tables_preferred_memberto the scaleout table and the router table name of one member table. To name several scaleout tables, separate the pairs with commas.SET scaleout_tables_preferred_member = 'orders:db.orders_s2'; -
At the routing endpoint, run
SHOW CREATE TABLE,SHOW COLUMNS, orSHOW INDEXon the scaleout table.
If the variable isn't set, or names a table that isn't a member of the scaleout table, the statement fails with error 1146.
For the variables that the routing layer holds and the statements that it answers itself, see Variables, character sets, and statements the routing layer answers.
Session pinning
Session pinning is how you reach one member cluster through the routing endpoint, without a second connection. A session pin binds one session to one member cluster. The routing layer passes every statement of a pinned session through to that member cluster unchanged, and doesn't route or refuse it.
Any statement that the member cluster accepts runs in a pinned session, including statements that query routing mode refuses:
-
DDL, such as
CREATE TABLEandALTER TABLE. For rolling out DDL this way, see Running DDL through a session pin. -
Administrative statements, such as listing and ending that member cluster's sessions.
-
Reads of the member cluster's own catalog.
-
Temporary tables, locks, and distributed transaction statements.
To pin, you name a member cluster alias from dbclustergroup_sys.member_cluster_mappings and a database on that member cluster, by its cluster name. On Aurora MySQL, you can leave out the database part. To unpin, set the pin to DEFAULT or to an empty string. Pin and unpin outside a transaction.
Pinning is an ordinary SET statement, so drivers and connection pools send it unchanged, including in a post-connect hook that accepts only the form SET . Name the target directly. The routing layer rejects a pin target that's a predicate or a routing function expression. Setting a different target moves the session to another member cluster in one statement, without reconnecting.name = value
You can leave out the database part. When you add it, the routing layer runs it as a USE of that database after the session pins. The following statements pin, pin with a database, and unpin.
SET SESSION router_cluster_pin = 'MEMBER_CLUSTER_ALIAS'; SET SESSION router_cluster_pin = 'MEMBER_CLUSTER_ALIAS@CLUSTER_DB'; SET SESSION router_cluster_pin = DEFAULT;
In a pinned session, statements such as SHOW PROCESSLIST, KILL of that member cluster's sessions, reads of information_schema and performance_schema, XA transactions, GET_LOCK, and LOCK TABLES run on the member cluster. With autocommit off, the first read or write opens a transaction, so commit or roll back before you pin or unpin.
To find pinned sessions, read dbclustergroup_sys.router_processlist in query routing mode. The pin_status column shows whether a session is pinned, and member_cluster_alias names its member cluster. For the syntax and errors, see SET router_cluster_pin.
When the pin has no database part, the pinned session keeps the name of the database that the session has selected, and the routing layer runs USE of that name on the member cluster. If no database of that name exists there, the pin fails with error 1049 and the session stays as it was. If the session has no database selected, the pinned session has none until you run USE.
Any router user can pin. The grants of your member cluster user limit what a pinned session reaches. Plan for the following behavior:
-
A pin is per session. It lasts until you unpin or the session ends. For pools and pins, see Managing connections.
-
A pin can't change inside a transaction. The routing layer also rejects a return to query routing mode while a change of pin target is in progress, and from
SET GLOBALorSET LOCALinstead ofSET SESSION. Temporary objects don't block the return. -
A pin keeps one member cluster session. Temporary tables, session variables, and other session state persist between statements, as they do on a direct connection.
-
Names are cluster names. Router names don't apply, and a scaleout table is reachable only through its member tables. The current database keeps its value when you pin or unpin, so a name that resolved through the routing layer might not resolve on the member cluster. A prepared statement that you made before you pin stays defined. If its tables are on the pinned member cluster, it runs on their physical names. If its tables are on another member cluster, running it fails with the error
Prepared statement cannot run in the current pin state. -
A write through a pin bypasses routing. Keep each row of a scaleout table's member table on the member cluster that its routing key maps to. Otherwise, the routing layer can't read the row by its key.
-
A mapping change or a move doesn't move a pinned session. The session uses a new mapping the next time it pins.
-
The group's system tables aren't reachable. Unpin to manage data mappings and scaleout tables.
-
The routing layer doesn't recover a lost member cluster connection. Your client connection fails as a direct connection would. Reconnect, then pin again.
Direct cluster access
Each member cluster keeps its own endpoints, with the full SQL of a standalone Aurora DB cluster. A direct connection runs what you send, under cluster names, without the checks of the routing layer. It's also the way to run a statement that query routing mode refuses. To change rows on two member clusters, run one transaction on each. The group doesn't make them atomic together. To read rows on two member clusters, run one query on each and combine the results in your application.
An application can write the same row through the routing endpoint and through a member cluster endpoint. Decide which path writes each table, and keep to it.
Transactions
A transaction through the routing endpoint runs on one member cluster.
Single-cluster transactions
Every transaction through the routing endpoint binds to exactly one member cluster. The routing layer chooses it from your first routable statement, not from BEGIN. The binding isn't a session pin, and you keep naming objects by router name. COMMIT, ROLLBACK, and the savepoint statements go to the bound member cluster. On Aurora MySQL, a session with autocommit=0 counts as an open transaction, as BEGIN does, so it stays on one member cluster until COMMIT or ROLLBACK.
After the transaction binds, the routing layer refuses any read or write that needs a different member cluster. Keep every statement of a transaction on one member cluster. Group the statements of each transaction by routing key, or by the database or table that a data mapping places.
The binding ends at COMMIT or ROLLBACK, so the next transaction in the session can bind to another member cluster. Within one member cluster, isolation levels and constraints behave as on a standalone cluster. A routed read goes to the writer of the member cluster that holds the rows, so it sees every write committed on that member cluster. A group gives no point-in-time view across member clusters: two reads that reach different member clusters can see a write on one and miss a later write on the other.
For how to keep transactions on one member cluster, how to combine reads from several member clusters, and how to retry, see Running queries and transactions through the routing layer. To read from a reader instance, connect to it through the endpoints of its member cluster, as Connecting to the routing endpoint describes.
Transaction rules on Aurora MySQL
The following table lists the transaction rules for Aurora MySQL. For the error text of each code, see Error codes for database cluster groups for Aurora.
| Rule | Aurora MySQL |
|---|---|
| Transaction characteristics | The routing layer refuses a bare SET TRANSACTION after BEGIN with error 1568 (SQLSTATE 25001). Issue it before BEGIN. SET SESSION TRANSACTION ISOLATION LEVEL and SET SESSION TRANSACTION READ ONLY apply on each member cluster that the session reaches. |
SAVEPOINT before the transaction binds |
The routing layer holds the savepoint and applies it when the transaction binds. |
| A statement for another member cluster after the transaction binds | Error 1235 (SQLSTATE 42000). The routing layer refuses only that statement. The transaction stays open, and a COMMIT keeps the statements that ran before it. |
A write to dbclustergroup_sys.data_mappings inside a transaction |
Error 1235 (SQLSTATE 42000). A session with autocommit=0 counts as an open transaction, and some drivers turn autocommit off by default. Run SET autocommit = 1 first. |
| A transaction that runs no routable statement | The routing layer completes it and returns OK. |
| A statement routed on an older data mapping | Inside a transaction: error 63928 (SQLSTATE 40001); the transaction is already rolled back, so retry from BEGIN. Otherwise, a statement a member cluster rejects as stale is retried automatically (text protocol only) up to 3 times, then returns 1412 (SQLSTATE HY000) with the session and any open transaction intact. If the routing layer can't confirm the rollback, or the session holds server-side prepared statements, it closes the connection instead. Reconnect and run the whole transaction again. |
| Distributed transactions on a session that isn't pinned | XA statements aren't supported. Use a pinned session or a direct connection. |
On Aurora MySQL, a refused statement doesn't end the transaction
A COMMIT after a refused statement keeps the statements that ran before it. If the transaction must apply all of its statements or none, run ROLLBACK after the refusal.
Transaction errors and retries
Inside a transaction, a statement that needs a mapping changed after the transaction's first routed statement fails with error 63928, and the routing layer rolls back the transaction. This is different from a statement that a member cluster rejects as stale outside a transaction, which the routing layer retries for you and which returns error 1412 with the session intact. For when a change reaches the routing layer, see Applying data mapping changes. For the error text, see Error codes for database cluster groups for Aurora. For the other transaction rules, see Transaction rules on Aurora MySQL.
Inside a transaction, error 63928 (SQLSTATE 40001) means the transaction is already rolled back, so retry from BEGIN. Otherwise, a statement that a member cluster rejects as stale is retried automatically (text protocol only) up to 3 times, then returns 1412 (SQLSTATE HY000) with the session and any open transaction intact. If the routing layer can't confirm the rollback, or the session holds server-side prepared statements, it closes the connection instead. Reconnect and run the whole transaction again.
On Aurora MySQL, if another session deletes the mapping of an object and doesn't create it again, a statement that names the object fails with error 1146, and the transaction stays open. Roll the transaction back instead of committing it.
When a data movement job switches routing to its target, writes to the moving objects pause briefly, and a write in that pause gets a retryable error. For the error codes, see Moving data between member clusters.
After error 2013 (lost connection) on Aurora MySQL, an autocommit write might already be committed. Retry only a write that's safe to apply twice, or check for the row first.