19.1.9. camcops_server.cc_modules.cc_config


Copyright (C) 2012-2018 Rudolf Cardinal (rudolf@pobox.com).

This file is part of CamCOPS.

CamCOPS is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.

CamCOPS is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with CamCOPS. If not, see <http://www.gnu.org/licenses/>.


class camcops_server.cc_modules.cc_config.CamcopsConfig(config_filename: str)[source]

Class representing the config.

get_sqla_engine() → sqlalchemy.engine.base.Engine[source]

I was previously misinterpreting the appropriate scope of an Engine. I thought: create one per request. But the Engine represents the connection pool. So if you create them all the time, you get e.g. a ‘Too many connections’ error.

“The appropriate scope is once per [database] URL per application, at the module level.”

https://groups.google.com/forum/#!topic/sqlalchemy/ZtCo2DsHhS4 https://stackoverflow.com/questions/8645250/how-to-close-sqlalchemy-connection-in-mysql

Now, our CamcopsConfig instance is cached, so there should be one of them overall. See get_config() below.

Therefore, making the engine a member of this class should do the trick, whilst avoiding global variables.