rspec/rules/S3074/java/rule.adoc
Fred Tingaud 16f6c0aecf
Inline adoc when include has no additional value (#1940)
Inline adoc files when they are included exactly once.

Also fix language tags because this inlining gives us better information
on what language the code is written in.
2023-05-25 14:18:12 +02:00

89 lines
1.7 KiB
Plaintext

== Why is this an issue?
The order in which you ``++close++`` database-releated resources is crucial. Close a ``++Connection++`` first, and depending on the database pooling in use, you may no longer be able to truly reach its ``++Statement++``s and ``++ResultSet++``s to close them, even though the calls are made and execute without error.
=== Noncompliant code example
[source,java]
----
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
try {
conn = DriverManager.getConnection(connectionString);
ps = conn.prepareStatement(query);
rs = ps.executeQuery();
// ...
} finally {
try {
if (conn != null) {
conn.close(); // Noncompliant; close this last
}
} catch (Exception e) {};
try {
if (ps != null) {
ps.close();
}
} catch (Exception e) {};
try {
if (rs != null) {
rs.close();
}
} catch (Exception e) {};
}
----
=== Compliant solution
[source,java]
----
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
try {
conn = DriverManager.getConnection(connectionString);
ps = conn.prepareStatement(query);
rs = ps.executeQuery();
// ...
} finally {
try {
if (rs != null) {
rs.close();
}
} catch (Exception e) {};
try {
if (ps != null) {
ps.close();
}
} catch (Exception e) {};
try {
if (conn != null) {
conn.close();
}
} catch (Exception e) {};
}
----
ifdef::env-github,rspecator-view[]
'''
== Implementation Specification
(visible only on this page)
=== Message
Close this "Connection" last.
'''
== Comments And Links
(visible only on this page)
=== on 16 Jun 2015, 17:28:24 Ann Campbell wrote:
CodePro: Close Order
endif::env-github,rspecator-view[]