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_out:: CopyOutStream ;
@@ -21,7 +21,7 @@ use fallible_iterator::FallibleIterator;
21
21
use futures_channel:: mpsc;
22
22
use futures_util:: { future, pin_mut, ready, StreamExt , TryStreamExt } ;
23
23
use parking_lot:: Mutex ;
24
- use postgres_protocol:: message:: { backend:: Message , frontend } ;
24
+ use postgres_protocol:: message:: backend:: Message ;
25
25
use postgres_types:: BorrowToSql ;
26
26
use std:: collections:: HashMap ;
27
27
use std:: fmt;
@@ -532,43 +532,7 @@ impl Client {
532
532
///
533
533
/// The transaction will roll back by default - use the `commit` method to commit it.
534
534
pub async fn transaction ( & mut self ) -> Result < Transaction < ' _ > , Error > {
535
- struct RollbackIfNotDone < ' me > {
536
- client : & ' me Client ,
537
- done : bool ,
538
- }
539
-
540
- impl < ' a > Drop for RollbackIfNotDone < ' a > {
541
- fn drop ( & mut self ) {
542
- if self . done {
543
- return ;
544
- }
545
-
546
- let buf = self . client . inner ( ) . with_buf ( |buf| {
547
- frontend:: query ( "ROLLBACK" , buf) . unwrap ( ) ;
548
- buf. split ( ) . freeze ( )
549
- } ) ;
550
- let _ = self
551
- . client
552
- . inner ( )
553
- . send ( RequestMessages :: Single ( FrontendMessage :: Raw ( buf) ) ) ;
554
- }
555
- }
556
-
557
- // This is done, as `Future` created by this method can be dropped after
558
- // `RequestMessages` is synchronously send to the `Connection` by
559
- // `batch_execute()`, but before `Responses` is asynchronously polled to
560
- // completion. In that case `Transaction` won't be created and thus
561
- // won't be rolled back.
562
- {
563
- let mut cleaner = RollbackIfNotDone {
564
- client : self ,
565
- done : false ,
566
- } ;
567
- self . batch_execute ( "BEGIN" ) . await ?;
568
- cleaner. done = true ;
569
- }
570
-
571
- Ok ( Transaction :: new ( self ) )
535
+ self . build_transaction ( ) . start ( ) . await
572
536
}
573
537
574
538
/// Returns a builder for a transaction with custom settings.
0 commit comments