Skip to content

Commit af05cf5

Browse files
docs: update README with connection pooling (#196)
* update README with connection pooling sample
1 parent 0bda4df commit af05cf5

File tree

1 file changed

+26
-27
lines changed

1 file changed

+26
-27
lines changed

README.md

Lines changed: 26 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -45,37 +45,15 @@ pip install git+https://github.com/GoogleCloudPlatform/cloud-sql-python-connecto
4545

4646
### How to use this connector
4747

48-
To use the connector: import the connector by including the following statement at the top of your Python file:
48+
To use the connector: import the connector and SQLAlchemy by including the following statements at the top of your Python file:
4949
```Python
5050
from google.cloud.sql.connector import connector
51+
import sqlalchemy
5152
```
5253

53-
Use the connector to create a connection object by calling the connect method. 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.
54+
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.
5455

55-
```
56-
conn = connector.connect(
57-
"project:region:instance",
58-
"pymysql",
59-
user="root",
60-
password="shhh",
61-
db="your-db-name"
62-
... insert other kwargs ...
63-
)
64-
```
65-
66-
The returned DB-API 2.0 compliant connection object can then be used to query and modify the database:
67-
```
68-
# Execute a query
69-
cursor = conn.cursor()
70-
cursor.execute("SELECT * from my_table")
71-
72-
# Fetch the results
73-
result = cursor.fetchall()
74-
75-
# Do something with the results
76-
for row in result:
77-
print(row)
78-
```
56+
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.
7957

8058
To use this connector with SQLAlchemy, use the `creator` argument for `sqlalchemy.create_engine`:
8159
```
@@ -89,12 +67,33 @@ def getconn() -> pymysql.connections.Connection:
8967
)
9068
return conn
9169
92-
engine = sqlalchemy.create_engine(
70+
pool = sqlalchemy.create_engine(
9371
"mysql+pymysql://",
9472
creator=getconn,
9573
)
9674
```
9775

76+
The returned connection pool engine can then be used to query and modify the database.
77+
```
78+
# insert statement
79+
insert_stmt = sqlalchemy.text(
80+
"INSERT INTO my_table (id, title) VALUES (:id, :title)",
81+
)
82+
83+
with pool.connect() as db_conn:
84+
# insert into database
85+
db_conn.execute(insert_stmt, id="book1", title="Book One")
86+
87+
# query database
88+
result = db_conn.execute("SELECT * from my_table").fetchall()
89+
90+
# Do something with the results
91+
for row in result:
92+
print(row)
93+
```
94+
95+
**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).
96+
9897
**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).
9998

10099
### Specifying Public or Private IP

0 commit comments

Comments
 (0)