Home  >  Article  >  Backend Development  >  Google Cloud Spanner error: Invalid session pool

Google Cloud Spanner error: Invalid session pool

王林
王林forward
2024-02-08 22:36:09808browse

Google Cloud Spanner 错误:会话池无效

php editor Strawberry brings you a solution to the Google Cloud Spanner error. When using Google Cloud Spanner, you sometimes encounter an "Invalid session pool" error. This error is usually caused by incorrect configuration of the connection pool or exceeding the limit. In this article, we will detail how to identify and resolve this issue so that your application can successfully use the Google Cloud Spanner service.

Question content

I have the following Go code using the Spanner library:

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
}
...
}

This results in the following error log:

2023/11/30 22:40:03 ERROR failed to get the DATA from spanner id=4 error="spanner: code = \"InvalidArgument\", desc = \"invalid session pool\""

I don't believe that the server is able to access the database specified by Google Cloud Spanner, but I don't get the Spanner client creation error. How to fix this invalid session pool error?

Workaround

You close the client inside createClient before returning. Remove defer dataClient.Close() and it should work as you expect.

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
}

The above is the detailed content of Google Cloud Spanner error: Invalid session pool. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:stackoverflow.com. If there is any infringement, please contact admin@php.cn delete