Skip to content

Commit 4a543dc

Browse files
feat!: deprecate default connect method (#316)
1 parent de7838f commit 4a543dc

File tree

2 files changed

+29
-32
lines changed

2 files changed

+29
-32
lines changed

README.md

Lines changed: 22 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -47,20 +47,24 @@ Conversely, install straight from Github using `pip`:
4747
pip install git+https://github.com/GoogleCloudPlatform/cloud-sql-python-connector
4848
```
4949

50-
### How to use this connector
50+
### How to use this Connector
5151

52-
To use the connector: import the connector and SQLAlchemy by including the following statements at the top of your Python file:
53-
```Python
54-
from google.cloud.sql.connector import connector
55-
import sqlalchemy
56-
```
52+
To connect to Cloud SQL using the connector, inititalize a `Connector`
53+
object and call it's `connect` method with the proper input parameters.
5754

58-
The connector itself creates connection objects by calling its `connect` method but does not manage database connection pooling. For this reason, it is recommended to use the connector alongside a library that can create connection pools, such as [SQLAlchemy](https://www.sqlalchemy.org/). This will allow for connections to remain open and be reused, reducing connection overhead and the number of connections needed.
55+
The `Connector` itself creates connection objects by calling its `connect` method but does not manage database connection pooling. For this reason, it is recommended to use the connector alongside a library that can create connection pools, such as [SQLAlchemy](https://www.sqlalchemy.org/). This will allow for connections to remain open and be reused, reducing connection overhead and the number of connections needed.
5956

60-
In the connector's `connect` method below, input your connection string as the first positional argument and the name of the database driver for the second positional argument. Insert the rest of your connection keyword arguments like user, password and database. You can also set the optional `timeout` or `ip_type` keyword arguments.
57+
In the Connector's `connect` method below, input your connection string as the first positional argument and the name of the database driver for the second positional argument. Insert the rest of your connection keyword arguments like user, password and database. You can also set the optional `timeout` or `ip_type` keyword arguments.
6158

6259
To use this connector with SQLAlchemy, use the `creator` argument for `sqlalchemy.create_engine`:
6360
```python
61+
from google.cloud.sql.connector import Connector
62+
import sqlalchemy
63+
64+
# initialize Connector object
65+
connector = Connector()
66+
67+
# function to return the database connection
6468
def getconn() -> pymysql.connections.Connection:
6569
conn: pymysql.connections.Connection = connector.connect(
6670
"project:region:instance",
@@ -71,6 +75,7 @@ def getconn() -> pymysql.connections.Connection:
7175
)
7276
return conn
7377

78+
# create connection pool
7479
pool = sqlalchemy.create_engine(
7580
"mysql+pymysql://",
7681
creator=getconn,
@@ -96,15 +101,21 @@ with pool.connect() as db_conn:
96101
print(row)
97102
```
98103

104+
To close the `Connector` object's background resources, call it's `close()` method as follows:
105+
106+
```python
107+
connector.close()
108+
```
109+
99110
**Note**: For more examples of using SQLAlchemy to manage connection pooling with the connector, please see [Cloud SQL SQLAlchemy Samples](https://cloud.google.com/sql/docs/postgres/connect-connectors#python_1).
100111

101112
**Note for SQL Server users**: If your SQL Server instance requires SSL, you need to download the CA certificate for your instance and include `cafile={path to downloaded certificate}` and `validate_host=False`. This is a workaround for a [known issue](https://issuetracker.google.com/184867147).
102113

103-
### Custom Connector Object
114+
### Configuring the Connector
104115

105116
If you need to customize something about the connector, or want to specify
106-
defaults for each connection to make, you can initialize a custom
107-
`Connector` object directly:
117+
defaults for each connection to make, you can initialize a
118+
`Connector` object as follows:
108119

109120
```python
110121
from google.cloud.sql.connector import Connector, IPTypes
@@ -118,27 +129,6 @@ connector = Connector(
118129
)
119130
```
120131

121-
You can then call the Connector object's `connect` method as you
122-
would the default `connector.connect`:
123-
124-
```python
125-
def getconn() -> pymysql.connections.Connection:
126-
conn = connector.connect(
127-
"project:region:instance",
128-
"pymysql",
129-
user="root",
130-
password="shhh",
131-
db="your-db-name"
132-
)
133-
return conn
134-
```
135-
136-
To close the `Connector` object's background resources, call it's `close()` method as follows:
137-
138-
```python
139-
connector.close()
140-
```
141-
142132
### Using Connector as a Context Manager
143133

144134
The `Connector` object can also be used as a context manager in order to

google/cloud/sql/connector/connector.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,13 @@ def connect(instance_connection_string: str, driver: str, **kwargs: Any) -> Any:
263263
:returns:
264264
A DB-API connection to the specified Cloud SQL instance.
265265
"""
266+
# deprecation warning
267+
logger.warning(
268+
"The global `connect` method is deprecated and may be removed in a later "
269+
"version. Please initialize a `Connector` object and call it's `connect` "
270+
"method directly. \n"
271+
"See https://github.com/GoogleCloudPlatform/cloud-sql-python-connector/blob/main/README.md#how-to-use-this-connector for examples.",
272+
)
266273
global _default_connector
267274
if _default_connector is None:
268275
_default_connector = Connector()

0 commit comments

Comments
 (0)