Elffuss BraidHub

kikocis/java-driver public

JAVA-1198: Document that BoundStatement is not thread-safe. (#685)

Changeset8f204f38907d9c77f63c63247af3ee8eea7c336a37b0b9e77e391eaa39a112c9
AuthorOlivier Michallat <olim7t@users.noreply.github.com> · 2016-06-27 11:22 -07:00
CommitterGitHub <noreply@github.com> · 2016-06-27 11:22 -07:00
Parentscbe1bc533f9e

Browse the files at this changeset

3 files changed

modified changelog/README.md +1 −0
@@ -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
modified driver-core/src/main/java/com/datastax/driver/core/BoundStatement.java +5 −0
@@ -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);
modified manual/statements/prepared/README.md +22 −3
@@ -118,7 +118,7 @@
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 @@
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