我有一个使用flask_sqlalchemy的Flask应用程序:
from flask import Flask
from flask_sqlalchemy import SQLAlchemy
app = Flask(__name__)
app.config.from_pyfile(filename='settings.py', silent=True)
db = SQLAlchemy(app=app)
我想从守护进程连接到同一个数据库 . 在守护进程中,我只导入db并使用 db.engine.execute
进行SQLAlchemy查询 .
但是当守护进程启动时主应用程序无法连接到数据库 . 在日志中我看到:
fdb.fbcore.DatabaseError: ('Error while connecting to database:\n- SQLCODE:
-902\n- I/O error during "lock" operation for file "main.fdb"\n- Database
already opened with engine instance, incompatible with current', -902,
335544344)
我尝试使用隔离级别:
from fdb.fbcore import ISOLATION_LEVEL_READ_COMMITED_LEGACY
class TPBAlchemy(SQLAlchemy):
def apply_driver_hacks(self, app_, info, options):
if 'isolation_level' not in options:
options['isolation_level'] = ISOLATION_LEVEL_READ_COMMITED_LEGACY
return super(TPBAlchemy, self).apply_driver_hacks(app_, info, options)
并替换这个:
db = SQLAlchemy()
至:
db = TPBAlchemy()
但这只会产生另一个错误:
TypeError: Invalid argument(s) 'isolation_level' sent to create_engine(),
using configuration FBDialect_fdb/QueuePool/Engine. Please check that the
keyword arguments are appropriate for this combination of components.
我很感激能够解决我的问题的完整例子 .
1 回答
您的连接字符串用于嵌入式数据库 . 您只能一次与嵌入式数据库 Build 一个“连接” .
如果启用了
Loopback
提供程序,则可以将连接字符串更改为:或者如果您启用了
Remote
提供程序,则必须从另一个远程节点访问您的数据库,并假设您的Firebird服务器位于192.168.1.100
,请将您的连接字符串更改为如果您打算使用
Engine12
提供程序(嵌入式提供程序),则必须停止已连接到该数据库的任何内容,因为您无法同时使用此提供程序执行两个用户 .此外,尝试设置一些数据库别名,这样您就不会明确指定数据库 . 在Firebird 3.0.3中查看databases.conf,您可以在其中执行以下操作:
并且您的连接字符串现在将是
mydatabasealias
而不是整个路径 .