|
| 1 | +/* |
| 2 | + * Copyright 2025 Google LLC |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.google.cloud.spannerlib; |
| 18 | + |
| 19 | +import static com.google.cloud.spannerlib.internal.SpannerLibrary.executeAndRelease; |
| 20 | + |
| 21 | +import com.google.cloud.spannerlib.internal.GoString; |
| 22 | +import com.google.cloud.spannerlib.internal.MessageHandler; |
| 23 | +import com.google.cloud.spannerlib.internal.SpannerLibrary; |
| 24 | + |
| 25 | +/** |
| 26 | + * A {@link Pool} that has been created by SpannerLib. A {@link Pool} can create any number of |
| 27 | + * {@link Connection} instances. All {@link Connection} instances share the same underlying Spanner |
| 28 | + * client. |
| 29 | + */ |
| 30 | +public class Pool extends AbstractLibraryObject { |
| 31 | + |
| 32 | + /** Creates a new {@link Pool} using the given connection string. */ |
| 33 | + public static Pool createPool(String connectionString) { |
| 34 | + SpannerLibrary library = SpannerLibrary.getInstance(); |
| 35 | + try (MessageHandler message = |
| 36 | + library.execute(lib -> lib.CreatePool(new GoString(connectionString)))) { |
| 37 | + return new Pool(library, message.getObjectId()); |
| 38 | + } |
| 39 | + } |
| 40 | + |
| 41 | + private Pool(SpannerLibrary library, long id) { |
| 42 | + super(library, id); |
| 43 | + } |
| 44 | + |
| 45 | + @Override |
| 46 | + public void close() { |
| 47 | + executeAndRelease(getLibrary(), library -> library.ClosePool(getId())); |
| 48 | + } |
| 49 | + |
| 50 | + /** Creates a new {@link Connection} in this {@link Pool}. */ |
| 51 | + public Connection createConnection() { |
| 52 | + try (MessageHandler message = |
| 53 | + getLibrary().execute(library -> library.CreateConnection(getId()))) { |
| 54 | + return new Connection(this, message.getObjectId()); |
| 55 | + } |
| 56 | + } |
| 57 | +} |
0 commit comments