Documentation ¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
var ErrAbortedDueToConcurrentModification = status.Error(codes.Aborted, "Transaction was aborted due to a concurrent modification")
ErrAbortedDueToConcurrentModification is returned by a read/write transaction that was aborted by Cloud Spanner, and where the internal retry attempt failed because it detected that the results during the retry were different from the initial attempt.
Use the RunTransaction function to execute a read/write transaction in a retry loop. This function will never return ErrAbortedDueToConcurrentModification.
Functions ¶
func RunTransaction ¶ added in v1.8.0
func RunTransaction(ctx context.Context, db *sql.DB, opts *sql.TxOptions, f func(ctx context.Context, tx *sql.Tx) error) error
RunTransaction runs the given function in a transaction on the given database. If the connection is a connection to a Spanner database, the transaction will automatically be retried if the transaction is aborted by Spanner. Any other errors will be propagated to the caller and the transaction will be rolled back. The transaction will be committed if the supplied function did not return an error.
If the connection is to a non-Spanner database, no retries will be attempted, and any error that occurs during the transaction will be propagated to the caller.
The application should *NOT* call tx.Commit() or tx.Rollback(). This is done automatically by this function, depending on whether the transaction function returned an error or not.
This function will never return ErrAbortedDueToConcurrentModification.
func WithDisableRetryAborts ¶ added in v1.9.0
func WithDisableRetryAborts(level sql.IsolationLevel) sql.IsolationLevel
WithDisableRetryAborts returns a specific Spanner isolation level that contains both the given standard isolation level and a custom Spanner isolation level that disables internal retries for aborted transactions for a single transaction.
Types ¶
type AutocommitDMLMode ¶
type AutocommitDMLMode int
AutocommitDMLMode indicates whether a single DML statement should be executed in a normal atomic transaction or as a Partitioned DML statement. See https://cloud.google.com/spanner/docs/dml-partitioned for more information.
const ( Transactional AutocommitDMLMode = iota PartitionedNonAtomic )
func (AutocommitDMLMode) String ¶
func (mode AutocommitDMLMode) String() string
type Driver ¶
type Driver struct {
// contains filtered or unexported fields
}
Driver represents a Google Cloud Spanner database/sql driver.
type SpannerConn ¶
type SpannerConn interface { // StartBatchDDL starts a DDL batch on the connection. After calling this // method all subsequent DDL statements will be cached locally. Calling // RunBatch will send all cached DDL statements to Spanner as one batch. // Use DDL batching to speed up the execution of multiple DDL statements. // Note that a DDL batch is not atomic. It is possible that some DDL // statements are executed successfully and some not. // See https://cloud.google.com/spanner/docs/schema-updates#order_of_execution_of_statements_in_batches // for more information on how Cloud Spanner handles DDL batches. StartBatchDDL() error // StartBatchDML starts a DML batch on the connection. After calling this // method all subsequent DML statements will be cached locally. Calling // RunBatch will send all cached DML statements to Spanner as one batch. // Use DML batching to speed up the execution of multiple DML statements. // DML batches can be executed both outside of a transaction and during // a read/write transaction. If a DML batch is executed outside an active // transaction, the batch will be applied atomically to the database if // successful and rolled back if one or more of the statements fail. // If a DML batch is executed as part of a transaction, the error will // be returned to the application, and the application can decide whether // to commit or rollback the transaction. StartBatchDML() error // RunBatch sends all batched DDL or DML statements to Spanner. This is a // no-op if no statements have been batched or if there is no active batch. RunBatch(ctx context.Context) error // AbortBatch aborts the current DDL or DML batch and discards all batched // statements. AbortBatch() error // InDDLBatch returns true if the connection is currently in a DDL batch. InDDLBatch() bool // InDMLBatch returns true if the connection is currently in a DML batch. InDMLBatch() bool // GetBatchedStatements returns a copy of the statements that are currently // buffered to be executed as a DML or DDL batch. It returns an empty slice // if no batch is active, or if there are no statements buffered. GetBatchedStatements() []spanner.Statement // RetryAbortsInternally returns true if the connection automatically // retries all aborted transactions. RetryAbortsInternally() bool // SetRetryAbortsInternally enables/disables the automatic retry of aborted // transactions. If disabled, any aborted error from a transaction will be // propagated to the application. SetRetryAbortsInternally(retry bool) error // AutocommitDMLMode returns the current mode that is used for DML // statements outside a transaction. The default is Transactional. AutocommitDMLMode() AutocommitDMLMode // SetAutocommitDMLMode sets the mode to use for DML statements that are // executed outside transactions. The default is Transactional. Change to // PartitionedNonAtomic to use Partitioned DML instead of Transactional DML. // See https://cloud.google.com/spanner/docs/dml-partitioned for more // information on Partitioned DML. SetAutocommitDMLMode(mode AutocommitDMLMode) error // ReadOnlyStaleness returns the current staleness that is used for // queries in autocommit mode, and for read-only transactions. ReadOnlyStaleness() spanner.TimestampBound // SetReadOnlyStaleness sets the staleness to use for queries in autocommit // mode and for read-only transaction. SetReadOnlyStaleness(staleness spanner.TimestampBound) error // ExcludeTxnFromChangeStreams returns true if the next transaction should be excluded from change streams with the // DDL option `allow_txn_exclusion=true`. ExcludeTxnFromChangeStreams() bool // SetExcludeTxnFromChangeStreams sets whether the next transaction should be excluded from change streams with the // DDL option `allow_txn_exclusion=true`. SetExcludeTxnFromChangeStreams(excludeTxnFromChangeStreams bool) error // Apply writes an array of mutations to the database. This method may only be called while the connection // is outside a transaction. Use BufferWrite to write mutations in a transaction. // See also spanner.Client#Apply Apply(ctx context.Context, ms []*spanner.Mutation, opts ...spanner.ApplyOption) (commitTimestamp time.Time, err error) // BufferWrite writes an array of mutations to the current transaction. This method may only be called while the // connection is in a read/write transaction. Use Apply to write mutations outside a transaction. // See also spanner.ReadWriteTransaction#BufferWrite BufferWrite(ms []*spanner.Mutation) error // CommitTimestamp returns the commit timestamp of the last implicit or explicit read/write transaction that // was executed on the connection, or an error if the connection has not executed a read/write transaction // that committed successfully. The timestamp is in the local timezone. CommitTimestamp() (commitTimestamp time.Time, err error) // UnderlyingClient returns the underlying Spanner client for the database. // The client cannot be used to access the current transaction or batch on // this connection. Executing a transaction or batch using the client that is // returned, does not affect this connection. // Note that multiple connections share the same Spanner client. Calling // this function on different connections to the same database, can // return the same Spanner client. UnderlyingClient() (client *spanner.Client, err error) // contains filtered or unexported methods }
SpannerConn is the public interface for the raw Spanner connection for the sql driver. This interface can be used with the db.Conn().Raw() method.