| 70 | } |
| 71 | |
| 72 | func NewSession(projectId string, instanceId string, databaseId string, priority pb.RequestOptions_Priority, role string, directedRead *pb.DirectedReadOptions, |
| 73 | protoDescriptor []byte, opts ...option.ClientOption) (*Session, error) { |
| 74 | ctx := context.Background() |
| 75 | dbPath := fmt.Sprintf("projects/%s/instances/%s/databases/%s", projectId, instanceId, databaseId) |
| 76 | clientConfig := defaultClientConfig |
| 77 | clientConfig.DatabaseRole = role |
| 78 | clientConfig.DirectedReadOptions = directedRead |
| 79 | clientConfig.DisableNativeMetrics = true |
| 80 | opts = append(opts, defaultClientOpts...) |
| 81 | client, err := spanner.NewClientWithConfig(ctx, dbPath, clientConfig, opts...) |
| 82 | if err != nil { |
| 83 | return nil, err |
| 84 | } |
| 85 | |
| 86 | adminClient, err := adminapi.NewDatabaseAdminClient(ctx, opts...) |
| 87 | if err != nil { |
| 88 | return nil, err |
| 89 | } |
| 90 | |
| 91 | if priority == pb.RequestOptions_PRIORITY_UNSPECIFIED { |
| 92 | priority = defaultPriority |
| 93 | } |
| 94 | |
| 95 | session := &Session{ |
| 96 | projectId: projectId, |
| 97 | instanceId: instanceId, |
| 98 | databaseId: databaseId, |
| 99 | client: client, |
| 100 | clientConfig: clientConfig, |
| 101 | clientOpts: opts, |
| 102 | adminClient: adminClient, |
| 103 | defaultPriority: priority, |
| 104 | directedRead: directedRead, |
| 105 | protoDescriptor: protoDescriptor, |
| 106 | } |
| 107 | go session.startHeartbeat() |
| 108 | |
| 109 | return session, nil |
| 110 | } |
| 111 | |
| 112 | // InReadWriteTransaction returns true if the session is running read-write transaction. |
| 113 | func (s *Session) InReadWriteTransaction() bool { |