|
16 | 16 |
|
17 | 17 | package com.example.spanner;
|
18 | 18 |
|
19 |
| -//[START spanner_create_instance] |
| 19 | +// [START spanner_create_instance] |
20 | 20 | import com.google.api.gax.longrunning.OperationFuture;
|
21 | 21 | import com.google.cloud.spanner.Instance;
|
22 | 22 | import com.google.cloud.spanner.InstanceAdminClient;
|
@@ -38,34 +38,36 @@ static void createInstance() {
|
38 | 38 | }
|
39 | 39 |
|
40 | 40 | static void createInstance(String projectId, String instanceId) {
|
41 |
| -Spanner spanner = SpannerOptions.newBuilder().setProjectId(projectId).build().getService(); |
42 |
| -InstanceAdminClient instanceAdminClient = spanner.getInstanceAdminClient(); |
| 41 | +try (Spanner spanner = |
| 42 | +SpannerOptions.newBuilder().setProjectId(projectId).build().getService()) { |
| 43 | +InstanceAdminClient instanceAdminClient = spanner.getInstanceAdminClient(); |
43 | 44 |
|
44 |
| -// Set Instance configuration. |
45 |
| -String configId = "regional-us-central1"; |
46 |
| -int nodeCount = 2; |
47 |
| -String displayName = "Descriptive name"; |
| 45 | +// Set Instance configuration. |
| 46 | +String configId = "regional-us-central1"; |
| 47 | +int nodeCount = 2; |
| 48 | +String displayName = "Descriptive name"; |
48 | 49 |
|
49 |
| -// Create an InstanceInfo object that will be used to create the instance. |
50 |
| -InstanceInfo instanceInfo = |
51 |
| -InstanceInfo.newBuilder(InstanceId.of(projectId, instanceId)) |
52 |
| -.setInstanceConfigId(InstanceConfigId.of(projectId, configId)) |
53 |
| -.setNodeCount(nodeCount) |
54 |
| -.setDisplayName(displayName) |
55 |
| -.build(); |
56 |
| -OperationFuture<Instance, CreateInstanceMetadata> operation = |
57 |
| -instanceAdminClient.createInstance(instanceInfo); |
58 |
| -try { |
59 |
| -// Wait for the createInstance operation to finish. |
60 |
| -Instance instance = operation.get(); |
61 |
| -System.out.printf("Instance %s was successfully created%n", instance.getId()); |
62 |
| -} catch (ExecutionException e) { |
63 |
| -System.out.printf( |
64 |
| -"Error: Creating instance %s failed with error message %s%n", |
65 |
| -instanceInfo.getId(), e.getMessage()); |
66 |
| -} catch (InterruptedException e) { |
67 |
| -System.out.println("Error: Waiting for createInstance operation to finish was interrupted"); |
| 50 | +// Create an InstanceInfo object that will be used to create the instance. |
| 51 | +InstanceInfo instanceInfo = |
| 52 | +InstanceInfo.newBuilder(InstanceId.of(projectId, instanceId)) |
| 53 | +.setInstanceConfigId(InstanceConfigId.of(projectId, configId)) |
| 54 | +.setNodeCount(nodeCount) |
| 55 | +.setDisplayName(displayName) |
| 56 | +.build(); |
| 57 | +OperationFuture<Instance, CreateInstanceMetadata> operation = |
| 58 | +instanceAdminClient.createInstance(instanceInfo); |
| 59 | +try { |
| 60 | +// Wait for the createInstance operation to finish. |
| 61 | +Instance instance = operation.get(); |
| 62 | +System.out.printf("Instance %s was successfully created%n", instance.getId()); |
| 63 | +} catch (ExecutionException e) { |
| 64 | +System.out.printf( |
| 65 | +"Error: Creating instance %s failed with error message %s%n", |
| 66 | +instanceInfo.getId(), e.getMessage()); |
| 67 | +} catch (InterruptedException e) { |
| 68 | +System.out.println("Error: Waiting for createInstance operation to finish was interrupted"); |
| 69 | +} |
68 | 70 | }
|
69 | 71 | }
|
70 | 72 | }
|
71 |
| -//[END spanner_create_instance] |
| 73 | +// [END spanner_create_instance] |
0 commit comments