Author: stliu
Date: 2010-05-15 04:37:30 -0400 (Sat, 15 May 2010)
New Revision: 19518
Added:
core/trunk/testsuite/src/test/java/org/hibernate/test/stateless/Contact.hbm.xml
core/trunk/testsuite/src/test/java/org/hibernate/test/stateless/Contact.java
core/trunk/testsuite/src/test/java/org/hibernate/test/stateless/Country.java
core/trunk/testsuite/src/test/java/org/hibernate/test/stateless/Org.java
core/trunk/testsuite/src/test/java/org/hibernate/test/stateless/StatelessSessionQueryTest.java
Modified:
core/trunk/core/src/main/java/org/hibernate/engine/PersistenceContext.java
core/trunk/core/src/main/java/org/hibernate/engine/StatefulPersistenceContext.java
core/trunk/core/src/main/java/org/hibernate/impl/StatelessSessionImpl.java
Log:
HHH-3220 Patch to prevent org.hibernate.AssertionFailure: possible non-threadsafe access
to the session error caused by stateless sessions
Modified: core/trunk/core/src/main/java/org/hibernate/engine/PersistenceContext.java
===================================================================
--- core/trunk/core/src/main/java/org/hibernate/engine/PersistenceContext.java 2010-05-15
07:48:04 UTC (rev 19517)
+++ core/trunk/core/src/main/java/org/hibernate/engine/PersistenceContext.java 2010-05-15
08:37:30 UTC (rev 19518)
@@ -445,8 +445,12 @@
* Call this after finishing a two-phase load
*/
public void afterLoad();
-
+
/**
+ * Is in a two-phase load?
+ */
+ public boolean isLoadFinished();
+ /**
* Returns a string representation of the object.
*
* @return a string representation of the object.
Modified:
core/trunk/core/src/main/java/org/hibernate/engine/StatefulPersistenceContext.java
===================================================================
---
core/trunk/core/src/main/java/org/hibernate/engine/StatefulPersistenceContext.java 2010-05-15
07:48:04 UTC (rev 19517)
+++
core/trunk/core/src/main/java/org/hibernate/engine/StatefulPersistenceContext.java 2010-05-15
08:37:30 UTC (rev 19518)
@@ -1095,6 +1095,9 @@
loadCounter--;
}
+ public boolean isLoadFinished() {
+ return loadCounter == 0;
+ }
/**
* Returns a string representation of the object.
*
Modified: core/trunk/core/src/main/java/org/hibernate/impl/StatelessSessionImpl.java
===================================================================
--- core/trunk/core/src/main/java/org/hibernate/impl/StatelessSessionImpl.java 2010-05-15
07:48:04 UTC (rev 19517)
+++ core/trunk/core/src/main/java/org/hibernate/impl/StatelessSessionImpl.java 2010-05-15
08:37:30 UTC (rev 19518)
@@ -179,7 +179,9 @@
errorIfClosed();
Object result = getFactory().getEntityPersister(entityName)
.load(id, null, lockMode, this);
- temporaryPersistenceContext.clear();
+ if ( temporaryPersistenceContext.isLoadFinished() ) {
+ temporaryPersistenceContext.clear();
+ }
return result;
}
Added: core/trunk/testsuite/src/test/java/org/hibernate/test/stateless/Contact.hbm.xml
===================================================================
--- core/trunk/testsuite/src/test/java/org/hibernate/test/stateless/Contact.hbm.xml
(rev 0)
+++
core/trunk/testsuite/src/test/java/org/hibernate/test/stateless/Contact.hbm.xml 2010-05-15
08:37:30 UTC (rev 19518)
@@ -0,0 +1,28 @@
+<?xml version="1.0"?>
+<!DOCTYPE hibernate-mapping PUBLIC
+ "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
+ "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
+
+<hibernate-mapping package="org.hibernate.test.stateless">
+
+ <class name="Contact">
+ <id name="id">
+ <generator class="native" />
+ </id>
+ <many-to-one name="org" lazy="false" fetch="join"
/>
+ </class>
+
+ <class name="Org">
+ <id name="id">
+ <generator class="native" />
+ </id>
+ <many-to-one name="country" lazy="false" fetch="join"
/>
+ </class>
+
+ <class name="Country">
+ <id name="id">
+ <generator class="native" />
+ </id>
+ </class>
+
+</hibernate-mapping>
\ No newline at end of file
Added: core/trunk/testsuite/src/test/java/org/hibernate/test/stateless/Contact.java
===================================================================
--- core/trunk/testsuite/src/test/java/org/hibernate/test/stateless/Contact.java
(rev 0)
+++
core/trunk/testsuite/src/test/java/org/hibernate/test/stateless/Contact.java 2010-05-15
08:37:30 UTC (rev 19518)
@@ -0,0 +1,47 @@
+/*
+ * Hibernate, Relational Persistence for Idiomatic Java
+ *
+ * Copyright (c) 2009, Red Hat Middleware LLC or third-party contributors as
+ * indicated by the @author tags or express copyright attribution
+ * statements applied by the authors. All third-party contributions are
+ * distributed under license by Red Hat Middleware LLC.
+ *
+ * This copyrighted material is made available to anyone wishing to use, modify,
+ * copy, or redistribute it subject to the terms and conditions of the GNU
+ * Lesser General Public License, as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+ * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
+ * for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this distribution; if not, write to:
+ * Free Software Foundation, Inc.
+ * 51 Franklin Street, Fifth Floor
+ * Boston, MA 02110-1301 USA
+ *
+ */
+package org.hibernate.test.stateless;
+
+/**
+ *
+ * @author stliu
+ */
+public class Contact {
+ private Integer id;
+ public Integer getId() {
+ return id;
+ }
+ public void setId( Integer id ) {
+ this.id = id;
+ }
+ public Org getOrg() {
+ return org;
+ }
+ public void setOrg( Org org ) {
+
this.org = org;
+ }
+ private Org org;
+
+}
Added: core/trunk/testsuite/src/test/java/org/hibernate/test/stateless/Country.java
===================================================================
--- core/trunk/testsuite/src/test/java/org/hibernate/test/stateless/Country.java
(rev 0)
+++
core/trunk/testsuite/src/test/java/org/hibernate/test/stateless/Country.java 2010-05-15
08:37:30 UTC (rev 19518)
@@ -0,0 +1,13 @@
+package org.hibernate.test.stateless;
+
+public class Country {
+ private Integer id;
+
+ public Integer getId() {
+ return id;
+ }
+
+ public void setId(Integer id) {
+ this.id = id;
+ }
+}
Added: core/trunk/testsuite/src/test/java/org/hibernate/test/stateless/Org.java
===================================================================
--- core/trunk/testsuite/src/test/java/org/hibernate/test/stateless/Org.java
(rev 0)
+++ core/trunk/testsuite/src/test/java/org/hibernate/test/stateless/Org.java 2010-05-15
08:37:30 UTC (rev 19518)
@@ -0,0 +1,23 @@
+package org.hibernate.test.stateless;
+
+public class Org {
+ private Integer id;
+ private Country country;
+
+ public Integer getId() {
+ return id;
+ }
+
+ public void setId(Integer id) {
+ this.id = id;
+ }
+
+ public Country getCountry() {
+ return country;
+ }
+
+ public void setCountry(Country country) {
+ this.country = country;
+ }
+
+}
Added:
core/trunk/testsuite/src/test/java/org/hibernate/test/stateless/StatelessSessionQueryTest.java
===================================================================
---
core/trunk/testsuite/src/test/java/org/hibernate/test/stateless/StatelessSessionQueryTest.java
(rev 0)
+++
core/trunk/testsuite/src/test/java/org/hibernate/test/stateless/StatelessSessionQueryTest.java 2010-05-15
08:37:30 UTC (rev 19518)
@@ -0,0 +1,117 @@
+/*
+ * Hibernate, Relational Persistence for Idiomatic Java
+ *
+ * Copyright (c) 2009, Red Hat Middleware LLC or third-party contributors as
+ * indicated by the @author tags or express copyright attribution
+ * statements applied by the authors. All third-party contributions are
+ * distributed under license by Red Hat Middleware LLC.
+ *
+ * This copyrighted material is made available to anyone wishing to use, modify,
+ * copy, or redistribute it subject to the terms and conditions of the GNU
+ * Lesser General Public License, as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+ * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
+ * for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this distribution; if not, write to:
+ * Free Software Foundation, Inc.
+ * 51 Franklin Street, Fifth Floor
+ * Boston, MA 02110-1301 USA
+ *
+ */
+package org.hibernate.test.stateless;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import org.hibernate.FetchMode;
+import org.hibernate.Session;
+import org.hibernate.StatelessSession;
+import org.hibernate.Transaction;
+import org.hibernate.cfg.Configuration;
+import org.hibernate.cfg.Environment;
+import org.hibernate.junit.functional.FunctionalTestCase;
+
+/**
+ *
+ * @author stliu
+ */
+public class StatelessSessionQueryTest extends FunctionalTestCase {
+
+ public StatelessSessionQueryTest( String string ) {
+ super( string );
+ }
+
+ public void testCriteria() {
+ TestData testData=new TestData();
+ testData.createData();
+ StatelessSession s = getSessions().openStatelessSession();
+ assertEquals( 1, s.createCriteria( Contact.class ).list().size() );
+ s.close();
+ testData.cleanData();
+ }
+
+ public void testCriteriaWithSelectFetchMode() {
+ TestData testData=new TestData();
+ testData.createData();
+ StatelessSession s = getSessions().openStatelessSession();
+ assertEquals( 1, s.createCriteria( Contact.class ).setFetchMode( "org",
FetchMode.SELECT )
+ .list().size() );
+ s.close();
+ testData.cleanData();
+ }
+
+ public void testHQL() {
+ TestData testData=new TestData();
+ testData.createData();
+ StatelessSession s = getSessions().openStatelessSession();
+ assertEquals( 1, s.createQuery( "from Contact c join fetch c.org join fetch
c.org.country" )
+ .list().size() );
+ s.close();
+ testData.cleanData();
+ }
+ private class TestData{
+ List list = new ArrayList();
+ public void createData(){
+ Session session = openSession();
+ Transaction tx = session.beginTransaction();
+ Country usa = new Country();
+ session.save( usa );
+ list.add( usa );
+ Org disney = new Org();
+ disney.setCountry( usa );
+ session.save( disney );
+ list.add( disney );
+ Contact waltDisney = new Contact();
+ waltDisney.setOrg( disney );
+ session.save( waltDisney );
+ list.add( waltDisney );
+ tx.commit();
+ session.close();
+ }
+ public void cleanData(){
+ Session session = openSession();
+ Transaction tx = session.beginTransaction();
+ for(Object obj: list){
+ session.delete( obj );
+ }
+ tx.commit();
+ session.close();
+ }
+ }
+
+
+ @Override
+ public void configure( Configuration cfg ) {
+ super.configure( cfg );
+ cfg.setProperty( Environment.MAX_FETCH_DEPTH, "1" );
+ }
+
+ public String[] getMappings() {
+ return new String[] { "stateless/Contact.hbm.xml" };
+ }
+
+}