@@ -113,7 +113,7 @@ impl<PdC: PdClient> Transaction<PdC> {
113
113
/// ```
114
114
pub async fn get ( & mut self , key : impl Into < Key > ) -> Result < Option < Value > > {
115
115
debug ! ( self . logger, "invoking transactional get request" ) ;
116
- self . check_allow_operation ( ) . await ?;
116
+ self . check_allow_operation ( true ) . await ?;
117
117
let timestamp = self . timestamp . clone ( ) ;
118
118
let rpc = self . rpc . clone ( ) ;
119
119
let key = key. into ( ) ;
@@ -177,7 +177,7 @@ impl<PdC: PdClient> Transaction<PdC> {
177
177
/// ```
178
178
pub async fn get_for_update ( & mut self , key : impl Into < Key > ) -> Result < Option < Value > > {
179
179
debug ! ( self . logger, "invoking transactional get_for_update request" ) ;
180
- self . check_allow_operation ( ) . await ?;
180
+ self . check_allow_operation ( false ) . await ?;
181
181
if !self . is_pessimistic ( ) {
182
182
let key = key. into ( ) ;
183
183
self . lock_keys ( iter:: once ( key. clone ( ) ) ) . await ?;
@@ -244,7 +244,7 @@ impl<PdC: PdClient> Transaction<PdC> {
244
244
keys : impl IntoIterator < Item = impl Into < Key > > ,
245
245
) -> Result < impl Iterator < Item = KvPair > > {
246
246
debug ! ( self . logger, "invoking transactional batch_get request" ) ;
247
- self . check_allow_operation ( ) . await ?;
247
+ self . check_allow_operation ( true ) . await ?;
248
248
let timestamp = self . timestamp . clone ( ) ;
249
249
let rpc = self . rpc . clone ( ) ;
250
250
let retry_options = self . options . retry_options . clone ( ) ;
@@ -299,7 +299,7 @@ impl<PdC: PdClient> Transaction<PdC> {
299
299
self . logger,
300
300
"invoking transactional batch_get_for_update request"
301
301
) ;
302
- self . check_allow_operation ( ) . await ?;
302
+ self . check_allow_operation ( false ) . await ?;
303
303
let keys: Vec < Key > = keys. into_iter ( ) . map ( |k| k. into ( ) ) . collect ( ) ;
304
304
if !self . is_pessimistic ( ) {
305
305
self . lock_keys ( keys. clone ( ) ) . await ?;
@@ -433,7 +433,7 @@ impl<PdC: PdClient> Transaction<PdC> {
433
433
/// ```
434
434
pub async fn put ( & mut self , key : impl Into < Key > , value : impl Into < Value > ) -> Result < ( ) > {
435
435
debug ! ( self . logger, "invoking transactional put request" ) ;
436
- self . check_allow_operation ( ) . await ?;
436
+ self . check_allow_operation ( false ) . await ?;
437
437
let key = key. into ( ) ;
438
438
if self . is_pessimistic ( ) {
439
439
self . pessimistic_lock ( iter:: once ( key. clone ( ) ) , false )
@@ -464,7 +464,7 @@ impl<PdC: PdClient> Transaction<PdC> {
464
464
/// ```
465
465
pub async fn insert ( & mut self , key : impl Into < Key > , value : impl Into < Value > ) -> Result < ( ) > {
466
466
debug ! ( self . logger, "invoking transactional insert request" ) ;
467
- self . check_allow_operation ( ) . await ?;
467
+ self . check_allow_operation ( false ) . await ?;
468
468
let key = key. into ( ) ;
469
469
if self . buffer . get ( & key) . is_some ( ) {
470
470
return Err ( Error :: DuplicateKeyInsertion ) ;
@@ -499,7 +499,7 @@ impl<PdC: PdClient> Transaction<PdC> {
499
499
/// ```
500
500
pub async fn delete ( & mut self , key : impl Into < Key > ) -> Result < ( ) > {
501
501
debug ! ( self . logger, "invoking transactional delete request" ) ;
502
- self . check_allow_operation ( ) . await ?;
502
+ self . check_allow_operation ( false ) . await ?;
503
503
let key = key. into ( ) ;
504
504
if self . is_pessimistic ( ) {
505
505
self . pessimistic_lock ( iter:: once ( key. clone ( ) ) , false )
@@ -537,7 +537,7 @@ impl<PdC: PdClient> Transaction<PdC> {
537
537
keys : impl IntoIterator < Item = impl Into < Key > > ,
538
538
) -> Result < ( ) > {
539
539
debug ! ( self . logger, "invoking transactional lock_keys request" ) ;
540
- self . check_allow_operation ( ) . await ?;
540
+ self . check_allow_operation ( false ) . await ?;
541
541
match self . options . kind {
542
542
TransactionKind :: Optimistic => {
543
543
for key in keys {
@@ -569,6 +569,15 @@ impl<PdC: PdClient> Transaction<PdC> {
569
569
/// ```
570
570
pub async fn commit ( & mut self ) -> Result < Option < Timestamp > > {
571
571
debug ! ( self . logger, "commiting transaction" ) ;
572
+
573
+ {
574
+ // readonly transaction no need to commit
575
+ let status = self . status . read ( ) . await ;
576
+ if * status == TransactionStatus :: ReadOnly {
577
+ return Ok ( None ) ;
578
+ }
579
+ }
580
+
572
581
{
573
582
let mut status = self . status . write ( ) . await ;
574
583
if !matches ! (
@@ -677,7 +686,7 @@ impl<PdC: PdClient> Transaction<PdC> {
677
686
#[ doc( hidden) ]
678
687
pub async fn send_heart_beat ( & mut self ) -> Result < u64 > {
679
688
debug ! ( self . logger, "sending heart_beat" ) ;
680
- self . check_allow_operation ( ) . await ?;
689
+ self . check_allow_operation ( true ) . await ?;
681
690
let primary_key = match self . buffer . get_primary_key ( ) {
682
691
Some ( k) => k,
683
692
None => return Err ( Error :: NoPrimaryKey ) ,
@@ -703,7 +712,7 @@ impl<PdC: PdClient> Transaction<PdC> {
703
712
key_only : bool ,
704
713
reverse : bool ,
705
714
) -> Result < impl Iterator < Item = KvPair > > {
706
- self . check_allow_operation ( ) . await ?;
715
+ self . check_allow_operation ( true ) . await ?;
707
716
let timestamp = self . timestamp . clone ( ) ;
708
717
let rpc = self . rpc . clone ( ) ;
709
718
let retry_options = self . options . retry_options . clone ( ) ;
@@ -840,10 +849,17 @@ impl<PdC: PdClient> Transaction<PdC> {
840
849
}
841
850
842
851
/// Checks if the transaction can perform arbitrary operations.
843
- async fn check_allow_operation ( & self ) -> Result < ( ) > {
852
+ async fn check_allow_operation ( & self , readonly : bool ) -> Result < ( ) > {
844
853
let status = self . status . read ( ) . await ;
845
854
match * status {
846
- TransactionStatus :: ReadOnly | TransactionStatus :: Active => Ok ( ( ) ) ,
855
+ TransactionStatus :: Active => Ok ( ( ) ) ,
856
+ TransactionStatus :: ReadOnly => {
857
+ if readonly {
858
+ Ok ( ( ) )
859
+ } else {
860
+ Err ( Error :: OperationReadOnlyError )
861
+ }
862
+ }
847
863
TransactionStatus :: Committed
848
864
| TransactionStatus :: Rolledback
849
865
| TransactionStatus :: StartedCommit
0 commit comments