Updated APIv5 Query API (markdown)

Risto Lahtela 2019-07-28 15:57:07 +03:00
parent 10caed53f0
commit c7b20090fd
1 changed files with 31 additions and 1 deletions

@ -95,4 +95,34 @@ done.get(); // (Optional) Block thread until transaction was executed
`execute` does not block the thread, because the SQL is executed on another thread concurrently.
If you would like to wait for the statement to execute you can call `Future#get()` to block the thread.
**Do not call `Future#get()` inside `execute` - This might deadlock the whole database due to blocked transaction thread!**
**Do not call `Future#get()` inside `execute` - This might deadlock the whole database due to blocked transaction thread!**
<details>
<summary>Example of update - insert if not exist procedure</summary>
```
public void storeProtocolVersion(UUID uuid, int version) throws ExecutionException {
String update = "UPDATE plan_version_protocol SET protocol_version=? WHERE uuid=?";
String insert = "INSERT INTO plan_version_protocol (protocol_version, uuid) VALUES (?, ?)";
AtomicBoolean updated = new AtomicBoolean(false);
try {
queryService.execute(update, statement -> {
statement.setInt(1, version);
statement.setString(2, uuid.toString());
updated.set(statement.execute());
}).get(); // Wait
if (!updated.get()) {
queryService.execute(insert, statement -> {
statement.setInt(1, version);
statement.setString(2, uuid.toString());
statement.execute();
});
}
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
}
```
</details>