php小編草莓為大家帶來了一個關於Google Cloud Spanner錯誤的解決方案。使用Google Cloud Spanner時,有時會遇到「會話池無效」的錯誤。這個錯誤通常是由於連接池配置不正確或超出了限制導致的。在本文中,我們將詳細介紹如何辨識和解決這個問題,讓你的應用程式順利使用Google Cloud Spanner服務。
我有以下使用 Spanner 函式庫的 Go 程式碼:
package main import ( "log/slog" "log" "fmt" "context" "cloud.google.com/go/spanner" ) func createClient(ctx context.Context, db string) (*spanner.Client, error) { sessionPoolConfig := spanner.SessionPoolConfig{ TrackSessionHandles: true, InactiveTransactionRemovalOptions: spanner.InactiveTransactionRemovalOptions{ ActionOnInactiveTransaction: spanner.WarnAndClose, }, } dataClient, err := spanner.NewClientWithConfig( ctx, db, spanner.ClientConfig{SessionPoolConfig: sessionPoolConfig}, ) if err != nil { slog.Error("failed to create the spanner client", "err", err) return nil, err } defer dataClient.Close() _ = dataClient return dataClient, nil } var dbLink = "projects/PROJECT_NAME/instances/INSTANCE_NAME/databases/DATABASE_NAME" func main() { ctx := context.Background() snapperClient, err := createClient(ctx, dbLink) if err != nil { log.Fatalf("failed to create a spanner client, error=%s", err.Error()) } row, err := spannerClient.Single().ReadRow(ctx, "DATA_TABLE", spanner.Key{THE_KEY}, []string{"THE_COLUMN"}) if err != nil { slog.Error("failed to get the DATA from spanner", "error", err.Error()) return nil, err } ... }
這會導致以下錯誤日誌:
2023/11/30 22:40:03 ERROR failed to get the DATA from spanner id=4 error="spanner: code = \"InvalidArgument\", desc = \"invalid session pool\""
我不相信該伺服器是否能夠存取 Google Cloud Spanner 指定的資料庫,但我沒有收到 Spanner 用戶端建立錯誤。如何修復此 invalid session pool
錯誤?
您在返回之前關閉 createClient
內部的客戶端。刪除 defer dataClient.Close()
它應該按您的預期工作。
func createClient(ctx context.Context, db string) (*spanner.Client, error) { sessionPoolConfig := spanner.SessionPoolConfig{ TrackSessionHandles: true, InactiveTransactionRemovalOptions: spanner.InactiveTransactionRemovalOptions{ ActionOnInactiveTransaction: spanner.WarnAndClose, }, } dataClient, err := spanner.NewClientWithConfig( ctx, db, spanner.ClientConfig{SessionPoolConfig: sessionPoolConfig}, ) if err != nil { slog.Error("failed to create the spanner client", "err", err) return nil, err } // Remove this defer dataClient.Close() // Also this isn't doing anything _ = dataClient return dataClient, nil }
以上是Google Cloud Spanner 錯誤:會話池無效的詳細內容。更多資訊請關注PHP中文網其他相關文章!