MCPcopy Create free account
hub / github.com/cloudspannerecosystem/spanner-cli / BeginReadOnlyTransaction

Method BeginReadOnlyTransaction

session.go:180–216  ·  view source on GitHub ↗

BeginReadOnlyTransaction starts read-only transaction and returns the snapshot timestamp for the transaction if successful.

(ctx context.Context, typ timestampBoundType, staleness time.Duration, timestamp time.Time, priority pb.RequestOptions_Priority, tag string)

Source from the content-addressed store, hash-verified

178
179// BeginReadOnlyTransaction starts read-only transaction and returns the snapshot timestamp for the transaction if successful.
180func (s *Session) BeginReadOnlyTransaction(ctx context.Context, typ timestampBoundType, staleness time.Duration, timestamp time.Time, priority pb.RequestOptions_Priority, tag string) (time.Time, error) {
181 if s.InReadOnlyTransaction() {
182 return time.Time{}, errors.New("read-only transaction is already running")
183 }
184
185 txn := s.client.ReadOnlyTransaction()
186 switch typ {
187 case strong:
188 txn = txn.WithTimestampBound(spanner.StrongRead())
189 case exactStaleness:
190 txn = txn.WithTimestampBound(spanner.ExactStaleness(staleness))
191 case readTimestamp:
192 txn = txn.WithTimestampBound(spanner.ReadTimestamp(timestamp))
193 }
194
195 // Use session's priority if transaction priority is not set.
196 if priority == pb.RequestOptions_PRIORITY_UNSPECIFIED {
197 priority = s.defaultPriority
198 }
199
200 // Because google-cloud-go/spanner defers calling BeginTransaction RPC until an actual query is run,
201 // we explicitly run a "SELECT 1" query so that we can determine the timestamp of read-only transaction.
202 opts := spanner.QueryOptions{Priority: priority}
203 if err := txn.QueryWithOptions(ctx, spanner.NewStatement("SELECT 1"), opts).Do(func(r *spanner.Row) error {
204 return nil
205 }); err != nil {
206 return time.Time{}, err
207 }
208
209 s.tc = &transactionContext{
210 tag: tag,
211 priority: priority,
212 roTxn: txn,
213 }
214
215 return txn.Timestamp()
216}
217
218// CloseReadOnlyTransaction closes a running read-only transaction.
219func (s *Session) CloseReadOnlyTransaction() error {

Callers 2

TestRequestPriorityFunction · 0.95
ExecuteMethod · 0.80

Calls 1

InReadOnlyTransactionMethod · 0.95

Tested by 1

TestRequestPriorityFunction · 0.76