title: Why doesn't this code close JDBC connections? (Java 7 Autocloseable unexpected behavior)
body: <p>Using Java 7u5, with the try-with-resources construct, the following code <strong>appears to leak jdbc connections</strong>:</p>
  
  <pre><code>try (Connection connection = ..; PreparedStatement stmt = ..) {
      stmt.setString(..);
      return stmt.executeUpdate() &gt; 0;
  }
  </code></pre>
  
  <p>The next piece of code works as expected and intended:</p>
  
  <pre><code>int ret = 0;
  
  try (Connection connection = ..; PreparedStatement stmt = ..) {
      stmt.setString(..);
      ret = stmt.executeUpdate();
  }
  
  return ret &gt; 0;
  </code></pre>
  
  <p>It seems that in the first case, the <code>Connection.close()</code> method is not being invoked.</p>
  
  <p>I am using the latest mysql connector. This is unexpected behavior, correct?</p>
  
  <h2>Test</h2>
  
  <p>The following test <strong>will NOT</strong> print <code>CLOSED</code>:</p>
  
  <pre><code>public class Test implements AutoCloseable {
  
  public static void main(String[] args) throws Exception {
      System.out.println(doTest());
  }
  
  private static boolean doTest() throws Exception {
      try (Test test = new Test()) {
          return test.execute() &gt; 0;
      }
  
  }
  
  private int execute() {
      return 1;
  }
  
  @Override
  public void close() throws Exception {
      System.out.println("CLOSED");
  }
  }
  </code></pre>
  
  <p>Strangely, if <code>execute()</code> is modified to <code>return 0;</code> then <code>CLOSED</code> <strong>WILL</strong> be printed.</p>
  
  <h3>javap -p -c Test.class output</h3>
  
  <pre><code>    Compiled from "Test.java"
  public class Test implements java.lang.AutoCloseable {
    public Test();
      Code:
         0: aload_0
         1: invokespecial #10                 // Method java/lang/Object."&lt;init&gt;":()V
         4: return
  
    public static void main(java.lang.String[]) throws java.lang.Exception;
      Code:
         0: getstatic     #21                 // Field java/lang/System.out:Ljava/io/PrintStream;
         3: invokestatic  #27                 // Method doTest:()Z
         6: invokevirtual #31                 // Method java/io/PrintStream.println:(Z)V
         9: return
  
    private static boolean doTest() throws java.lang.Exception;
      Code:
         0: aconst_null
         1: astore_0
         2: aconst_null
         3: astore_1
         4: new           #1                  // class Test
         7: dup
         8: invokespecial #39                 // Method "&lt;init&gt;":()V
        11: astore_2
        12: aload_2
        13: invokespecial #40                 // Method execute:()I
        16: ifle          21
        19: iconst_1
        20: ireturn
        21: iconst_0
        22: aload_2
        23: ifnull        30
        26: aload_2
        27: invokevirtual #44                 // Method close:()V
        30: ireturn
        31: astore_0
        32: aload_2
        33: ifnull        40
        36: aload_2
        37: invokevirtual #44                 // Method close:()V
        40: aload_0
        41: athrow
        42: astore_1
        43: aload_0
        44: ifnonnull     52
        47: aload_1
        48: astore_0
        49: goto          62
        52: aload_0
        53: aload_1
        54: if_acmpeq     62
        57: aload_0
        58: aload_1
        59: invokevirtual #47                 // Method java/lang/Throwable.addSuppressed:(Ljava/lang/Throwable;)V
        62: aload_0
        63: athrow
      Exception table:
         from    to  target type
            12    22    31   any
            30    31    31   any
             4    42    42   any
  
    private int execute();
      Code:
         0: iconst_1
         1: ireturn
  
    public void close() throws java.lang.Exception;
      Code:
         0: getstatic     #21                 // Field java/lang/System.out:Ljava/io/PrintStream;
         3: ldc           #55                 // String CLOSED
         5: invokevirtual #57                 // Method java/io/PrintStream.println:(Ljava/lang/String;)V
         8: return
  }
  </code></pre>