Search code examples
google-cloud-platformconnection-poolinggoogle-cloud-spanner

How to create Connection Pooling in Cloud Spanner for the Enterprise applications


I'm pretty new to Cloud Spanenr. I had understood hoe to create connection(dbClient) and use to for transactions and executeQuery/mutations other. But i'm not clear with how to create the ConnectionPooling for Cloud Spanner well there is SpannerOptions, Session/SessionPoolOptions. By just creating

 SpannerOptions options = SpannerOptions.newBuilder().build();
          Spanner spanner = options.getService();
DatabaseClient dbClient = spanner.getDatabaseClient(db);

will handle the connectionPooling required for Enterprise applications.

Is there a proper way to create the JDBC/Hibernate style Connection pooling were we take the connection object form Pool and return to pool for reuse again instead of creating connections multiple time.

I had done creating the connection object and create all type transactions with queries and mutations and other process. I'm in level of creating an Centralized library to provice the connection(dbClient) for multiple dao's. Unable to get proper documentation or process to achieve the JDBC/Hibernate style of Connection Pooling class for Cloud Spanner

 SpannerOptions options = SpannerOptions.newBuilder().build();
          Spanner spanner = options.getService();
            try {
              DatabaseId db = DatabaseId.of(options.getProjectId(),SpannerInstanceId, SpannerDatabaseId);

              String clientProject = spanner.getOptions().getProjectId();
              if (!db.getInstanceId().getProject().equals(clientProject)) {
                System.err.println(
                    "Invalid project specified. Project in the database id should match"
                        + "the project name set in the environment variable GCLOUD_PROJECT. Expected: "
                        + clientProject);
              }
              DatabaseClient dbClient = spanner.getDatabaseClient(db);
              dbClient
              .readWriteTransaction()
              .run(
                  new TransactionCallable<Void>() {
                    @Override
                    public Void run(TransactionContext transaction) throws Exception {
                        ResultSet resultSet;

Currently i'm trying to repeat the same for each Dao mathod when connection is required. I'm looking to create a centralized class for providing this connection form connection pool and return it for the pool for resue.


Solution

  • In Cloud Spanner a long-lived "connection"/"communication channel" with the database is modeled by a "Session" and not a DatabaseClient object. The DatabaseClient object already implements connection(session) pooling internally in a SessionPool object which can be configured via SessionPoolOptions.

    If you want to manage your own sessions, instead of giving a DatabaseClient object (or a class wrapping it) to each of your DAO objects, you could initialize your own SessionPool and give them each a Session object (or a class wrapping it).