-
Notifications
You must be signed in to change notification settings - Fork 39
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
Close throws Exception #108
Comments
Sounds reasonable to me to use JsonbException and only impacts implementations in terms of breaking change so it looks "ok". Regarding your second question: implementations can use this method for anything, CDI being the most common case and default one only so better to always call it IMHO. |
I understand that currently, the spec allows for implementers to leave “resources” open during the lifetime of a jsonb instance and expect that the user will close the jsonb instance. And therefore, the user has to close the jsonb instances. I wonder if a change in the spec could be considered: giving one more guarantee to the user, namely, that the jsonb instance does not have to be closed in non CDI-environments. I realize that, as usual, giving supplementary guarantees to the user in the spec comes with the price of a supplementary constraint to implementors. And therefore, in general, possible lowering of quality of service or difficulty in ensuring that the implementation is correct. But in this case, having to close instances is a burden to the user that I suspect is not justified by related improvements in terms of implementation performance or ease of implementation. That is because I hardly see why an implementor would significantly benefit from being able to use system resources or anything similar that needs to be closed when the work with a jsonb instance is done. I could be wrong of course, even more so that I am not an implementor of the jsonb spec. In any case, making close throw a specific exception would already be a great improvement, IMHO. |
You can't assume that by a lot of integration - any IoC integration will require that constraint, some memory optimization will require it as well, some GC enablement require it even in plain SE too. Note that the autocloseable usage makes this "burden" very low in all trivial SE cases and in all other cases you have a kind of lifecycle where close is mandatory so it is good to keep it IMHO and less misleading for libraries and users than putting some rules like "if you don't use anything you can skip this method" IMHO . On the exception side if I get it right your request is not a JsonbException but a RuntimeException to not catch it - just to clarify the goal. I agree with you that being able to use method reference would be nice and this does not break user code to do it as you propose so +1. |
Well, I am happy to leave the last word to you on that matter. But just for my own curiosity, I must say I do not understand the argument, because I do not see anything specific to JSON-B that justifies that it must be closed, at least if we exclude CDI related matters (and see below about CDI). By that reasoning, if I understand it correctly, it seems like any library providing any kind of service should require the user to close it when finished. Fortunately, this principle does not seem to be usually followed. Consider JAXB as an example. Although it is a complex spec that involves a great variety of complex objects and memory burden in general, as far as I recall, (almost?) nothing must be closed by the user when using JAXB. See the various examples in Unmarshaller. And now that I think about it, even when using CDI injected managed beans, I am not sure I understand the requirement to close jsonb instances. CDI already defines the lifecycle of the managed beans, and thus it is already specified when to stop their life (for example, at the end of a session for a @SessionScoped bean). Won’t this conflict with the requirement of stopping their life when a jsonb instance is closed (if this is what the documentation in the close method suggests)?
I suppose there is some subjectivity here. It does not hurt me as it is common in the Java world.
Not sure I understand what you mean? JsonbException is a |
@oliviercailloux JAXB does not have very advanced implementations so the memory and so on issue does not appear AFAIK. However the issue exists trivially if you use CDI - or any other resource related impl - in adapters and yes it leaks today without workarounds in apps. The CDI requirement is when the instances are @dependent, JSON-B will create a creational context and will need to release it when no more needed - to destroy transitive dependent beans etc. In general JSON-B must provide a hook to release all needed resources used by user instances during the runtime. CDI is just here by default and can be implicit which is why a library can't bypass close if it aims to be used in any CDI/EE container. The point about the exception is that JsonbException is not that important since when call close you catch or not the exception but you don't have any other options so whatever the type is it will match your original need which is to not catch it in general to meet java method reference syntax. Just wanted to highlight that point cause I think it is the way to express it in the spec rather than just saying it is a JsonbException so maybe "close method is overriden to throw a RuntimeException of type JsonbException so if you want to handle potential errors you need to catch it yourself but it enables you to use method reference to call it". Do you want to try to do a PR on the spec in that direction? |
(The CDI spec says: “Finally, the container is permitted to destroy any @dependent scoped contextual instance at any time if the instance is no longer referenced by the application”. I’d have to think more about all this… And I am still slightly worried that this reasoning seems to apply to everything, and thus seems to lead to having to systematically close everything we use that is provided by any library.) But let’s cut the discussion short, in any case this was not the main point of this issue. (Thanks for your patience anyway.) Yes, to summarize, I think we agree that overriding the close method in the I am sorry, what is a PR and how should I proceed? |
Yes but permitted != does ;) A PR = pull request. You "fork" the spec repository to get your own copy, do the change then hit "create a pull request" and follow the form github will propose you. It will propose the change to the spec and make easier/concrete the review compared to a discussion thread. |
Signed-off-by: Olivier Cailloux <[email protected]>
Ah, yes, I know what a pull request is, it just didn’t occur to me that PR was referring to that in this context. Done. (As you’ve seen already.) |
Closing this issue as it appears to be complete |
The corresponding PR (#109) is stalled since a long time. Anyway, I have re-read the whole thread and thought again about this issue. I hope it is appropriate to come back to it here (please tell me if I should rather open a new issue). I’d like to insist with my proposition that the spec be slightly changed to mention that it is safe to omit calling close() when the user does not use adapters, serializers or deserializers that use CDI injection of beans under a “dependent” scope. Here is my justification for this proposition.
I note that rmannibucau disagrees with some of the above points.
I read this point as arguing against my item 4. But the point seems too vague to justify a requirement to close instances on the part of the user, and is at least partly countered by 4. c). A specific example would certainly help, if possible.
This is probably subjective, but strikes me as a very unconsensual statement. Even with the modern try-with-resources syntax, having to close instances explicitly rather than let them be garbage collected automatically is a pain. I hope that my proposition can be seriously considered, even if rmannibucau still disagrees with some of the more subjective aspects of the discussion. I think it would help Json B gain traction in the Java SE world. (Which I would consider nice because, first, a standard with a spec is better than multiple incompatible implementations; and second, after having used both Jackson and Gson, I find the Json B approach more elegant on multiple aspects.) |
It is unclear to me why this issue has been closed, as it is not complete. Should I open a new one? |
Not sure why it was closed but to comment why close must be called (your previous comment) here are some inline inputs:
No, as soon as you create a JSON-B instance, even without any CDI instance nor IoC you have to call close because the impl can require it (
Don't forget the bean can be a CDI bean without any injection (but to use the scope or be injected itself in another bean which is not JSON-B related), this is per spec as of today - but once again
This statement is right but unrelated to
You can omit it if you don't write a portable code at all and know your implementation in the version you run.
Depends, this is generally not true but to be concrete the case you reference there works because it uses |
Can you please give me a concrete example of what you mean? Or an article that discusses this? Or perhaps, is there a current implementation of JSON-B that does release some resources not related to IoC when calling close(), with some documentation about it? |
@oliviercailloux not sure there are much public cases but here are the cases I encountered - but once again the API to be portable needs it even without a concrete case:
Indeed, |
Thank you for these clear and concrete examples.
I agree that such approach would make the spec too fuzzy and would be undesirable. So I no longer wish to remove the requirement for closing altogether. (However I still suspect that a better exception management is desirable, which was the initial point of this thread on which we got stuck some time ago. I’ll come back to it soon if I have not exhausted your patience.) |
Still agree but we need to take care of the backward compatibility, we had some bad time changing some internal exception stacks in johnzon even if we made it (and exception stack are not validated by static tools at all so it is can be silent breaking change at build time). |
I just commented at #110: I thought that it’s best to comment there as the more technical discussion happened there already. Please tell me if this is inappropriate. |
AutoCloseable::close
documentation mentions: “While this interface method is declared to throw Exception, implementers are strongly encouraged to declare concrete implementations of the close method to throw more specific exceptions, or to throw no exception at all if the close operation cannot fail.”Would you consider modifying the declaration of close() in
Jsonb::close
(currently not declared thus inheriting fromAutoCloseable
) as throwingJsonbException
?Relatedly, the documentation of
Jsonb
mentions: “Calling Closable.close() method will cleanup all CDI managed components (such as adapters with CDI dependencies) created during interaction with Jsonb.” Can I conclude that, if using Jsonb in Java SE (and no CDI), it is safe to not close the jsonb instances I create? If so, would you consider documenting that fact?The text was updated successfully, but these errors were encountered: