File tree Expand file tree Collapse file tree 2 files changed +26
-0
lines changed Expand file tree Collapse file tree 2 files changed +26
-0
lines changed Original file line number Diff line number Diff line change @@ -422,6 +422,11 @@ impl Client {
422
422
self . connection . block_on ( self . client . batch_execute ( query) )
423
423
}
424
424
425
+ /// Check the connection is alive and wait for the confirmation.
426
+ pub fn check_connection ( & mut self ) -> Result < ( ) , Error > {
427
+ self . connection . block_on ( self . client . check_connection ( ) )
428
+ }
429
+
425
430
/// Begins a new database transaction.
426
431
///
427
432
/// The transaction will roll back by default - use the `commit` method to commit it.
Original file line number Diff line number Diff line change @@ -508,3 +508,24 @@ fn check_send() {
508
508
is_send :: < Statement > ( ) ;
509
509
is_send :: < Transaction < ' _ > > ( ) ;
510
510
}
511
+
512
+ #[ test]
513
+ fn is_closed ( ) {
514
+ let mut client = Client :: connect ( "host=localhost port=5433 user=postgres" , NoTls ) . unwrap ( ) ;
515
+ assert ! ( !client. is_closed( ) ) ;
516
+ client. check_connection ( ) . unwrap ( ) ;
517
+
518
+ let row = client. query_one ( "select pg_backend_pid()" , & [ ] ) . unwrap ( ) ;
519
+ let pid: i32 = row. get ( 0 ) ;
520
+
521
+ {
522
+ let mut client2 = Client :: connect ( "host=localhost port=5433 user=postgres" , NoTls ) . unwrap ( ) ;
523
+ client2
524
+ . query ( "SELECT pg_terminate_backend($1)" , & [ & pid] )
525
+ . unwrap ( ) ;
526
+ }
527
+
528
+ assert ! ( !client. is_closed( ) ) ;
529
+ client. check_connection ( ) . unwrap_err ( ) ;
530
+ assert ! ( client. is_closed( ) ) ;
531
+ }
You can’t perform that action at this time.
0 commit comments