-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create rule S6912: Use batch Processing in JDBC (#3612)
- Loading branch information
1 parent
2437702
commit e81a653
Showing
3 changed files
with
95 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
{ | ||
} |