1- use crate :: codec:: { BackendMessages , FrontendMessage } ;
1+ use crate :: codec:: BackendMessages ;
22use crate :: config:: SslMode ;
33use crate :: connection:: { Request , RequestMessages } ;
44use crate :: copy_both:: CopyBothDuplex ;
@@ -23,7 +23,7 @@ use fallible_iterator::FallibleIterator;
2323use futures_channel:: mpsc;
2424use futures_util:: { future, pin_mut, ready, StreamExt , TryStreamExt } ;
2525use parking_lot:: Mutex ;
26- use postgres_protocol:: message:: { backend:: Message , frontend } ;
26+ use postgres_protocol:: message:: backend:: Message ;
2727use postgres_types:: BorrowToSql ;
2828use std:: collections:: HashMap ;
2929use std:: fmt;
@@ -493,43 +493,7 @@ impl Client {
493493 ///
494494 /// The transaction will roll back by default - use the `commit` method to commit it.
495495 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
533497 }
534498
535499 /// Returns a builder for a transaction with custom settings.
0 commit comments