32
32
class ClientSessionImpl extends BaseClientSessionImpl implements ClientSession {
33
33
34
34
private enum TransactionState {
35
- NONE , IN , DONE , ABORTED
35
+ NONE , IN , COMMITTED , ABORTED
36
36
}
37
37
38
38
private final OperationExecutor executor ;
39
39
private TransactionState transactionState = TransactionState .NONE ;
40
- private boolean messageSent ;
40
+ private boolean messageSentInCurrentTransaction ;
41
41
private boolean commitInProgress ;
42
42
43
43
private TransactionOptions transactionOptions ;
@@ -50,19 +50,26 @@ private enum TransactionState {
50
50
51
51
@ Override
52
52
public boolean hasActiveTransaction () {
53
- return transactionState == TransactionState .IN || (transactionState == TransactionState .DONE && commitInProgress );
53
+ return transactionState == TransactionState .IN || (transactionState == TransactionState .COMMITTED && commitInProgress );
54
54
}
55
55
56
56
@ Override
57
57
public boolean notifyMessageSent () {
58
- boolean firstMessage = !messageSent ;
59
- messageSent = true ;
60
- return firstMessage ;
58
+ if (hasActiveTransaction ()) {
59
+ boolean firstMessageInCurrentTransaction = !messageSentInCurrentTransaction ;
60
+ messageSentInCurrentTransaction = true ;
61
+ return firstMessageInCurrentTransaction ;
62
+ } else {
63
+ if (transactionState == TransactionState .COMMITTED || transactionState == TransactionState .ABORTED ) {
64
+ cleanupTransaction (TransactionState .NONE );
65
+ }
66
+ return false ;
67
+ }
61
68
}
62
69
63
70
@ Override
64
71
public TransactionOptions getTransactionOptions () {
65
- isTrue ("in transaction" , transactionState == TransactionState .IN || transactionState == TransactionState .DONE );
72
+ isTrue ("in transaction" , transactionState == TransactionState .IN || transactionState == TransactionState .COMMITTED );
66
73
return transactionOptions ;
67
74
}
68
75
@@ -77,7 +84,7 @@ public void startTransaction(final TransactionOptions transactionOptions) {
77
84
if (transactionState == TransactionState .IN ) {
78
85
throw new IllegalStateException ("Transaction already in progress" );
79
86
}
80
- if (transactionState == TransactionState .DONE ) {
87
+ if (transactionState == TransactionState .COMMITTED ) {
81
88
cleanupTransaction (TransactionState .IN );
82
89
} else {
83
90
transactionState = TransactionState .IN ;
@@ -94,8 +101,8 @@ public void commitTransaction(final SingleResultCallback<Void> callback) {
94
101
if (transactionState == TransactionState .NONE ) {
95
102
throw new IllegalStateException ("There is no transaction started" );
96
103
}
97
- if (!messageSent ) {
98
- cleanupTransaction (TransactionState .DONE );
104
+ if (!messageSentInCurrentTransaction ) {
105
+ cleanupTransaction (TransactionState .COMMITTED );
99
106
callback .onResult (null , null );
100
107
} else {
101
108
ReadConcern readConcern = transactionOptions .getReadConcern ();
@@ -109,7 +116,7 @@ public void commitTransaction(final SingleResultCallback<Void> callback) {
109
116
@ Override
110
117
public void onResult (final Void result , final Throwable t ) {
111
118
commitInProgress = false ;
112
- transactionState = TransactionState .DONE ;
119
+ transactionState = TransactionState .COMMITTED ;
113
120
callback .onResult (result , t );
114
121
}
115
122
});
@@ -121,13 +128,13 @@ public void abortTransaction(final SingleResultCallback<Void> callback) {
121
128
if (transactionState == TransactionState .ABORTED ) {
122
129
throw new IllegalStateException ("Cannot call abortTransaction twice" );
123
130
}
124
- if (transactionState == TransactionState .DONE ) {
131
+ if (transactionState == TransactionState .COMMITTED ) {
125
132
throw new IllegalStateException ("Cannot call abortTransaction after calling commitTransaction" );
126
133
}
127
134
if (transactionState == TransactionState .NONE ) {
128
135
throw new IllegalStateException ("There is no transaction started" );
129
136
}
130
- if (!messageSent ) {
137
+ if (!messageSentInCurrentTransaction ) {
131
138
cleanupTransaction (TransactionState .ABORTED );
132
139
callback .onResult (null , null );
133
140
} else {
@@ -163,7 +170,7 @@ public void onResult(final Void result, final Throwable t) {
163
170
}
164
171
165
172
private void cleanupTransaction (final TransactionState nextState ) {
166
- messageSent = false ;
173
+ messageSentInCurrentTransaction = false ;
167
174
transactionOptions = null ;
168
175
transactionState = nextState ;
169
176
}
0 commit comments