Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve Cursor Closeable behavior #1494

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

hutteman
Copy link

@hutteman hutteman commented Mar 7, 2019

See https://groups.google.com/forum/#!topic/mybatis-user/PzTPldYdThA

The Cursor interface extends Closeable, enabling its usage in a try-with-resources construct.

Unfortunately by not explicitly overriding Closeable.close(), any try-with-resources over a Cursor forces developers to have to catch IOException, even though this isn't thrown by DefaultCursor, nor would it make much sense in any future Cursor implementation.

This change adds the below to the Cursor interface to make them easier to work with in try-with-resources :

    @Override
    void close();

It also changes Cursor to override AutoCloseable rather than Closeable, as suggested by Iwao on the google groups thread.

Technically, this is a breaking change, as existing code that either explicitly or implicitly calls Cursor.close() and currently catches IOException (even though it will never be thrown) will likely need to remove that catch-block or fail compilation. This is an easy change to make during the upgrade to the new version however, and will simplify their code and that of any future users of Cursor.

By the way the javadocs for AutoCloseable.close() explicitly mention this:

     * <p>While this interface method is declared to throw {@code
     * Exception}, implementers are <em>strongly</em> encouraged to
     * declare concrete implementations of the {@code close} method to
     * throw more specific exceptions, or to throw no exception at all
     * if the close operation cannot fail.

…erride close() to no longer throw IOException
@hutteman hutteman changed the title Change Cursor to implement AutoCloseable instead of Closeable, and override close() to no longer throw IOException Improve Cursor Closeable behavior Mar 7, 2019
@kazuki43zoo
Copy link
Member

@hutteman Thanks for your contribution!!

I think this PR is good and reasonable improvement. However this changes has cases that broken backward-compatibility at compile time. Therefore I think PR should merge at next minor(3.6) or major(4.0) version-up.

  • If catch the IOException, it is broken.
  • If throws the IOException, it is not broken.

e.g.

@Test
public void closeTryWithResourcesWithCatch() {
  try (Cursor<Object> c = mapper.select()) {
    for (Object o : c) {
      c.isOpen();
    }
  } catch (IOException e) { // this code is broken
    e.printStackTrace();
  }
}


@Test
public void closeTryWithResourcesWithThrows() throws IOException { // this code is not broken
  try (Cursor<Object> c = mapper.select()) {
    for (Object o : c) {
      c.isOpen();
    }
  }
}

@kazuki43zoo kazuki43zoo added enhancement Improve a feature or add a new feature no backward compatibility Includes change no backward compatibility for previous version labels Mar 7, 2019
@harawata
Copy link
Member

harawata commented Mar 8, 2019

Thank you, @hutteman !

@kazuki43zoo ,
It is true that this breaks backward compatibility and that's big for sure.
But I just thought this would be a change that makes users happy even though it might surprise them at first.
I mean, if I were using Cursor, I would be dying to eliminate the unnecessary catch or throws! ;D

Anyway, as you suggested, let's plan this for the next major release for now.

@kazuki43zoo kazuki43zoo added this to the 3.6.x milestone Apr 1, 2020
@GeorgeSalu
Copy link

How about thinking about a 3.6.x version? It's been more than 4 years since version 3.5.x @harawata @kazuki43zoo

@hazendaz
Copy link
Member

hazendaz commented Dec 16, 2022 via email

@GeorgeSalu
Copy link

Yes, I also think upgrading to Java 11 would be good, are there any features rodmaps that we can think of for this new version? I think thinking about Ahead-of-Time (AOT) in a 3.6.x or 4 version would be a good idea at least an initial implementation is a move that many are making

@hazendaz
Copy link
Member

@GeorgeSalu Head over to discussions tab, I have created a discussion for mybatis 3.6.0. Let that server as the discussion point for any wants to the framework. I have listed this specific item as part of that but feel free to bring up AOT there. I've not worked with that myself so we would probably expect community help in laying out the roadmap and contributing to it. Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement Improve a feature or add a new feature no backward compatibility Includes change no backward compatibility for previous version
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants