Skip to content

Commit

Permalink
The query timeout can be set.
Browse files Browse the repository at this point in the history
  • Loading branch information
Heromyth committed Jun 2, 2022
1 parent 07d91dc commit f3642c2
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 46 deletions.
46 changes: 26 additions & 20 deletions examples/postgresql/source/app.d
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,14 @@ void main() {
Statement statement;
RowSet rs;

Database db = new Database(
"postgresql://postgres:[email protected]:5432/postgres?charset=utf-8");
string url = "postgresql://postgres:[email protected]:5432/postgres?charset=utf-8";
DatabaseOption options = new DatabaseOption(url);

options.setConnectionTimeout(60000);

Database db = new Database(options);
SqlConnection conn = db.getConnection();


// //
// writeln("============= Delete ==================");
Expand All @@ -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);
}


//
Expand Down Expand Up @@ -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();
Expand Down
49 changes: 26 additions & 23 deletions source/hunt/database/Database.d
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 4 additions & 0 deletions source/hunt/database/base/impl/PoolBase.d
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion source/hunt/database/base/impl/SqlClientBase.d
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
4 changes: 3 additions & 1 deletion source/hunt/database/driver/mysql/impl/MySQLPoolImpl.d
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit f3642c2

Please sign in to comment.