-
-
Notifications
You must be signed in to change notification settings - Fork 3.7k
HHH-12986 ConfigLoader does not close file when loading hibernate.cfg.xml #11165
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
Open
mehmetali2003
wants to merge
2
commits into
hibernate:main
Choose a base branch
from
mehmetali2003:HHH-12986
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+159
−3
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
110 changes: 110 additions & 0 deletions
110
...-core/src/test/java/org/hibernate/orm/test/boot/cfgXml/CfgXmlResourceFileClosingTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,110 @@ | ||
| /* | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| * Copyright Red Hat Inc. and Hibernate Authors | ||
| */ | ||
| package org.hibernate.orm.test.boot.cfgXml; | ||
|
|
||
| import org.hibernate.boot.cfgxml.internal.ConfigLoader; | ||
| import org.hibernate.boot.registry.BootstrapServiceRegistry; | ||
| import org.hibernate.boot.registry.BootstrapServiceRegistryBuilder; | ||
| import org.hibernate.boot.registry.StandardServiceRegistry; | ||
| import org.hibernate.boot.registry.StandardServiceRegistryBuilder; | ||
| import org.hibernate.boot.registry.classloading.internal.ClassLoaderServiceImpl; | ||
| import org.hibernate.testing.junit4.BaseUnitTestCase; | ||
| import org.hibernate.testing.orm.junit.JiraKey; | ||
| import org.hibernate.testing.util.ServiceRegistryUtil; | ||
| import org.junit.Test; | ||
|
|
||
| import java.io.File; | ||
| import java.io.IOException; | ||
| import java.io.InputStream; | ||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
|
|
||
| import static org.junit.Assert.assertTrue; | ||
|
|
||
| /** | ||
| * Test that makes sure the input stream inside {@link ConfigLoader#loadConfigXmlFile(File)} | ||
| * gets closed. | ||
| * | ||
| * @author Mehmet Ali Emektar(inspired from CfgXmlResourceNameClosingTest) | ||
| */ | ||
| @JiraKey( value = "HHH-12986" ) | ||
| public class CfgXmlResourceFileClosingTest extends BaseUnitTestCase { | ||
| private static class InputStreamWrapper extends InputStream { | ||
| private final InputStream wrapped; | ||
| private boolean wasClosed = false; | ||
|
|
||
| public InputStreamWrapper(InputStream wrapped) { | ||
| this.wrapped = wrapped; | ||
| } | ||
|
|
||
| @Override | ||
| public int read() throws IOException { | ||
| return wrapped.read(); | ||
| } | ||
|
|
||
| @Override | ||
| public void close() throws IOException { | ||
| wrapped.close(); | ||
| wasClosed = true; | ||
| super.close(); | ||
| } | ||
|
|
||
| public boolean wasClosed() { | ||
| return wasClosed; | ||
| } | ||
| } | ||
|
|
||
| private static class LocalClassLoaderServiceImpl extends ClassLoaderServiceImpl { | ||
| final List<InputStreamWrapper> openedStreams = new ArrayList<InputStreamWrapper>(); | ||
| boolean stopped = false; | ||
|
|
||
| @Override | ||
| public InputStream locateResourceStream(String name) { | ||
| InputStreamWrapper stream = new InputStreamWrapper( super.locateResourceStream( name ) ); | ||
| openedStreams.add( stream ); | ||
| return stream; | ||
| } | ||
|
|
||
| @Override | ||
| public void stop() { | ||
| for ( InputStreamWrapper openedStream : openedStreams ) { | ||
| if ( !openedStream.wasClosed ) { | ||
| try { | ||
| openedStream.close(); | ||
| } | ||
| catch (IOException ignore) { | ||
| } | ||
| } | ||
| } | ||
| openedStreams.clear(); | ||
| stopped = true; | ||
| super.stop(); | ||
| } | ||
| } | ||
|
|
||
| LocalClassLoaderServiceImpl classLoaderService = new LocalClassLoaderServiceImpl(); | ||
|
|
||
| @Test | ||
| public void testStreamClosing() { | ||
| String filePath = "src/test/java/org/hibernate/orm/test/boot/cfgXml/hibernate.cfg.xml"; | ||
| File cfgXmlFile = new File(filePath); | ||
| BootstrapServiceRegistry bsr = new BootstrapServiceRegistryBuilder() | ||
| .applyClassLoaderService( classLoaderService ) | ||
| .build(); | ||
| StandardServiceRegistry ssr = ServiceRegistryUtil.serviceRegistryBuilder( bsr ) | ||
| .configure( cfgXmlFile) | ||
| .build(); | ||
| try { | ||
| for ( InputStreamWrapper openedStream : classLoaderService.openedStreams ) { | ||
| assertTrue( openedStream.wasClosed ); | ||
| } | ||
| } | ||
| finally { | ||
| StandardServiceRegistryBuilder.destroy( ssr ); | ||
| } | ||
|
|
||
| assertTrue( classLoaderService.stopped ); | ||
| } | ||
| } |
30 changes: 30 additions & 0 deletions
30
hibernate-core/src/test/java/org/hibernate/orm/test/boot/cfgXml/hibernate.cfg.xml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| <!-- | ||
| ~ SPDX-License-Identifier: Apache-2.0 | ||
| ~ Copyright Red Hat Inc. and Hibernate Authors | ||
| --> | ||
| <!DOCTYPE hibernate-configuration PUBLIC | ||
| "-//Hibernate/Hibernate Configuration DTD 3.0//EN" | ||
| "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> | ||
|
|
||
| <hibernate-configuration> | ||
| <session-factory> | ||
| <property name="hibernate.dialect">org.hibernate.dialect.HSQLDialect</property> | ||
| <property name="hibernate.connection.driver_class">org.hsqldb.jdbcDriver</property> | ||
| <property name="hibernate.connection.username">sa</property> | ||
| <property name="hibernate.connection.password"></property> | ||
| <property name="hibernate.connection.url">jdbc:hsqldb:.</property> | ||
| <property name="hibernate.cache.use_query_cache">true</property> | ||
| <property name="hibernate.cache.region_prefix">hibernate.test</property> | ||
| <property name="hibernate.jdbc.batch_size">0</property> | ||
| <property name="hibernate.max_fetch_depth">3</property> | ||
| <property name="hibernate.hbm2ddl.auto">create-drop</property> | ||
| <property name="hibernate.generate_statistics">true</property> | ||
| <property name="hibernate.cache.region.factory_class">org.hibernate.testing.cache.CachingRegionFactory</property> | ||
| <mapping class="org.hibernate.orm.test.jpa.Item"/> | ||
| <mapping class="org.hibernate.orm.test.jpa.Cat"/> | ||
| <mapping class="org.hibernate.orm.test.jpa.Kitten"/> | ||
| <mapping class="org.hibernate.orm.test.jpa.Distributor"/> | ||
| <class-cache class="org.hibernate.orm.test.jpa.Item" usage="read-write"/> | ||
| <collection-cache collection="org.hibernate.orm.test.jpa.Item.distributors" usage="read-write" region="RegionName"/> | ||
| </session-factory> | ||
| </hibernate-configuration> |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
what about using the auto-closable?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It seems reasonable.