Skip to content

Commit

Permalink
fix bugs in onOpen callback to configure connection settings, improve…
Browse files Browse the repository at this point in the history
…ments to README, implemented connection pool for 'postgres' (v2) driver_implementation
  • Loading branch information
Isaque Neves committed Apr 19, 2024
1 parent df94933 commit 8609534
Show file tree
Hide file tree
Showing 20 changed files with 2,949 additions and 101 deletions.
8 changes: 7 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,4 +80,10 @@ final manager = Manager();

## 3.1.2

- Fix bugs in lost connection detection to automatically reconnect. Updated postgres to 3.1.2 to be able to use the onOpen callback to configure connection settings like setting search path
- fix bugs in lost connection detection to automatically reconnect. Updated postgres to 3.1.2 to be able to use the onOpen callback to configure connection settings like setting search path

## 3.2.0

- fix bugs in onOpen callback to configure connection settings
- improvements to README
- implemented connection pool for 'postgres' (v2) driver_implementation
101 changes: 100 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ for now it only works with PostgreSQL and MySQL
'password': 'pass',
'charset': 'utf8',
'prefix': '',
'schema': ['public'],
'schema': 'public',
});
manager.setAsGlobal();
final db = await manager.connection();
Expand Down Expand Up @@ -76,6 +76,105 @@ for now it only works with PostgreSQL and MySQL
await db.table('temp_location')
.where('id', '=', 1).delete();
```
## using connection pool (works for mysql and postgresql)

```dart
var manager = new Manager();
manager.addConnection({
'driver': 'pgsql',
'driver_implementation': 'postgres_v3',
'host': 'localhost',
'port': '5432',
'database': 'database_name',
'username': 'user_name',
'password': 'pass',
'charset': 'utf8',
'prefix': '',
'schema': 'public,other',
'pool': true,
'poolsize': 8,
});
final db = await manager.connection();
final query = db.table('temp_location');
final res = await query
.select(['temp_location.id', 'city', 'street'])
.where('temp_location.id', '=', 1)
.join('people', 'people.id', '=', 'temp_location.id_people', 'inner')
.limit(1)
.offset(0)
.get();
```


## connect and disconnect in loop

```dart
var manager = new Manager();
manager.addConnection({
'driver': 'pgsql',
'host': 'localhost',
'port': '5432',
'database': 'database_name',
'username': 'user_name',
'password': 'pass',
'charset': 'utf8',
'prefix': '',
'schema': 'public,other',
});
while (true){
//connect
final db = await manager.connection();
final res = await query
.select(['temp_location.id', 'city', 'street'])
.where('temp_location.id', '=', 1)
.join('people', 'people.id', '=', 'temp_location.id_people', 'inner')
.limit(1)
.offset(0)
.get();
//disconnect
await manager.getDatabaseManager().purge();
}
```

## using different drivers implementation for postgresql

```dart
var manager = new Manager();
manager.addConnection({
'driver': 'pgsql',
'driver_implementation': 'postgres_v3', // postgres | dargres | postgres_v3
'host': 'localhost',
'port': '5432',
'database': 'database_name',
'username': 'user_name',
'password': 'pass',
'charset': 'utf8',
'prefix': '',
'schema': 'public,other',
});
//connect
final db = await manager.connection();
final res = await query
.select(['temp_location.id', 'city', 'street'])
.where('temp_location.id', '=', 1)
.join('people', 'people.id', '=', 'temp_location.id_people', 'inner')
.limit(1)
.offset(0)
.get();
//disconnect
await manager.getDatabaseManager().purge();
```

## mysql example
Expand Down
22 changes: 17 additions & 5 deletions lib/src/connection.dart
Original file line number Diff line number Diff line change
Expand Up @@ -583,9 +583,21 @@ class Connection with DetectsLostConnections implements ConnectionInterface {
.runQueryCallback(query, bindings, callback, timeoutInSeconds);
//print('Connection@run $result');
} catch (e) {
//print('Connection@run error $e');
result = await this
.tryAgainIfCausedByLostConnection(e, query, bindings, callback, timeoutInSeconds);
// print('Connection@run error $e $e');
// result = await this
// .tryAgainIfCausedByLostConnection(e, query, bindings, callback, timeoutInSeconds);

if (this.causedByLostConnection(e) &&
tryReconnectCount < tryReconnectLimit) {
await Future.delayed(Duration(milliseconds: 1000));
// print('Eloquent@tryAgainIfCausedByLostConnection try reconnect...');
tryReconnectLimit++;
await this.reconnect();
tryReconnectLimit = 0;
return await this
.runQueryCallback(query, bindings, callback, timeoutInSeconds);
}
rethrow;
}

// Once we have run the query we will calculate the time that it took to run and
Expand Down Expand Up @@ -656,14 +668,14 @@ class Connection with DetectsLostConnections implements ConnectionInterface {
if (this.causedByLostConnection(e) &&
tryReconnectCount < tryReconnectLimit) {
await Future.delayed(Duration(milliseconds: delay));
// print('Eloquent@tryAgainIfCausedByLostConnection try reconnect...');
// print('Eloquent@tryAgainIfCausedByLostConnection try reconnect...');
tryReconnectLimit++;
await this.reconnect();
tryReconnectLimit = 0;
return await this
.runQueryCallback(query, bindings, callback, timeoutInSeconds);
}
// print('tryAgainIfCausedByLostConnection');
// print('tryAgainIfCausedByLostConnection');
throw e;
}

Expand Down
2 changes: 1 addition & 1 deletion lib/src/detects_lost_connections.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ mixin DetectsLostConnections {
/// @param \Exception $e
/// @return bool
///
bool causedByLostConnection(Exception e) {
bool causedByLostConnection(dynamic e) {
final message = '$e';
//TODO revise isso para outros cenários
final isR = Utils.string_contains(message, [
Expand Down
Loading

0 comments on commit 8609534

Please sign in to comment.