Skip to content

Commit 23ae8d2

Browse files
committed
HHH-14694 Test proxy class reuse between SessionFactories within same classloader
1 parent 702d154 commit 23ae8d2

File tree

1 file changed

+81
-0
lines changed

1 file changed

+81
-0
lines changed
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
/*
2+
* Hibernate, Relational Persistence for Idiomatic Java
3+
*
4+
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
5+
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
6+
*/
7+
package org.hibernate.orm.test.proxy;
8+
9+
import jakarta.persistence.Entity;
10+
import jakarta.persistence.Id;
11+
import org.hibernate.boot.MetadataSources;
12+
import org.hibernate.boot.registry.StandardServiceRegistry;
13+
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
14+
import org.hibernate.bytecode.internal.bytebuddy.BytecodeProviderImpl;
15+
import org.hibernate.bytecode.spi.BytecodeProvider;
16+
import org.hibernate.engine.spi.SessionFactoryImplementor;
17+
import org.hibernate.engine.spi.SessionImplementor;
18+
import org.hibernate.testing.orm.junit.Jira;
19+
import org.hibernate.testing.util.ServiceRegistryUtil;
20+
import org.junit.jupiter.api.Test;
21+
22+
import java.util.function.Function;
23+
24+
import static org.junit.jupiter.api.Assertions.assertNotSame;
25+
import static org.junit.jupiter.api.Assertions.assertSame;
26+
27+
@Jira("https://hibernate.atlassian.net/browse/HHH-14694")
28+
public class ProxyClassReuseTest {
29+
30+
@Test
31+
void testReuse() {
32+
Function<SessionFactoryImplementor, Class<?>> proxyGetter = sf -> {
33+
try (SessionImplementor s = sf.openSession()) {
34+
return s.getReference( MyEntity.class, "abc" ).getClass();
35+
}
36+
};
37+
final BytecodeProvider bytecodeProvider = new BytecodeProviderImpl();
38+
Class<?> proxyClass1 = withFactory( proxyGetter, bytecodeProvider );
39+
Class<?> proxyClass2 = withFactory( proxyGetter, bytecodeProvider );
40+
assertSame( proxyClass1, proxyClass2 );
41+
}
42+
43+
@Test
44+
void testNoReuse() {
45+
Function<SessionFactoryImplementor, Class<?>> proxyGetter = sf -> {
46+
try (SessionImplementor s = sf.openSession()) {
47+
return s.getReference( MyEntity.class, "abc" ).getClass();
48+
}
49+
};
50+
Class<?> proxyClass1 = withFactory( proxyGetter, null );
51+
Class<?> proxyClass2 = withFactory( proxyGetter, null );
52+
assertNotSame( proxyClass1, proxyClass2 );
53+
}
54+
55+
<T> T withFactory(Function<SessionFactoryImplementor, T> consumer, BytecodeProvider bytecodeProvider) {
56+
final StandardServiceRegistryBuilder builder = ServiceRegistryUtil.serviceRegistryBuilder();
57+
if ( bytecodeProvider != null ) {
58+
builder.addService( BytecodeProvider.class, bytecodeProvider );
59+
}
60+
final StandardServiceRegistry ssr = builder.build();
61+
62+
try (final SessionFactoryImplementor sf = (SessionFactoryImplementor) new MetadataSources( ssr )
63+
.addAnnotatedClass( MyEntity.class )
64+
.buildMetadata()
65+
.getSessionFactoryBuilder()
66+
.build()) {
67+
return consumer.apply( sf );
68+
}
69+
catch ( Exception e ) {
70+
StandardServiceRegistryBuilder.destroy( ssr );
71+
throw e;
72+
}
73+
}
74+
75+
@Entity(name = "MyEntity")
76+
public static class MyEntity {
77+
@Id
78+
String id;
79+
String name;
80+
}
81+
}

0 commit comments

Comments
 (0)