Skip to content

Conversation

@Pankraz76
Copy link

@Pankraz76 Pankraz76 commented Jun 13, 2025

Java 7 introduced the try-with-resources statement. This statement ensures that each resource is closed at the end of the statement. It avoids the need of explicitly closing the resources in a finally block. Additionally exceptions are better handled: If an exception occurred both in the try block and finally block, then the exception from the try block was suppressed. With the try-with-resources statement, the exception thrown from the try-block is preserved.

image

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged or decided on label Jun 13, 2025
@Pankraz76 Pankraz76 force-pushed the use-try-with-resources-statement- branch from 4e2e1be to 9aa6812 Compare June 13, 2025 09:52
Signed-off-by: Vincent Potucek <vpotucek@me.com>
@Pankraz76 Pankraz76 force-pushed the use-try-with-resources-statement- branch from 9aa6812 to d8d4b6e Compare June 13, 2025 09:54
Copy link
Member

@bclozel bclozel left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for raising this, but as this review shows, these stylistic changes are mostly breaking the behavior or adding little value.

continue;
}
InputStream is = cl.getResourceAsStream(c.getName().replace('.', '/') + ".class");
if (is == null) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please don't change files in the cglib package, this part is manually shaded into our repository and we shouldn't change the source if we want to easily keep up with the original.

Set bridges = (Set) entry.getValue();
try {
InputStream is = classLoader.getResourceAsStream(owner.getName().replace('.', '/') + ".class");
if (is == null) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same

name.replace('.','/') + ".class"
);

if (is == null) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same

}
return size;
}
finally {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please revert. The exception thrown while closing the stream is not caught/logged anymore.
See this message for a discussion on possible workarounds. I think we should keep the current code.

try {
con.close();
}
catch (SQLException ex) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same. Please revert.

}
try {
handleListenerException(ex);
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please revert this new line change.

if (ex instanceof JMSException jmsException) {
throw jmsException;
}
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please revert this new line change.

TransactionSynchronizationManager.unbindResource(obtainConnectionFactory());
}
observation.stop();
scope.close();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not a neutral change. The scope is now closed before the observation is stopped and the transaction finished. This will change the behavior and will have unintended consequences.
Please revert.

in.close();
}
catch (Throwable ex) {
// ignore, see SPR-12999
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please revert. This will catch all Throwable thrown by the code block, not just when the resource is closed.

responseHeaders.setContentLength(rangeLength);

InputStream in = region.getResource().getInputStream();
// We cannot use try-with-resources here for the InputStream, since we have
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is literally called out in this comment...

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes need to stay open i know this behavior from maven. should give more detail then as ignored exc for good reason.

@bclozel
Copy link
Member

bclozel commented Jun 13, 2025

Given the state of this PR, I don't think it's worth spending time on this - we're more likely to break behavior than anything.

@bclozel bclozel closed this Jun 13, 2025
@bclozel bclozel added status: declined A suggestion or change that we don't feel we should currently apply and removed status: waiting-for-triage An issue we've not yet triaged or decided on labels Jun 13, 2025
@Pankraz76
Copy link
Author

Pankraz76 commented Jun 13, 2025

yes of course will SOC. if this changes anything its a interesting to invest, as its a valid feature used all around already in this code base:

@Pankraz76
Copy link
Author

Pankraz76 commented Jun 13, 2025

if can not use java like supposed to, it seems smell.

Copy link
Author

@Pankraz76 Pankraz76 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thx. for detailed review. yes this need special attention.

responseHeaders.setContentLength(rangeLength);

InputStream in = region.getResource().getInputStream();
// We cannot use try-with-resources here for the InputStream, since we have
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes need to stay open i know this behavior from maven. should give more detail then as ignored exc for good reason.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

status: declined A suggestion or change that we don't feel we should currently apply

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants