SqlJocky 的 ConnectionPool 是否需要关闭
Does the ConnectionPool from SqlJocky require a close
我正在 Dart 中创建一个后端服务器应用程序,它使用 MySQL 数据库来存储数据。为了进行 SQL 调用,我使用了 SqlJocky 的 ConnectionPool。
应用启动时我做了什么:
- 创建一个存储连接池的单例
- 使用 prepareExecute 和 query 执行多个查询
在本地,这种方法运行良好。现在我将一个开发版本推送到 Heroku,几分钟后我遇到了连接问题。
所以我想知道,我是否需要 close/release 来自用于执行查询的池的单个连接?还是查询后的连接重新放入池中免费使用?
所有 MySQL 商店的抽象基础 class:
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;
}
我得到的错误:
SocketException: OS Error: Connection timed out, errno = 110, address = ...
这并没有完全回答您的问题,但我认为您可以像这样简化代码:
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;
}
}
我确定这里不需要关闭与 query
的连接。不过我不知道 prepareExecute
。
根据 SqlJocky 代码中的评论,数据库服务器释放连接可能需要相当长的时间。
也许您需要增加连接池大小(默认为 5),这样在 ConnectionPool
等待释放连接时您不会 运行 连接中断。
在收到 Heroku 的一些反馈后,我设法通过实施一个计时器任务解决了这个问题,该任务每 50 秒执行一次基本 MySQL 调用。
来自 Heroku 的回复:
Heroku's networking enforces an idle timeout of 60-90 seconds to prevent runaway processes. If you're using persistent connections in your application, make sure that you're sending a keep-alive at, say, 55 seconds to prevent your open connection from being dropped by the server.
变通代码:
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 ***');
});
我正在 Dart 中创建一个后端服务器应用程序,它使用 MySQL 数据库来存储数据。为了进行 SQL 调用,我使用了 SqlJocky 的 ConnectionPool。
应用启动时我做了什么:
- 创建一个存储连接池的单例
- 使用 prepareExecute 和 query 执行多个查询
在本地,这种方法运行良好。现在我将一个开发版本推送到 Heroku,几分钟后我遇到了连接问题。
所以我想知道,我是否需要 close/release 来自用于执行查询的池的单个连接?还是查询后的连接重新放入池中免费使用?
所有 MySQL 商店的抽象基础 class:
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;
}
我得到的错误:
SocketException: OS Error: Connection timed out, errno = 110, address = ...
这并没有完全回答您的问题,但我认为您可以像这样简化代码:
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;
}
}
我确定这里不需要关闭与 query
的连接。不过我不知道 prepareExecute
。
根据 SqlJocky 代码中的评论,数据库服务器释放连接可能需要相当长的时间。
也许您需要增加连接池大小(默认为 5),这样在 ConnectionPool
等待释放连接时您不会 运行 连接中断。
在收到 Heroku 的一些反馈后,我设法通过实施一个计时器任务解决了这个问题,该任务每 50 秒执行一次基本 MySQL 调用。
来自 Heroku 的回复:
Heroku's networking enforces an idle timeout of 60-90 seconds to prevent runaway processes. If you're using persistent connections in your application, make sure that you're sending a keep-alive at, say, 55 seconds to prevent your open connection from being dropped by the server.
变通代码:
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 ***');
});