dbconn

package
v0.0.0-...-f9f8287 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 3, 2024 License: Apache-2.0 Imports: 17 Imported by: 0

Documentation

Overview

Package dbconn contains a series of database-related utility functions.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func BeginStandardTrx

func BeginStandardTrx(ctx context.Context, db *sql.DB, opts *sql.TxOptions) (*sql.Tx, int, error)

BeginStandardTrx is like db.BeginTx but returns the connection id.

func Exec

func Exec(ctx context.Context, db *sql.DB, stmt string, args ...interface{}) error

Exec is like db.Exec but only returns an error. This makes it a little bit easier to use in error handling. It accepts args which are escaped client side using the TiDB escape library. i.e. %n is an identifier, %? is automatic type conversion on a variable.

func IsRDSHost

func IsRDSHost(host string) bool

func New

func New(inputDSN string, config *DBConfig) (*sql.DB, error)

New is similar to sql.Open except we take the inputDSN and append additional options to it to standardize the connection. It will also ping the connection to ensure it is valid.

func NewTLSConfig

func NewTLSConfig() *tls.Config

func RetryableTransaction

func RetryableTransaction(ctx context.Context, db *sql.DB, ignoreDupKeyWarnings bool, config *DBConfig, stmts ...string) (int64, error)

RetryableTransaction retries all statements in a transaction, retrying if a statement errors, or there is a deadlock. It will retry up to maxRetries times.

Types

type DBConfig

type DBConfig struct {
	LockWaitTimeout          int
	InnodbLockWaitTimeout    int
	MaxRetries               int
	MaxOpenConnections       int
	RangeOptimizerMaxMemSize int64
}

func NewDBConfig

func NewDBConfig() *DBConfig

type TableLock

type TableLock struct {
	// contains filtered or unexported fields
}

func NewTableLock

func NewTableLock(ctx context.Context, db *sql.DB, table *table.TableInfo, writeLock bool, config *DBConfig, logger loggers.Advanced) (*TableLock, error)

NewTableLock creates a new server wide lock on a table. i.e. LOCK TABLES .. READ. It uses a short-timeout with backoff and retry, since if there is a long-running process that currently prevents the lock by being acquired, it is considered "nice" to let a few short-running processes slip in and proceed, then optimistically try and acquire the lock again.

func (*TableLock) Close

func (s *TableLock) Close() error

Close closes the table lock

func (*TableLock) ExecUnderLock

func (s *TableLock) ExecUnderLock(ctx context.Context, stmts []string) error

ExecUnderLock executes a set of statements under a table lock.

type TrxPool

type TrxPool struct {
	sync.Mutex
	// contains filtered or unexported fields
}

func NewTrxPool

func NewTrxPool(ctx context.Context, db *sql.DB, count int, config *DBConfig) (*TrxPool, error)

NewTrxPool creates a pool of transactions which have already had their read-view created in REPEATABLE READ isolation.

func (*TrxPool) Close

func (p *TrxPool) Close() error

Close closes all transactions in the pool.

func (*TrxPool) Get

func (p *TrxPool) Get() (*sql.Tx, error)

Get gets a transaction from the pool.

func (*TrxPool) Put

func (p *TrxPool) Put(trx *sql.Tx)

Put puts a transaction back in the pool.

Directories

Path Synopsis
Package sqlescape provides SQL escaping functionality.
Package sqlescape provides SQL escaping functionality.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL