Skip to content

Commit 638b5c7

Browse files
committed
HHH-17887 Add test for issue
1 parent 239014d commit 638b5c7

File tree

1 file changed

+129
-0
lines changed

1 file changed

+129
-0
lines changed
Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
package org.hibernate.orm.test.connections;
2+
3+
import java.sql.Connection;
4+
import java.sql.SQLException;
5+
import java.util.List;
6+
import java.util.concurrent.atomic.AtomicInteger;
7+
8+
import org.hibernate.cfg.AvailableSettings;
9+
import org.hibernate.engine.jdbc.connections.spi.ConnectionProvider;
10+
11+
import org.hibernate.testing.jta.JtaAwareConnectionProviderImpl;
12+
import org.hibernate.testing.orm.junit.DomainModel;
13+
import org.hibernate.testing.orm.junit.JiraKey;
14+
import org.hibernate.testing.orm.junit.ServiceRegistry;
15+
import org.hibernate.testing.orm.junit.SessionFactory;
16+
import org.hibernate.testing.orm.junit.SessionFactoryScope;
17+
import org.hibernate.testing.orm.junit.Setting;
18+
import org.junit.jupiter.api.BeforeAll;
19+
import org.junit.jupiter.api.Test;
20+
21+
import jakarta.persistence.Entity;
22+
import jakarta.persistence.Id;
23+
24+
import static org.assertj.core.api.Assertions.assertThat;
25+
import static org.junit.jupiter.api.Assertions.assertTrue;
26+
27+
@DomainModel(
28+
annotatedClasses = {
29+
ConnectionCloseAfterStatementTest.TestEntity.class
30+
}
31+
32+
)
33+
@ServiceRegistry(
34+
settings = {
35+
@Setting(name = AvailableSettings.CONNECTION_HANDLING, value = "DELAYED_ACQUISITION_AND_RELEASE_AFTER_STATEMENT"),
36+
@Setting(name = AvailableSettings.CONNECTION_PROVIDER, value = "org.hibernate.orm.test.connections.ConnectionCloseAfterStatementTest$JtaAwareConnectionProvider"),
37+
@Setting(name = AvailableSettings.JPA_TRANSACTION_TYPE, value = "JTA")
38+
}
39+
)
40+
@SessionFactory
41+
@JiraKey("HHH-17887")
42+
public class ConnectionCloseAfterStatementTest {
43+
44+
@BeforeAll
45+
public void setUp(SessionFactoryScope scope) {
46+
scope.inTransaction(
47+
session -> {
48+
TestEntity testEntity = new TestEntity( 1l, "test" );
49+
session.persist( testEntity );
50+
}
51+
);
52+
}
53+
54+
@Test
55+
public void testConnectionClosedAfterFind(SessionFactoryScope scope) {
56+
scope.inTransaction(
57+
session -> {
58+
TestEntity testEntity = session.find( TestEntity.class, 1l );
59+
assertThat( testEntity ).isNotNull();
60+
assertTrue( getConnectionProvider(scope).areAllConnectionClosed() );
61+
}
62+
);
63+
}
64+
65+
@Test
66+
public void testConnectionClosedAfterResultList(SessionFactoryScope scope) {
67+
scope.inTransaction(
68+
session -> {
69+
List<TestEntity> testEntity = session.createQuery( "select t from TestEntity t", TestEntity.class )
70+
.getResultList();
71+
assertThat( testEntity.size() ).isEqualTo( 1 );
72+
assertTrue( getConnectionProvider(scope).areAllConnectionClosed() );
73+
}
74+
);
75+
}
76+
77+
@Test
78+
public void testConnectionClosedAfterSingleResult(SessionFactoryScope scope) {
79+
scope.inTransaction(
80+
session -> {
81+
session.createQuery( "select t from TestEntity t", TestEntity.class )
82+
.getSingleResult();
83+
assertTrue( getConnectionProvider(scope).areAllConnectionClosed() );
84+
}
85+
);
86+
}
87+
88+
@Entity(name = "TestEntity")
89+
public static class TestEntity {
90+
@Id
91+
private Long id;
92+
93+
private String name;
94+
95+
public TestEntity() {
96+
}
97+
98+
public TestEntity(Long id, String name) {
99+
this.id = id;
100+
this.name = name;
101+
}
102+
}
103+
104+
private JtaAwareConnectionProvider getConnectionProvider(SessionFactoryScope scope) {
105+
return (JtaAwareConnectionProvider) scope.getSessionFactory().getServiceRegistry()
106+
.getService( ConnectionProvider.class );
107+
}
108+
109+
public static class JtaAwareConnectionProvider extends JtaAwareConnectionProviderImpl {
110+
private final AtomicInteger connectionOpenEventCount = new AtomicInteger();
111+
112+
@Override
113+
public Connection getConnection() throws SQLException {
114+
this.connectionOpenEventCount.incrementAndGet();
115+
return super.getConnection();
116+
}
117+
118+
@Override
119+
public void closeConnection(Connection connection) throws SQLException {
120+
this.connectionOpenEventCount.decrementAndGet();
121+
super.closeConnection( connection );
122+
}
123+
124+
public boolean areAllConnectionClosed() {
125+
return connectionOpenEventCount.get() == 0;
126+
}
127+
128+
}
129+
}

0 commit comments

Comments
 (0)