1
- use crate :: codec:: { BackendMessages , FrontendMessage } ;
1
+ use crate :: codec:: BackendMessages ;
2
2
use crate :: config:: SslMode ;
3
3
use crate :: connection:: { Request , RequestMessages } ;
4
4
use crate :: copy_both:: CopyBothDuplex ;
@@ -23,7 +23,7 @@ use fallible_iterator::FallibleIterator;
23
23
use futures_channel:: mpsc;
24
24
use futures_util:: { future, pin_mut, ready, StreamExt , TryStreamExt } ;
25
25
use parking_lot:: Mutex ;
26
- use postgres_protocol:: message:: { backend:: Message , frontend } ;
26
+ use postgres_protocol:: message:: backend:: Message ;
27
27
use postgres_types:: BorrowToSql ;
28
28
use std:: collections:: HashMap ;
29
29
use std:: fmt;
@@ -493,43 +493,7 @@ impl Client {
493
493
///
494
494
/// The transaction will roll back by default - use the `commit` method to commit it.
495
495
pub async fn transaction ( & mut self ) -> Result < Transaction < ' _ > , Error > {
496
- struct RollbackIfNotDone < ' me > {
497
- client : & ' me Client ,
498
- done : bool ,
499
- }
500
-
501
- impl < ' a > Drop for RollbackIfNotDone < ' a > {
502
- fn drop ( & mut self ) {
503
- if self . done {
504
- return ;
505
- }
506
-
507
- let buf = self . client . inner ( ) . with_buf ( |buf| {
508
- frontend:: query ( "ROLLBACK" , buf) . unwrap ( ) ;
509
- buf. split ( ) . freeze ( )
510
- } ) ;
511
- let _ = self
512
- . client
513
- . inner ( )
514
- . send ( RequestMessages :: Single ( FrontendMessage :: Raw ( buf) ) ) ;
515
- }
516
- }
517
-
518
- // This is done, as `Future` created by this method can be dropped after
519
- // `RequestMessages` is synchronously send to the `Connection` by
520
- // `batch_execute()`, but before `Responses` is asynchronously polled to
521
- // completion. In that case `Transaction` won't be created and thus
522
- // won't be rolled back.
523
- {
524
- let mut cleaner = RollbackIfNotDone {
525
- client : self ,
526
- done : false ,
527
- } ;
528
- self . batch_execute ( "BEGIN" ) . await ?;
529
- cleaner. done = true ;
530
- }
531
-
532
- Ok ( Transaction :: new ( self ) )
496
+ self . build_transaction ( ) . start ( ) . await
533
497
}
534
498
535
499
/// Returns a builder for a transaction with custom settings.
0 commit comments