Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
HHH-9764 : StaleObjectStateExceptions raising outside flush context
- Loading branch information
Showing
5 changed files
with
210 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
188 changes: 188 additions & 0 deletions
188
hibernate-core/src/test/java/org/hibernate/test/cache/LockModeTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,188 @@ | |||
package org.hibernate.test.cache; | |||
|
|||
import org.junit.After; | |||
import org.junit.Before; | |||
import org.junit.Test; | |||
|
|||
import org.hibernate.Hibernate; | |||
import org.hibernate.LockMode; | |||
import org.hibernate.Session; | |||
import org.hibernate.cache.internal.CollectionCacheInvalidator; | |||
import org.hibernate.cfg.Configuration; | |||
import org.hibernate.cfg.Environment; | |||
import org.hibernate.engine.spi.SessionFactoryImplementor; | |||
import org.hibernate.testing.TestForIssue; | |||
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; | |||
|
|||
import static org.junit.Assert.assertEquals; | |||
import static org.junit.Assert.assertFalse; | |||
import static org.junit.Assert.assertNull; | |||
import static org.junit.Assert.assertTrue; | |||
|
|||
/** | |||
* @author Guenther Demetz | |||
* @author Gail Badner | |||
*/ | |||
public class LockModeTest extends BaseCoreFunctionalTestCase { | |||
@Override | |||
protected Class<?>[] getAnnotatedClasses() { | |||
return new Class[] { User.class, Company.class }; | |||
} | |||
|
|||
@Before | |||
public void before() { | |||
CollectionCacheInvalidator.PROPAGATE_EXCEPTION = true; | |||
} | |||
|
|||
@After | |||
public void after() { | |||
CollectionCacheInvalidator.PROPAGATE_EXCEPTION = false; | |||
} | |||
|
|||
@Override | |||
protected void configure(Configuration cfg) { | |||
super.configure( cfg ); | |||
cfg.setProperty( Environment.AUTO_EVICT_COLLECTION_CACHE, "true" ); | |||
cfg.setProperty( Environment.USE_SECOND_LEVEL_CACHE, "true" ); | |||
cfg.setProperty( Environment.USE_QUERY_CACHE, "true" ); | |||
cfg.setProperty( Environment.CACHE_PROVIDER_CONFIG, "true" ); | |||
} | |||
|
|||
@Override | |||
protected void prepareTest() throws Exception { | |||
Session s = openSession(); | |||
s.beginTransaction(); | |||
|
|||
Company company1 = new Company( 1 ); | |||
s.save( company1 ); | |||
|
|||
User user = new User( 1, company1 ); | |||
s.save( user ); | |||
|
|||
Company company2 = new Company( 2 ); | |||
s.save( company2 ); | |||
|
|||
s.getTransaction().commit(); | |||
s.close(); | |||
} | |||
|
|||
@Override | |||
protected void cleanupTest() throws Exception { | |||
Session s = openSession(); | |||
s.beginTransaction(); | |||
|
|||
s.createQuery( "delete from org.hibernate.test.cache.User" ).executeUpdate(); | |||
s.createQuery( "delete from org.hibernate.test.cache.Company" ).executeUpdate(); | |||
|
|||
s.getTransaction().commit(); | |||
s.close(); | |||
} | |||
|
|||
/** | |||
*/ | |||
@TestForIssue(jiraKey = "HHH-9764") | |||
@Test | |||
public void testDefaultLockModeOnCollectionInitialization() { | |||
Session s1 = openSession(); | |||
s1.beginTransaction(); | |||
|
|||
Company company1 = s1.get( Company.class, 1 ); | |||
|
|||
User user1 = s1.get( User.class, 1 ); // into persistent context | |||
|
|||
/****************************************** | |||
* | |||
*/ | |||
Session s2 = openSession(); | |||
s2.beginTransaction(); | |||
User user = s2.get( User.class, 1 ); | |||
user.setName("TestUser"); | |||
s2.getTransaction().commit(); | |||
s2.close(); | |||
|
|||
|
|||
/****************************************** | |||
* | |||
*/ | |||
|
|||
// init cache of collection | |||
assertEquals( 1, company1.getUsers().size() ); // raises org.hibernate.StaleObjectStateException if 2LCache is enabled | |||
|
|||
|
|||
s1.getTransaction().commit(); | |||
s1.close(); | |||
} | |||
|
|||
@TestForIssue(jiraKey = "HHH-9764") | |||
@Test | |||
public void testDefaultLockModeOnEntityLoad() { | |||
|
|||
// first evict user | |||
sessionFactory().getCache().evictEntity( User.class.getName(), 1 ); | |||
|
|||
Session s1 = openSession(); | |||
s1.beginTransaction(); | |||
|
|||
Company company1 = s1.get( Company.class, 1 ); | |||
|
|||
/****************************************** | |||
* | |||
*/ | |||
Session s2 = openSession(); | |||
s2.beginTransaction(); | |||
Company company = s2.get( Company.class, 1 ); | |||
company.setName( "TestCompany" ); | |||
s2.getTransaction().commit(); | |||
s2.close(); | |||
|
|||
|
|||
/****************************************** | |||
* | |||
*/ | |||
|
|||
User user1 = s1.get( User.class, 1 ); // into persistent context | |||
|
|||
// init cache of collection | |||
assertNull( user1.getCompany().getName() ); // raises org.hibernate.StaleObjectStateException if 2LCache is enabled | |||
|
|||
s1.getTransaction().commit(); | |||
s1.close(); | |||
} | |||
|
|||
@TestForIssue(jiraKey = "HHH-9764") | |||
@Test | |||
public void testReadLockModeOnEntityLoad() { | |||
|
|||
// first evict user | |||
sessionFactory().getCache().evictEntity( User.class.getName(), 1 ); | |||
|
|||
Session s1 = openSession(); | |||
s1.beginTransaction(); | |||
|
|||
Company company1 = s1.get( Company.class, 1 ); | |||
|
|||
/****************************************** | |||
* | |||
*/ | |||
Session s2 = openSession(); | |||
s2.beginTransaction(); | |||
Company company = s2.get( Company.class, 1 ); | |||
company.setName( "TestCompany" ); | |||
s2.getTransaction().commit(); | |||
s2.close(); | |||
|
|||
|
|||
/****************************************** | |||
* | |||
*/ | |||
|
|||
User user1 = s1.get( User.class, 1, LockMode.READ ); // into persistent context | |||
|
|||
// init cache of collection | |||
assertNull( user1.getCompany().getName() ); // raises org.hibernate.StaleObjectStateException if 2LCache is enabled | |||
|
|||
s1.getTransaction().commit(); | |||
s1.close(); | |||
} | |||
|
|||
} |
c767b0e
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Interesting., so finally return LockMode.READ at ResultSetProcessingContextImpl.java line 127 was just some kind of typo?