kikocis/java-driver public
Changesets · Branches · Tags · Compare
| Changeset | 8f204f38907d9c77f63c63247af3ee8eea7c336a37b0b9e77e391eaa39a112c9 |
|---|---|
| Author | Olivier Michallat <olim7t@users.noreply.github.com> · 2016-06-27 11:22 -07:00 |
| Committer | GitHub <noreply@github.com> · 2016-06-27 11:22 -07:00 |
| Parents | cbe1bc533f9e |
3 files changed
modified changelog/README.md +1 −0
| @@ -12,6 +12,7 @@ | ||
| 12 | 12 | - [improvement] JAVA-1175: Warn if DCAwarePolicy configuration is inconsistent. |
| 13 | 13 | - [bug] JAVA-1139: ConnectionException.getMessage() throws NPE if address is null. |
| 14 | 14 | - [bug] JAVA-1202: Handle null rpc_address when checking schema agreement. |
| 15 | +- [improvement] JAVA-1198: Document that BoundStatement is not thread-safe. | |
| 15 | 16 | |
| 16 | 17 | |
| 17 | 18 | ### 3.0.2 |
modified driver-core/src/main/java/com/datastax/driver/core/BoundStatement.java +5 −0
| @@ -46,6 +46,11 @@ | ||
| 46 | 46 | * will be ignored server side (no tombstones will be generated). If you're reusing |
| 47 | 47 | * a bound statement, you can {@link #unset(int) unset} variables that were previously |
| 48 | 48 | * 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). | |
| 49 | 54 | */ |
| 50 | 55 | public class BoundStatement extends Statement implements SettableData<BoundStatement>, GettableData { |
| 51 | 56 | static final ByteBuffer UNSET = ByteBuffer.allocate(0); |
modified manual/statements/prepared/README.md +22 −3
| @@ -118,7 +118,7 @@ | ||
| 118 | 118 | ```java |
| 119 | 119 | BoundStatement bound = ps1.bind() |
| 120 | 120 | .setString("sku", "324378") |
| 121 | - .setString("description", "LCD screen") | |
| 121 | + .setString("description", "LCD screen"); | |
| 122 | 122 | |
| 123 | 123 | // Using the unset method to unset previously set value. |
| 124 | 124 | // Positional setter: |
| @@ -133,8 +133,27 @@ | ||
| 133 | 133 | serialized form. |
| 134 | 134 | |
| 135 | 135 | `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 | + | |
| 138 | 157 | Also, make sure you don't accidentally reuse parameters from previous executions. |
| 139 | 158 | |
| 140 | 159 | ### Preparing on multiple nodes |