Skip to content

Commit fc4d692

Browse files
authored
Merge pull request google#5 from google/connection
Fix connections in Apis
2 parents 1b62326 + e4c868d commit fc4d692

2 files changed

Lines changed: 17 additions & 14 deletions

File tree

spanner_orm/admin/api.py

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@
2222
class SpannerAdminApi(api.TableReadApi):
2323
"""Manages table schema information on Spanner."""
2424

25-
_connection = None
2625
_connection_info = None
26+
_spanner_connection = None
2727

2828
@classmethod
2929
def connect(cls,
@@ -34,32 +34,35 @@ def connect(cls,
3434
create_ddl=None):
3535
"""Connects to the specified database, optionally creating tables."""
3636
connection_info = (project, instance, database, credentials)
37-
if cls._connection is not None and connection_info == cls._connection_info:
38-
return
37+
if cls._spanner_connection is not None:
38+
if connection_info == cls._connection_info:
39+
return
40+
cls.hangup()
3941

4042
client = spanner.Client(project=project, credentials=credentials)
4143
instance = client.instance(instance)
4244

4345
if create_ddl is not None:
44-
cls._connection = instance.database(database, ddl_statements=create_ddl)
46+
cls._spanner_connection = instance.database(
47+
database, ddl_statements=create_ddl)
4548
operation = cls._connection.create()
4649
operation.result()
4750
else:
48-
cls._connection = instance.database(database)
51+
cls._spanner_connection = instance.database(database)
4952

5053
cls._connection_info = connection_info
5154

5255
@classmethod
53-
def _database_connection(cls):
54-
assert cls._connection is not None
55-
return cls._connection
56+
def _connection(cls):
57+
assert cls._spanner_connection is not None
58+
return cls._spanner_connection
5659

5760
@classmethod
5861
def hangup(cls):
59-
cls._connection = None
62+
cls._spanner_connection = None
6063
cls._connection_info = None
6164

6265
@classmethod
6366
def update_schema(cls, change):
64-
operation = cls._database_connection().update_ddl([change])
67+
operation = cls._connection().update_ddl([change])
6568
operation.result()

spanner_orm/api.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,8 @@ def upsert(transaction, table_name, columns, values):
8282
class SpannerApi(SpannerReadApi, SpannerWriteApi):
8383
"""Class that handles reading from and writing to Spanner tables."""
8484

85-
_spanner_connection = None
8685
_connection_info = None
86+
_spanner_connection = None
8787

8888
@classmethod
8989
def _connection(cls):
@@ -95,17 +95,17 @@ def _connection(cls):
9595
def connect(cls, project, instance, database, credentials=None):
9696
"""Connects to the specified Spanner database."""
9797
connection_info = (project, instance, database, credentials)
98-
if cls._connection is not None:
98+
if cls._spanner_connection is not None:
9999
if connection_info == cls._connection_info:
100100
return
101101
cls.hangup()
102102

103103
client = spanner.Client(project=project, credentials=credentials)
104104
instance = client.instance(instance)
105-
cls._connection = instance.database(database)
105+
cls._spanner_connection = instance.database(database)
106106
cls._connection_info = connection_info
107107

108108
@classmethod
109109
def hangup(cls):
110-
cls._connection = None
110+
cls._spanner_connection = None
111111
cls._connection_info = None

0 commit comments

Comments
 (0)