-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: read-only transaction with options
Adds a function for starting a read-only transaction with Spanner options. This function takes a custom Spanner ReadOnlyTransactionOptions struct as input argument, which in the future can be extended to add any new options that Spanner might add. The transaction options that are given for the transaction are only used for this one transaction, instead of being persisted on the connection.
- Loading branch information
Showing
3 changed files
with
192 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
// Copyright 2025 Google LLC | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// https://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package main | ||
|
||
import ( | ||
"context" | ||
"database/sql" | ||
"fmt" | ||
"time" | ||
|
||
"cloud.google.com/go/spanner" | ||
spannerdriver "github.com/googleapis/go-sql-spanner" | ||
"github.com/googleapis/go-sql-spanner/examples" | ||
) | ||
|
||
// Sample showing how to execute a read-only transaction with specific options on a Spanner database. | ||
// | ||
// Execute the sample with the command `go run main.go` from this directory. | ||
func readOnlyTransactionWithOptions(projectId, instanceId, databaseId string) error { | ||
ctx := context.Background() | ||
db, err := sql.Open("spanner", fmt.Sprintf("projects/%s/instances/%s/databases/%s", projectId, instanceId, databaseId)) | ||
if err != nil { | ||
return err | ||
} | ||
defer db.Close() | ||
|
||
// Get a connection that should be used for the transaction. | ||
conn, err := db.Conn(ctx) | ||
if err != nil { | ||
return err | ||
} | ||
defer conn.Close() | ||
// Start a read-only transaction on the Spanner database using a staleness option. | ||
tx, err := spannerdriver.BeginReadOnlyTransaction( | ||
ctx, conn, spannerdriver.ReadOnlyTransactionOptions{ | ||
TimestampBound: spanner.ExactStaleness(time.Second * 10), | ||
}) | ||
if err != nil { | ||
return err | ||
} | ||
fmt.Println("Started a read-only transaction with a staleness option") | ||
|
||
// Use the read-only transaction... | ||
|
||
// Committing or rolling back a read-only transaction will not execute an actual Commit or Rollback | ||
// on the database, but it is needed in order to release the resources that are held by the read-only | ||
// transaction. | ||
if err := tx.Commit(); err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func main() { | ||
examples.RunSampleOnEmulator(readOnlyTransactionWithOptions) | ||
} |