== 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[]
