Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ public String toString() {
private boolean callCleared;
private boolean closed;
@Nullable private String closeMessage;
private final boolean withBeginTransaction;
private volatile boolean withBeginTransaction;
private final boolean lastStatement;
@Nullable private volatile Timeout streamWaitTimeout;
@Nullable private volatile SpannerException error;
Expand Down Expand Up @@ -156,6 +156,14 @@ void registerListener(AsyncResultSet.StreamMessageListener streamMessageListener
}
}

@VisibleForTesting
void setCall(SpannerRpc.StreamingCall call, boolean withBeginTransaction) {
synchronized (this) {
this.withBeginTransaction = withBeginTransaction;
}
Comment on lines +161 to +163

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The withBeginTransaction variable is declared as volatile, which already guarantees visibility and atomicity for writes. If this assignment does not need to be atomic with other operations protected by synchronized (this), wrapping it in a synchronized block is redundant. Please verify whether the lock is required for coordinating other states before removing it.

this.withBeginTransaction = withBeginTransaction;
References
  1. Before suggesting lock-free alternatives or atomic variables to avoid lock contention, verify whether the target method actually acquires any locks or if the state is already managed without synchronization.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If it can be simplified, then we will do so in a follow-up PR (currently under investigation).

setCall(call);
}

void setCall(SpannerRpc.StreamingCall call) {
Timeout timeout = extractTimeout(call);
boolean shouldCancel;
Expand Down
Loading