rspec/rules/S5139/java/rule.adoc

39 lines
975 B
Plaintext
Raw Normal View History

== Why is this an issue?
2021-04-28 16:49:39 +02:00
Some implementations of ``++java.sql.ResultSet#getMetaData()++`` suffer from performance issue and should not be called in a loop. Instead, multiple calls in a row should be replaced by a single cached call.
=== Noncompliant code example
2021-04-28 16:49:39 +02:00
2022-02-04 17:28:24 +01:00
[source,java]
2021-04-28 16:49:39 +02:00
----
ResultSetMetaData rsmd = rs.getMetaData();
for (int i=1; i<rsmd.getColumnCount(); i++) {
System.out.println(rs.getMetaData().getAutoIncrement()); // Noncompliant; "rs.getMetaData()" will be called for each columns
}
----
=== Compliant solution
2021-04-28 16:49:39 +02:00
2022-02-04 17:28:24 +01:00
[source,java]
2021-04-28 16:49:39 +02:00
----
ResultSetMetaData rsmd = rs.getMetaData(); // Compliant; result of "rs.getMetaData()" is cached
for (int i=1; i<rsmd.getColumnCount(); i++) {
System.out.println(rsmd.getAutoIncrement());
}
----
ifdef::env-github,rspecator-view[]
'''
== Implementation Specification
(visible only on this page)
=== Message
Cache the result of "java.sql.ResultSet#getMetaData()" out of this loop.
endif::env-github,rspecator-view[]