|
17 | 17 | import jakarta.persistence.Id; |
18 | 18 |
|
19 | 19 | import org.hibernate.Session; |
| 20 | +import org.hibernate.SessionException; |
20 | 21 | import org.hibernate.SessionFactory; |
21 | 22 | import org.hibernate.Transaction; |
22 | 23 | import org.hibernate.boot.Metadata; |
23 | 24 | import org.hibernate.boot.MetadataSources; |
24 | 25 | import org.hibernate.boot.SessionFactoryBuilder; |
25 | | -import org.hibernate.boot.registry.StandardServiceRegistryBuilder; |
26 | 26 | import org.hibernate.cfg.AvailableSettings; |
27 | 27 | import org.hibernate.cfg.Environment; |
28 | 28 | import org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl; |
29 | 29 | import org.hibernate.engine.jdbc.connections.spi.ConnectionProvider; |
30 | 30 | import org.hibernate.internal.util.PropertiesHelper; |
| 31 | +import org.hibernate.query.Query; |
31 | 32 | import org.hibernate.service.spi.ServiceRegistryImplementor; |
32 | 33 | import org.hibernate.service.spi.Stoppable; |
33 | 34 | import org.hibernate.tool.schema.internal.HibernateSchemaManagementTool; |
|
37 | 38 |
|
38 | 39 | import org.hibernate.testing.AfterClassOnce; |
39 | 40 | import org.hibernate.testing.junit4.BaseUnitTestCase; |
| 41 | +import org.hibernate.testing.orm.junit.JiraKey; |
40 | 42 | import org.hibernate.testing.util.ServiceRegistryUtil; |
41 | 43 |
|
42 | 44 | import org.hibernate.orm.test.util.DdlTransactionIsolatorTestingImpl; |
| 45 | + |
| 46 | +import org.junit.After; |
| 47 | +import org.junit.Assert; |
43 | 48 | import org.junit.Test; |
44 | 49 |
|
| 50 | +import static org.junit.Assert.assertEquals; |
| 51 | + |
45 | 52 | /** |
46 | 53 | * @author Vlad Mihalcea |
47 | 54 | */ |
@@ -82,6 +89,12 @@ public void destroy() { |
82 | 89 | } |
83 | 90 | } |
84 | 91 |
|
| 92 | + @After |
| 93 | + public void cleanup() { |
| 94 | + doInSession(FRONT_END_TENANT, session -> session.createMutationQuery( "delete from Person" ).executeUpdate() ); |
| 95 | + doInSession(BACK_END_TENANT, session -> session.createMutationQuery( "delete from Person" ).executeUpdate() ); |
| 96 | + } |
| 97 | + |
85 | 98 | //tag::multitenacy-hibernate-MultiTenantConnectionProvider-example[] |
86 | 99 |
|
87 | 100 | protected void registerConnectionProvider(String tenantIdentifier) { |
@@ -116,6 +129,63 @@ public void testBasicExpectedBehavior() { |
116 | 129 | //end::multitenacy-multitenacy-hibernate-same-entity-example[] |
117 | 130 | } |
118 | 131 |
|
| 132 | + @Test |
| 133 | + @JiraKey( value = "HHH-17972") |
| 134 | + public void testChangeTenantWithoutConnectionReuse() { |
| 135 | + Person person = new Person(); |
| 136 | + person.setId( 1L ); |
| 137 | + person.setName( "John Doe" ); |
| 138 | + Person person2 = new Person(); |
| 139 | + person2.setId( 2L ); |
| 140 | + person2.setName( "Jane Doe" ); |
| 141 | + |
| 142 | + Transaction t; |
| 143 | + Session session = null; |
| 144 | + Session newSession = null; |
| 145 | + try { |
| 146 | + session = sessionFactory.withOptions().tenantIdentifier( FRONT_END_TENANT ).openSession(); |
| 147 | + t = session.beginTransaction(); |
| 148 | + session.persist( person ); |
| 149 | + t.commit(); |
| 150 | + |
| 151 | + Query<Person> sessionQuery = session.createQuery( "from Person", Person.class ); |
| 152 | + assertEquals( 1, sessionQuery.getResultList().size() ); |
| 153 | + assertEquals( "John Doe", sessionQuery.getResultList().get( 0 ).getName() ); |
| 154 | + |
| 155 | + newSession = session.sessionWithOptions().tenantIdentifier( BACK_END_TENANT ).openSession(); |
| 156 | + t = newSession.beginTransaction(); |
| 157 | + newSession.persist( person2 ); |
| 158 | + t.commit(); |
| 159 | + |
| 160 | + Query<Person> newSessionQuery = newSession.createQuery( "from Person", Person.class ); |
| 161 | + assertEquals( 1, newSessionQuery.getResultList().size() ); |
| 162 | + assertEquals( "Jane Doe", newSessionQuery.getResultList().get( 0 ).getName() ); |
| 163 | + } |
| 164 | + finally { |
| 165 | + if (session != null) { |
| 166 | + session.close(); |
| 167 | + } |
| 168 | + if (newSession != null) { |
| 169 | + newSession.close(); |
| 170 | + } |
| 171 | + } |
| 172 | + } |
| 173 | + |
| 174 | + @Test |
| 175 | + @JiraKey( value = "HHH-17972") |
| 176 | + public void testChangeTenantWithConnectionReuse() { |
| 177 | + try (Session session = sessionFactory.withOptions().tenantIdentifier( FRONT_END_TENANT ).openSession()) { |
| 178 | + Assert.assertThrows( "Cannot redefine the tenant identifier on a child session if the connection is reused", |
| 179 | + SessionException.class, |
| 180 | + () -> session.sessionWithOptions().tenantIdentifier( BACK_END_TENANT ).connection().openSession() |
| 181 | + ); |
| 182 | + Assert.assertThrows( "Cannot redefine the tenant identifier on a child session if the connection is reused", |
| 183 | + SessionException.class, |
| 184 | + () -> session.sessionWithOptions().connection().tenantIdentifier( BACK_END_TENANT ).openSession() |
| 185 | + ); |
| 186 | + } |
| 187 | + } |
| 188 | + |
119 | 189 | protected Properties properties() { |
120 | 190 | Properties properties = new Properties(); |
121 | 191 | URL propertiesURL = Thread.currentThread().getContextClassLoader().getResource("hibernate.properties"); |
|
0 commit comments