Mysql SqlJocky的ConnectionPool是否需要关闭
我正在Dart中创建一个后端服务器应用程序,它使用MySQL数据库存储数据。为了进行SQL调用,我正在使用SqlJocky的ConnectionPool 应用程序启动时的操作:Mysql SqlJocky的ConnectionPool是否需要关闭,mysql,heroku,dart,Mysql,Heroku,Dart,我正在Dart中创建一个后端服务器应用程序,它使用MySQL数据库存储数据。为了进行SQL调用,我正在使用SqlJocky的ConnectionPool 应用程序启动时的操作: 创建存储ConnectionPool的单例 使用prepareExecute和query执行多个查询 在当地,这种方法很有效。现在我把一个开发版本推给了Heroku,几分钟后我就遇到了连接问题 所以我想知道,我是否需要关闭/释放用于执行查询的池中的单个连接?或者,查询后的连接是否再次放置在池中并可免费使用 所有MySQL
abstract class MySQLStore {
MySQLStore(ConnectionPool connectionPool) {
this._connectionPool = connectionPool;
}
ConnectionPool get connectionPool => this._connectionPool;
ConnectionPool _connectionPool;
}
getAll方法的具体实现:
Future<List<T>> getAll() async {
Completer completer = new Completer();
connectionPool.query("SELECT id, name, description FROM role").then((result) {
return result.toList();
}).then((rows) {
completer.complete(this._processRows(rows));
}).catchError((error) {
// TODO: Better error handling.
print(error);
completer.complete(null);
});
return completer.future;
}
Future getAll()异步{
Completer Completer=新的Completer();
查询(“从角色中选择id、名称、描述”)。然后((结果){
返回result.toList();
}).然后((行){
completer.complete(this._processRows(rows));
}).catchError((错误){
//TODO:更好的错误处理。
打印(错误);
completer.complete(空);
});
返回completer.future;
}
我得到的错误是:
SocketException:OS错误:连接超时,错误号=110,地址=
这并不能完全回答您的问题,但我认为您可以简化代码,如:
Future<List<T>> getAll() async {
try {
var result = await connectionPool.query(
"SELECT id, name, description FROM role");
return this._processRows(await result.toList());
} catch(error) {
// TODO: Better error handling.
print(error);
return null;
}
}
Future getAll()异步{
试一试{
var result=await connectionPool.query(
“从角色中选择id、名称、描述”);
返回此值。_processRows(wait result.toList());
}捕获(错误){
//TODO:更好的错误处理。
打印(错误);
返回null;
}
}
我确信这里不需要关闭与query
的连接。但是我不知道prepareExecute
。根据SqlJocky代码中的注释,数据库服务器释放连接可能需要相当长的时间。
也许你需要增加连接池的大小(默认值5),这样你就不会在
ConnectionPool
等待释放连接时耗尽连接。在Heroku的反馈之后,我通过实现一个计时器任务来解决这个问题,该任务每50秒执行一次基本的MySQL调用
Heroku的答复是:
Heroku的网络强制执行60-90秒的空闲超时,以防止进程失控。如果您在应用程序中使用持久连接,请确保在55秒时发送keep-alive,以防止服务器断开您打开的连接
解决方案代码:
const duration = const Duration(seconds: 50);
new Timer.periodic(duration, (Timer t) {
// Do a simple MySQL call with on the connection pool.
this.connectionPool.execute('SELECT id from role');
print('*** Keep alive triggered for MySQL heroku ***');
});
我很久没用这个了,但我认为你需要关闭它。SqlJocky如何知道在连接未关闭时何时重用连接。连接池上有关闭调用,但这将关闭所有连接:。这可能只是一种方便的方法,例如,当您离开应用程序以确保释放所有资源时。希望有更深入的知识的人能给出答案;-)我正在使用调用prepareExecute,它会返回一个结果。我注意到还有一个prepare调用,它返回一个带有close方法的查询对象。但我认为prepareExecute会在执行后关闭它。我也收到了Heroku的回复“Heroku的网络强制执行60-90秒的空闲超时,以防止进程失控。如果您在应用程序中使用持久连接,请确保在55秒时发送keep-alive,以防止服务器断开您打开的连接。“我还发现Heroku vars中定义的MySQL连接字符串的reconnect选项设置为true。我在sqljocky代码中没有找到重新连接选项?感谢您指出了更好的代码,我的新代码使用了wait方法。Connectionpool不是问题所在,因为我重新启动了服务器实例,进行了一次调用,等待10分钟,然后再次进行调用,这将失败。它一定是“Heroku相关的”…另一个关于连接池的评论。我还使用Apache基准测试工具对服务器进行压力测试。在本地,这可以轻松处理1000个请求,同时处理50个请求。