Search in sources :

Example 16 with Triggerable

use of org.hibernate.testing.logger.Triggerable in project hibernate-orm by hibernate.

the class MultipleSessionCollectionWarningTest method testUnsetSessionCannotOverwriteConnectedSesssion.

@Test
@TestForIssue(jiraKey = "HHH-9518")
public void testUnsetSessionCannotOverwriteConnectedSesssion() {
    Parent p = new Parent();
    Child c = new Child();
    p.children.add(c);
    Session s1 = openSession();
    s1.getTransaction().begin();
    s1.saveOrUpdate(p);
    // The collection is "connected" to s1 because it contains the CollectionEntry
    CollectionEntry ce = ((SessionImplementor) s1).getPersistenceContext().getCollectionEntry((PersistentCollection) p.children);
    assertNotNull(ce);
    // the collection session should be s1
    assertSame(s1, ((AbstractPersistentCollection) p.children).getSession());
    Session s2 = openSession();
    s2.getTransaction().begin();
    Triggerable triggerable = logInspection.watchForLogMessages("HHH000471:");
    assertFalse(triggerable.wasTriggered());
    // The following should trigger warning because we're unsetting a different session
    // We should not do this in practice; it is done here only to force the warning.
    // Since s1 was not flushed, the collection role will not be known (no way to test that).
    assertFalse(((PersistentCollection) p.children).unsetSession((SessionImplementor) s2));
    assertTrue(triggerable.wasTriggered());
    // collection's session should still be s1
    assertSame(s1, ((AbstractPersistentCollection) p.children).getSession());
    s2.getTransaction().rollback();
    s2.close();
    s1.getTransaction().rollback();
    s1.close();
}
Also used : CollectionEntry(org.hibernate.engine.spi.CollectionEntry) SessionImplementor(org.hibernate.engine.spi.SessionImplementor) Triggerable(org.hibernate.testing.logger.Triggerable) Session(org.hibernate.Session) Test(org.junit.Test) TestForIssue(org.hibernate.testing.TestForIssue)

Example 17 with Triggerable

use of org.hibernate.testing.logger.Triggerable in project hibernate-orm by hibernate.

the class MultipleSessionCollectionWarningTest method testUnsetSessionCannotOverwriteConnectedSesssionFlushed.

@Test
@TestForIssue(jiraKey = "HHH-9518")
public void testUnsetSessionCannotOverwriteConnectedSesssionFlushed() {
    Parent p = new Parent();
    Child c = new Child();
    p.children.add(c);
    Session s1 = openSession();
    s1.getTransaction().begin();
    s1.saveOrUpdate(p);
    // flush the session so that p.children will contain its role
    s1.flush();
    // The collection is "connected" to s1 because it contains the CollectionEntry
    CollectionEntry ce = ((SessionImplementor) s1).getPersistenceContext().getCollectionEntry((PersistentCollection) p.children);
    assertNotNull(ce);
    // the collection session should be s1
    assertSame(s1, ((AbstractPersistentCollection) p.children).getSession());
    Session s2 = openSession();
    s2.getTransaction().begin();
    Triggerable triggerable = logInspection.watchForLogMessages("HHH000471:");
    assertFalse(triggerable.wasTriggered());
    // The following should trigger warning because we're unsetting a different session
    // We should not do this in practice; it is done here only to force the warning.
    // The collection role and key should be included in the message (no way to test that other than inspection).
    assertFalse(((PersistentCollection) p.children).unsetSession((SessionImplementor) s2));
    assertTrue(triggerable.wasTriggered());
    // collection's session should still be s1
    assertSame(s1, ((AbstractPersistentCollection) p.children).getSession());
    s2.getTransaction().rollback();
    s2.close();
    s1.getTransaction().rollback();
    s1.close();
}
Also used : CollectionEntry(org.hibernate.engine.spi.CollectionEntry) SessionImplementor(org.hibernate.engine.spi.SessionImplementor) Triggerable(org.hibernate.testing.logger.Triggerable) Session(org.hibernate.Session) Test(org.junit.Test) TestForIssue(org.hibernate.testing.TestForIssue)

Example 18 with Triggerable

use of org.hibernate.testing.logger.Triggerable in project hibernate-orm by hibernate.

the class MultipleSessionCollectionWarningTest method testUnsetSessionCannotOverwriteNonConnectedSesssion.

@Test
@TestForIssue(jiraKey = "HHH-9518")
public void testUnsetSessionCannotOverwriteNonConnectedSesssion() {
    Parent p = new Parent();
    Child c = new Child();
    p.children.add(c);
    Session s1 = openSession();
    s1.getTransaction().begin();
    s1.saveOrUpdate(p);
    // Now remove the collection from the PersistenceContext without unsetting its session
    // This should never be done in practice; it is done here only to test that the warning
    // gets logged. s1 will not function properly so the transaction will ultimately need
    // to be rolled-back.
    CollectionEntry ce = (CollectionEntry) ((SessionImplementor) s1).getPersistenceContext().getCollectionEntries().remove(p.children);
    assertNotNull(ce);
    // the collection session should still be s1; the collection is no longer "connected" because its
    // CollectionEntry has been removed.
    assertSame(s1, ((AbstractPersistentCollection) p.children).getSession());
    Session s2 = openSession();
    s2.getTransaction().begin();
    Triggerable triggerable = logInspection.watchForLogMessages("HHH000471:");
    assertFalse(triggerable.wasTriggered());
    // The following should trigger warning because we're unsetting a different session.
    // We should not do this in practice; it is done here only to force the warning.
    // Since s1 was not flushed, the collection role will not be known (no way to test that).
    assertFalse(((PersistentCollection) p.children).unsetSession((SessionImplementor) s2));
    assertTrue(triggerable.wasTriggered());
    // collection's session should still be s1
    assertSame(s1, ((AbstractPersistentCollection) p.children).getSession());
    s2.getTransaction().rollback();
    s2.close();
    s1.getTransaction().rollback();
    s1.close();
}
Also used : CollectionEntry(org.hibernate.engine.spi.CollectionEntry) SessionImplementor(org.hibernate.engine.spi.SessionImplementor) Triggerable(org.hibernate.testing.logger.Triggerable) Session(org.hibernate.Session) Test(org.junit.Test) TestForIssue(org.hibernate.testing.TestForIssue)

Aggregations

Triggerable (org.hibernate.testing.logger.Triggerable)18 Test (org.junit.Test)18 TestForIssue (org.hibernate.testing.TestForIssue)12 Session (org.hibernate.Session)8 CollectionEntry (org.hibernate.engine.spi.CollectionEntry)5 Transaction (org.hibernate.Transaction)3 Metadata (org.hibernate.boot.Metadata)3 MetadataSources (org.hibernate.boot.MetadataSources)3 StandardServiceRegistryBuilder (org.hibernate.boot.registry.StandardServiceRegistryBuilder)3 SessionImplementor (org.hibernate.engine.spi.SessionImplementor)3 HashMap (java.util.HashMap)2 Map (java.util.Map)2 Criteria (org.hibernate.Criteria)2 ServiceRegistryImplementor (org.hibernate.service.spi.ServiceRegistryImplementor)2 CachingRegionFactory (org.hibernate.testing.cache.CachingRegionFactory)2 BMRules (org.jboss.byteman.contrib.bmunit.BMRules)2