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 is resulting 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 am not convinced that this server is even reaching the Google Cloud Spanner specified database, but I didn't get a Spanner client creation error. How can I fix this invalid session pool
error?
You're closing the client inside of createClient
before returning it. 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
}