Google Cloud Spanner error: Invalid session pool

王林
Release: 2024-02-08 22:36:09
forward
811 people have browsed it

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
}
...
}
Copy after login

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\""
Copy after login

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
}
Copy after login

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!

source:stackoverflow.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!