Skip to content

Commit

Permalink
Create rule S6912: Use batch Processing in JDBC (#3612)
Browse files Browse the repository at this point in the history
  • Loading branch information
github-actions[bot] authored Feb 23, 2024
1 parent 2437702 commit e81a653
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 0 deletions.
25 changes: 25 additions & 0 deletions rules/S6912/java/metadata.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"title": "Use batch Processing in JDBC",
"type": "CODE_SMELL",
"status": "ready",
"remediation": {
"func": "Constant\/Issue",
"constantCost": "5min"
},
"tags": [
"sustainability",
"sql"
],
"defaultSeverity": "Major",
"ruleSpecification": "RSPEC-6912",
"sqKey": "S6912",
"scope": "All",
"defaultQualityProfiles": ["Sonar way"],
"quickfix": "unknown",
"code": {
"impacts": {
"MAINTAINABILITY": "LOW"
},
"attribute": "EFFICIENT"
}
}
68 changes: 68 additions & 0 deletions rules/S6912/java/rule.adoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
== Why is this an issue?

Executing a batch of SQL queries instead of individual queries improves performance by reducing communication overhead with the database.

Batching SQL statements is beneficial in common situations where a SQL statement is executed within a loop.
In such cases, adding the statement to a batch and subsequently executing it reduces the number of interactions with the database.
This results in improved efficiency and faster execution times.

The rule raises an issue when it detects a `java.sql.Statement` being executed within a loop instruction, such as `for`, `while` or the `forEach` method of `java.lang.Iterable`, `java.util.Map` and `java.util.stream.Stream`.

== How to fix it

Group SQL statements by using the method `addBatch` to add them to a batch and then execute them using `executeBatch` to send them to the database in a single call.

=== Code examples

==== Noncompliant code example

[source,java,diff-id=1,diff-type=noncompliant]
----
public void execute(Connection connection) {
try {
Statement statement = connection.createStatement();
for (int i = 0; i < 10; i++) {
statement.execute("INSERT INTO myTable (column1, column2) VALUES (" + i + ", 'value" + i + "')"); // Noncompliant
}
statement.close();
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
----

==== Compliant solution

[source,java,diff-id=1,diff-type=compliant]
----
public void execute(Connection connection) {
try {
Statement statement = connection.createStatement();
for (int i = 0; i < 10; i++) {
statement.addBatch("INSERT INTO myTable (column1, column2) VALUES (" + i + ", 'value" + i + "')"); // Compliant
}
statement.executeBatch();
statement.close();
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
----

== Resources

=== Documentation

* https://docs.oracle.com/en/java/javase/21/docs/api/java.sql/java/sql/Statement.html[Oracle Java SE 21 API - java.sql.Statement]
* https://docs.oracle.com/en/java/javase/21/docs/api/java.sql/java/sql/PreparedStatement.html[Oracle Java SE 21 API - java.sql.PreparedStatement]

=== Articles & blog posts

* https://www.baeldung.com/jdbc-batch-processing[Baeldung - JDBC Batch Processing]

2 changes: 2 additions & 0 deletions rules/S6912/metadata.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
{
}

0 comments on commit e81a653

Please sign in to comment.