Connection pool with Google App Engine and Google

2019-08-19 02:45发布

There are numerous questions about using a db connection pool with Google App Engine, but a lot has changed recently. Up to this point, I could never get a connection pool to work with GAE. However, I think some recent develops may allow connection pooling to work, which may be why it is mentioned in the Google documentation (which seems to have recently been updated).

https://cloud.google.com/sql/docs/mysql/connect-app-engine

Can someone confirm that connection pools can be used?

1) We used Google Cloud SQL 1st gen and the database could deactivate (go to sleep). This would make any existing connections stale.

With a 2nd gen database, there is no deactivtion of databases. So this may address the problem.

2) Many connection pool implementations used threads.

With Java 8 being supported on GAE, it looks like threads are permitted.

3) Some people suggest that GAE's limited number of database connections (12) are a reason to use connection pools. The connection pool size could be set to GAE's limit and thus an app would never exceed the limit.

a) First, documentation indicates a much larger number of connections, based on the size of the database.

https://cloud.google.com/sql/docs/quotas

b) Second, if there is a limit for a GAE app, is the limit per individual server instance or for an entire GAE app?

Any confirmation that the above thinking makes sense would be appreciated.

1条回答
一夜七次
2楼-- · 2019-08-19 03:15

Regarding 1) Yes, the Cloud SQL instances of 2nd generation, your instances don't deactivate unless it's for maintenance etc.

2) I don't see why you can't use threads to connect to a 2nd generation Cloud SQL database. With Java 8, you can absolutely do that. To check how many threads you have open, you can run mysql> SHOW STATUS WHERE Variable_name = 'Threads_connected';

For 3a), I would go with the official documentation link that you provided already but remember that database connections consume resources on the server and the connecting application. Always use good connection management practices to minimize your application's footprint and reduce the likelihood of exceeding Cloud SQL connection limits. The limit of 12 connections was indeed in place in the past but it doesn't exist anymore.

3b) When a limit or quota refers to a Google App Engine app, then it's for the whole app unless it's specified that it's per instance. More specifically for Cloud SQL connections, you can find the limits here and there is actually a limit that is specific to instances. You can't have more than 100 concurrent connections for each App Engine instance running in a standard environment.

I hope that helps!

查看更多
登录 后发表回答