Skip to content

Commit 5ca21e6

Browse files
committed
HHH-19708 add a simple test
1 parent 04fdf27 commit 5ca21e6

File tree

1 file changed

+117
-0
lines changed

1 file changed

+117
-0
lines changed
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
/*
2+
* SPDX-License-Identifier: Apache-2.0
3+
* Copyright Red Hat Inc. and Hibernate Authors
4+
*/
5+
package org.hibernate.orm.test.readonly;
6+
7+
import jakarta.persistence.Entity;
8+
import jakarta.persistence.Id;
9+
import org.hibernate.Session;
10+
import org.hibernate.StatelessSession;
11+
import org.hibernate.cfg.JdbcSettings;
12+
import org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProvider;
13+
import org.hibernate.testing.orm.junit.DomainModel;
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.Test;
19+
20+
import java.sql.Connection;
21+
import java.sql.SQLException;
22+
23+
import static org.junit.jupiter.api.Assertions.assertThrows;
24+
import static org.junit.jupiter.api.Assertions.assertTrue;
25+
26+
@SessionFactory
27+
@ServiceRegistry(settings = @Setting(name = JdbcSettings.CONNECTION_PROVIDER,
28+
value = "org.hibernate.orm.test.readonly.ReplicasTest$Provider" ))
29+
@DomainModel(annotatedClasses = ReplicasTest.Thing.class)
30+
class ReplicasTest {
31+
@Test void testStateful(SessionFactoryScope scope) {
32+
readOnlyConnectionsOpened = 0;
33+
readOnlyConnectionsClosed = 0;
34+
var factory = scope.getSessionFactory();
35+
factory.getSchemaManager().truncate();
36+
Session s1 =
37+
factory.withOptions()
38+
// .connectionHandling( AS_NEEDED, ON_CLOSE )
39+
.readOnly(true)
40+
.openSession();
41+
s1.inTransaction( tx -> {
42+
assertThrows( IllegalStateException.class,
43+
() -> s1.persist( new Thing() ) );
44+
assertThrows( IllegalStateException.class,
45+
() -> s1.merge( new Thing() ) );
46+
} );
47+
s1.close();
48+
scope.inTransaction(s -> s.persist(new Thing()));
49+
Session s2 =
50+
factory.withOptions().readOnly(true)
51+
.openSession();
52+
s2.inTransaction( tx -> {
53+
Thing thing = s2.find( Thing.class, 2L );
54+
assertTrue( s2.isReadOnly( thing ) );
55+
assertThrows( IllegalStateException.class,
56+
() -> s1.remove( thing ) );
57+
// s2.doWork( connection -> assertTrue( connection.isReadOnly() ) );
58+
} );
59+
s2.close();
60+
assert readOnlyConnectionsOpened == 1;
61+
assert readOnlyConnectionsClosed == 1;
62+
}
63+
64+
@Test void testStateless(SessionFactoryScope scope) {
65+
readOnlyConnectionsOpened = 0;
66+
readOnlyConnectionsClosed = 0;
67+
var factory = scope.getSessionFactory();
68+
factory.getSchemaManager().truncate();
69+
StatelessSession s1 =
70+
factory.withStatelessOptions()
71+
// .connectionHandling( AS_NEEDED, ON_CLOSE )
72+
.readOnly(true)
73+
.openStatelessSession();
74+
s1.inTransaction( tx -> {
75+
assertThrows( IllegalStateException.class,
76+
() -> s1.insert( new Thing() ) );
77+
} );
78+
s1.close();
79+
scope.inTransaction(s -> s.persist(new Thing()));
80+
StatelessSession s2 =
81+
factory.withStatelessOptions().readOnly(true)
82+
.openStatelessSession();
83+
s2.inTransaction( tx -> {
84+
Thing thing = s2.get( Thing.class, 2L );
85+
assertThrows( IllegalStateException.class,
86+
() -> s1.update( thing ) );
87+
assertThrows( IllegalStateException.class,
88+
() -> s1.delete( thing ) );
89+
} );
90+
s2.close();
91+
assert readOnlyConnectionsOpened == 1;
92+
assert readOnlyConnectionsClosed == 1;
93+
}
94+
95+
static int readOnlyConnectionsOpened;
96+
static int readOnlyConnectionsClosed;
97+
98+
public static class Provider extends DriverManagerConnectionProvider {
99+
@Override
100+
public Connection getReadOnlyConnection() throws SQLException {
101+
readOnlyConnectionsOpened++;
102+
return super.getReadOnlyConnection();
103+
}
104+
105+
@Override
106+
public void closeReadOnlyConnection(Connection connection) throws SQLException {
107+
super.closeReadOnlyConnection( connection );
108+
readOnlyConnectionsClosed ++;
109+
}
110+
}
111+
112+
@Entity
113+
static class Thing {
114+
@Id
115+
Long id = 2L;
116+
}
117+
}

0 commit comments

Comments
 (0)