Skip to content

Commit a8a7f39

Browse files
committed
HHH-14694 Test proxy class reuse between SessionFactories within same classloader
1 parent dc20229 commit a8a7f39

File tree

1 file changed

+79
-0
lines changed

1 file changed

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

0 commit comments

Comments
 (0)