From f3642c21bdd9a6546a6c25a1e278d6ed8c10c406 Mon Sep 17 00:00:00 2001 From: heromyth Date: Thu, 2 Jun 2022 16:48:03 +0800 Subject: [PATCH] The query timeout can be set. --- examples/postgresql/source/app.d | 46 +++++++++-------- source/hunt/database/Database.d | 49 ++++++++++--------- source/hunt/database/base/impl/PoolBase.d | 4 ++ .../hunt/database/base/impl/SqlClientBase.d | 2 +- .../driver/mysql/impl/MySQLPoolImpl.d | 4 +- .../postgresql/impl/PostgreSQLPoolImpl.d | 4 +- 6 files changed, 63 insertions(+), 46 deletions(-) diff --git a/examples/postgresql/source/app.d b/examples/postgresql/source/app.d index eb97c16..c36d04d 100755 --- a/examples/postgresql/source/app.d +++ b/examples/postgresql/source/app.d @@ -22,8 +22,14 @@ void main() { Statement statement; RowSet rs; - Database db = new Database( - "postgresql://postgres:123456@10.1.11.44:5432/postgres?charset=utf-8"); + string url = "postgresql://postgres:123456@10.1.11.44:5432/postgres?charset=utf-8"; + DatabaseOption options = new DatabaseOption(url); + + options.setConnectionTimeout(60000); + + Database db = new Database(options); + SqlConnection conn = db.getConnection(); + // // // writeln("============= Delete =================="); @@ -48,15 +54,15 @@ void main() { // result = statement.execute(); // tracef("result: %d", result); - // // - // writeln("============= Select =================="); - // statement = db.prepare("SELECT * FROM public.test where id=:id limit 10"); - // statement.setParameter("id", 1); - // rs = statement.query(); + // + writeln("============= Select =================="); + statement = db.prepare(conn, "SELECT * FROM public.test where id=:id limit 10"); + statement.setParameter("id", 1); + rs = statement.query(); - // foreach (Row row; rs) { - // writeln(row); - // } + foreach (Row row; rs) { + writeln(row); + } // @@ -94,19 +100,19 @@ void main() { // - writeln("============= Class Binding =================="); - sql = `SELECT a.id as immutable__as__id, a.message as immutable__as__message, - b.id as world__as__id, b.randomnumber as world__as__randomnumber - FROM immutable as a LEFT JOIN world as b on a.id = b.id where a.id=1;`; + // writeln("============= Class Binding =================="); + // sql = `SELECT a.id as immutable__as__id, a.message as immutable__as__message, + // b.id as world__as__id, b.randomnumber as world__as__randomnumber + // FROM immutable as a LEFT JOIN world as b on a.id = b.id where a.id=1;`; - statement = db.prepare(sql); - rs = statement.query(); + // statement = db.prepare(sql); + // rs = statement.query(); - Immutable[] testEntities = rs.bind!(Immutable, (a, b) => a ~ "__as__" ~ b)(); + // Immutable[] testEntities = rs.bind!(Immutable, (a, b) => a ~ "__as__" ~ b)(); - foreach (Immutable t; testEntities) { - writeln(t); - } + // foreach (Immutable t; testEntities) { + // writeln(t); + // } db.close(); diff --git a/source/hunt/database/Database.d b/source/hunt/database/Database.d index 921d1d9..807a931 100644 --- a/source/hunt/database/Database.d +++ b/source/hunt/database/Database.d @@ -86,29 +86,32 @@ class Database tracef("maximumSize: %d, connectionTimeout: %d", _options.maximumPoolSize, _options.connectionTimeout); } - if(_options.isPgsql()) { - PgConnectOptions connectOptions = new PgConnectOptions(_options.url); - connectOptions.setDecoderBufferSize(_options.getDecoderBufferSize()); - connectOptions.setEncoderBufferSize(_options.getEncoderBufferSize()); - - PoolOptions poolOptions = new PoolOptions() - .setMaxSize(_options.maximumPoolSize) - .awaittingTimeout(_options.connectionTimeout.msecs); - _pool = new PgPoolImpl(connectOptions, poolOptions); - - } else if(_options.isMysql()) { - MySQLConnectOptions connectOptions = new MySQLConnectOptions(_options.url); - connectOptions.setDecoderBufferSize(_options.getDecoderBufferSize()); - connectOptions.setEncoderBufferSize(_options.getEncoderBufferSize()); - - PoolOptions poolOptions = new PoolOptions() - .setMaxSize(_options.maximumPoolSize) - .awaittingTimeout(_options.connectionTimeout.msecs); - _pool = new MySQLPoolImpl(connectOptions, poolOptions); - - } else { - throw new DatabaseException("Unsupported database driver: " ~ _options.schemeName()); - } + // dfmt off + PoolOptions poolOptions = new PoolOptions() + .setMaxSize(_options.maximumPoolSize) + // .retry(_options.retry) + .awaittingTimeout(_options.connectionTimeout.msecs); + + if(_options.isPgsql()) { + PgConnectOptions connectOptions = new PgConnectOptions(_options.url); + connectOptions.setDecoderBufferSize(_options.getDecoderBufferSize()); + connectOptions.setEncoderBufferSize(_options.getEncoderBufferSize()); + connectOptions.setConnectTimeout(_options.connectionTimeout().msecs); + + _pool = new PgPoolImpl(connectOptions, poolOptions); + } else if(_options.isMysql()) { + MySQLConnectOptions connectOptions = new MySQLConnectOptions(_options.url); + connectOptions.setDecoderBufferSize(_options.getDecoderBufferSize()); + connectOptions.setEncoderBufferSize(_options.getEncoderBufferSize()); + connectOptions.setConnectTimeout(_options.connectionTimeout().msecs); + + _pool = new MySQLPoolImpl(connectOptions, poolOptions); + + } else { + throw new DatabaseException("Unsupported database driver: " ~ _options.schemeName()); + } + + // dfmt on } int execute(string sql) diff --git a/source/hunt/database/base/impl/PoolBase.d b/source/hunt/database/base/impl/PoolBase.d index d288748..1167521 100755 --- a/source/hunt/database/base/impl/PoolBase.d +++ b/source/hunt/database/base/impl/PoolBase.d @@ -61,6 +61,10 @@ abstract class PoolBase(P) : SqlClientBase!(P), Pool { // extends PoolBase!(P) abstract void connect(AsyncDbConnectionHandler completionHandler); + PoolOptions options() { + return _options; + } + override void getConnection(AsyncSqlConnectionHandler handler) { pool.acquire((DbConnectionAsyncResult ar) { diff --git a/source/hunt/database/base/impl/SqlClientBase.d b/source/hunt/database/base/impl/SqlClientBase.d index c3a35cd..7af5645 100755 --- a/source/hunt/database/base/impl/SqlClientBase.d +++ b/source/hunt/database/base/impl/SqlClientBase.d @@ -88,7 +88,7 @@ abstract class SqlClientBase(C) : SqlClient, CommandScheduler { // if(is(C : Sq RowSet query(string sql) { auto f = queryAsync(sql); try { - version(HUNT_DEBUG) trace("try to get a query result"); + version(HUNT_DB_DEBUG) tracef("try to get a query result in %s", _awaittingTimeout); import core.time; return f.get(_awaittingTimeout); } catch(Exception ex) { diff --git a/source/hunt/database/driver/mysql/impl/MySQLPoolImpl.d b/source/hunt/database/driver/mysql/impl/MySQLPoolImpl.d index b80815f..afdde38 100755 --- a/source/hunt/database/driver/mysql/impl/MySQLPoolImpl.d +++ b/source/hunt/database/driver/mysql/impl/MySQLPoolImpl.d @@ -30,7 +30,9 @@ class MySQLPoolImpl : PoolBase!(MySQLPoolImpl), MySQLPool { } override protected SqlConnection wrap(DbConnection conn) { - return new MySQLConnectionImpl(factory, conn); + MySQLConnectionImpl impl = new MySQLConnectionImpl(factory, conn); + impl.awaittingTimeout = options.awaittingTimeout(); + return impl; } override protected void doClose() { diff --git a/source/hunt/database/driver/postgresql/impl/PostgreSQLPoolImpl.d b/source/hunt/database/driver/postgresql/impl/PostgreSQLPoolImpl.d index 8520d54..d2a4119 100755 --- a/source/hunt/database/driver/postgresql/impl/PostgreSQLPoolImpl.d +++ b/source/hunt/database/driver/postgresql/impl/PostgreSQLPoolImpl.d @@ -54,7 +54,9 @@ class PgPoolImpl : PoolBase!(PgPoolImpl), PgPool { override protected SqlConnection wrap(DbConnection conn) { - return new PgConnectionImpl(factory, conn); + PgConnectionImpl impl = new PgConnectionImpl(factory, conn); + impl.awaittingTimeout = options.awaittingTimeout(); + return impl; } override