@@ -10,9 +10,27 @@ use crate::Result;
1010
1111use  super :: WaitFrameNoCb ; 
1212
13+ #[ derive( Debug ,  Default ) ]  
14+ enum  State  { 
15+     Txn , 
16+     #[ default]  
17+     Idle , 
18+     Unknown 
19+ } 
20+ 
21+ impl  State  { 
22+     /// Returns `true` if the state is [`Idle`]. 
23+ /// 
24+ /// [`Idle`]: State::Idle 
25+ #[ must_use]  
26+     fn  is_idle ( & self )  -> bool  { 
27+         matches ! ( self ,  Self :: Idle ) 
28+     } 
29+ } 
30+ 
1331#[ derive( Debug ,  Default ) ]  
1432pub ( crate )  struct  ConnState  { 
15-     is_txn :   bool , 
33+     state :   State , 
1634    last_frame_no :  Option < FrameNo > , 
1735} 
1836
@@ -123,6 +141,9 @@ where
123141                state :  self . state . clone ( ) , 
124142            } ; 
125143
144+             // set the connection state to unknown before executing on the remote 
145+             self . state . lock ( ) . state  = State :: Unknown ; 
146+ 
126147            self . conn 
127148                . execute_program ( & self . pgm ,  Box :: new ( builder) ) 
128149                . unwrap ( ) ; 
@@ -144,7 +165,7 @@ where
144165        pgm :  & Program , 
145166        builder :  Box < dyn  ResultBuilder > , 
146167    )  -> crate :: Result < ( ) >  { 
147-         if  ! self . state . lock ( ) . is_txn  && pgm. is_read_only ( )  { 
168+         if  self . state . lock ( ) . state . is_idle ( )  && pgm. is_read_only ( )  { 
148169            if  let  Some ( frame_no)  = self . state . lock ( ) . last_frame_no  { 
149170                ( self . wait_frame_no_cb ) ( frame_no) ; 
150171            } 
@@ -162,6 +183,9 @@ where
162183            // rollback(&mut self.conn.read_db); 
163184            Ok ( ( ) ) 
164185        }  else  { 
186+             // we set the state to unknown because until we have received from the actual 
187+             // connection state from the primary. 
188+             self . state . lock ( ) . state  = State :: Unknown ; 
165189            let  builder = ExtractFrameNoBuilder  { 
166190                builder, 
167191                state :  self . state . clone ( ) , 
@@ -243,7 +267,11 @@ impl ResultBuilder for ExtractFrameNoBuilder {
243267    )  -> Result < bool ,  QueryResultBuilderError >  { 
244268        let  mut  state = self . state . lock ( ) ; 
245269        state. last_frame_no  = frame_no; 
246-         state. is_txn  = is_txn; 
270+         if  is_txn { 
271+             state. state  = State :: Txn ; 
272+         }  else  { 
273+             state. state  = State :: Idle ; 
274+         } 
247275        self . builder . finnalize ( is_txn,  frame_no) 
248276    } 
249277} 
0 commit comments