Skip to content

Commit 66f5814

Browse files
authored
JAVA-1198: Document that BoundStatement is not thread-safe. (apache#685)
1 parent 1516ab4 commit 66f5814

3 files changed

Lines changed: 28 additions & 3 deletions

File tree

changelog/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
- [improvement] JAVA-1175: Warn if DCAwarePolicy configuration is inconsistent.
1313
- [bug] JAVA-1139: ConnectionException.getMessage() throws NPE if address is null.
1414
- [bug] JAVA-1202: Handle null rpc_address when checking schema agreement.
15+
- [improvement] JAVA-1198: Document that BoundStatement is not thread-safe.
1516

1617

1718
### 3.0.2

driver-core/src/main/java/com/datastax/driver/core/BoundStatement.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,11 @@
4646
* will be ignored server side (no tombstones will be generated). If you're reusing
4747
* a bound statement, you can {@link #unset(int) unset} variables that were previously
4848
* set.
49+
* <p/>
50+
* This class is <b>not thread-safe</b>. Do not share instances among requests that will
51+
* execute concurrently (e.g. requests run from separate application threads, but also
52+
* separate {@link Session#executeAsync(Statement) executeAsync} calls, even if they're
53+
* triggered from the same thread).
4954
*/
5055
public class BoundStatement extends Statement implements SettableData<BoundStatement>, GettableData {
5156
static final ByteBuffer UNSET = ByteBuffer.allocate(0);

manual/statements/prepared/README.md

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ that were previously set:
118118
```java
119119
BoundStatement bound = ps1.bind()
120120
.setString("sku", "324378")
121-
.setString("description", "LCD screen")
121+
.setString("description", "LCD screen");
122122

123123
// Using the unset method to unset previously set value.
124124
// Positional setter:
@@ -133,8 +133,27 @@ this has a small performance overhead since values are stored in their
133133
serialized form.
134134

135135
`BoundStatement` is **not thread-safe**. You can reuse an instance multiple times with different parameters, but only
136-
from a single thread, and only if you use the synchronous API ([Session#execute][execute] is fine,
137-
[Session#executeAsync][executeAsync] is not).
136+
from a single thread and only if you use synchronous calls:
137+
138+
```java
139+
BoundStatement bound = ps1.bind();
140+
141+
// This is safe:
142+
bound.setString("sku", "324378");
143+
session.execute(bound);
144+
145+
bound.setString("sku", "324379");
146+
session.execute(bound);
147+
148+
// This is NOT SAFE. executeAsync runs concurrently with your code, so the first execution might actually read the
149+
// values after the second setString call, and you would insert 324381 twice:
150+
bound.setString("sku", "324380");
151+
session.executeAsync(bound);
152+
153+
bound.setString("sku", "324381");
154+
session.executeAsync(bound);
155+
```
156+
138157
Also, make sure you don't accidentally reuse parameters from previous executions.
139158

140159
### Preparing on multiple nodes

0 commit comments

Comments
 (0)