Skip to content
Open
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
18 changes: 5 additions & 13 deletions best-practices/java-app-best-practices.md
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,10 @@ To verify that this setting already takes effect, you can do:
- Go to TiDB monitoring dashboard and view the request command type through **Query Summary** > **CPS By Instance**.
- If the number of `COM_STMT_EXECUTE` in the request is far more than the number of `COM_STMT_PREPARE`, it means this setting already takes effect.

#### `readOnlyPropagatesToServer`

You should disable the `readOnlyPropagatesToServer` property. This prevents the JDBC driver from sending the `SET SESSION TRANSACTION READ ONLY` statement to the server. TiDB does not support this statement, and it is also unnecessary because all TiDB nodes can handle read/write connections.

#### Batch-related parameters

While processing batch writes, it is recommended to configure `rewriteBatchedStatements=true`. After using `addBatch()` or `executeBatch()`, JDBC still sends SQL one by one by default, for example:
Expand All @@ -142,8 +146,6 @@ pstmt.executeBatch();

Although `Batch` methods are used, the SQL statements sent to TiDB are still individual `INSERT` statements:

{{< copyable "sql" >}}

```sql
insert into t(a) values(10);
insert into t(a) values(11);
Expand All @@ -152,16 +154,12 @@ insert into t(a) values(12);

But if you set `rewriteBatchedStatements=true`, the SQL statements sent to TiDB will be a single `INSERT` statement:

{{< copyable "sql" >}}

```sql
insert into t(a) values(10),(11),(12);
```

Note that the rewrite of the `INSERT` statements is to concatenate the values after multiple "values" keywords into a whole SQL statement. If the `INSERT` statements have other differences, they cannot be rewritten, for example:

{{< copyable "sql" >}}

```sql
insert into t (a) values (10) on duplicate key update a = 10;
insert into t (a) values (11) on duplicate key update a = 11;
Expand All @@ -170,8 +168,6 @@ insert into t (a) values (12) on duplicate key update a = 12;

The above `INSERT` statements cannot be rewritten into one statement. But if you change the three statements into the following ones:

{{< copyable "sql" >}}

```sql
insert into t (a) values (10) on duplicate key update a = values(a);
insert into t (a) values (11) on duplicate key update a = values(a);
Expand All @@ -180,16 +176,12 @@ insert into t (a) values (12) on duplicate key update a = values(a);

Then they meet the rewrite requirement. The above `INSERT` statements will be rewritten into the following one statement:

{{< copyable "sql" >}}

```sql
insert into t (a) values (10), (11), (12) on duplicate key update a = values(a);
```

If there are three or more updates during the batch update, the SQL statements will be rewritten and sent as multiple queries. This effectively reduces the client-to-server request overhead, but the side effect is that a larger SQL statement is generated. For example:

{{< copyable "sql" >}}

```sql
update t set a = 10 where id = 1; update t set a = 11 where id = 2; update t set a = 12 where id = 3;
```
Expand Down Expand Up @@ -416,4 +408,4 @@ Based on commonly used Java components that interact with databases, this docume

## Need help?

Ask the community on [Discord](https://discord.gg/DQZ2dy3cuc?utm_source=doc) or [Slack](https://slack.tidb.io/invite?team=tidb-community&channel=everyone&ref=pingcap-docs), or [submit a support ticket](/support.md).
Ask the community on [Discord](https://discord.gg/DQZ2dy3cuc?utm_source=doc) or [Slack](https://slack.tidb.io/invite?team=tidb-community&channel=everyone&ref=pingcap-docs), or [submit a support ticket](/support.md).