Hibernate write followed by reading calls object not found

I am currently working on the Quizzing Tool, which uses hibernate and spring. I actually build it as a Sakai LMS tool, and this complicates this question a bit more, but let me see if I can generalize.

My current scenario is when users go to the StartQuiz page, which, when the form is submitted on the page, initializes the "Attempt" object (stored in sleep mode). It fills the object below:

<class name="org.quiztool.model.Attempt" table="QT_ATTEMPTS"> <cache usage="transactional" /> <id name="id" type="long"> <generator class="native"> <param name="sequence">QT_ATTEMPTS_ID_SEQ</param> </generator> </id> <many-to-one name="quizId" class="org.quiztool.model.Quiz" cascade="none" /> <property name="score" type="int" not-null="true" /> <property name="outOf" type="int" not-null="true" /> <list name="responses" cascade="none" table="QT_RESPONSES" lazy="false"> <key column="id"/> <index column="idxr"/> <many-to-many class="org.quiztool.model.QuizAnswer" /> </list> <list name="questionList" cascade="none" table="QT_ATTEMPT_QUESTIONS" lazy="false"> <key column="id"/> <index column="idxq"/> <many-to-many class="org.quiztool.model.QuizQuestion" /> </list> <property name="userId" type="string" length="99" /> <property name="siteRole" type="string" length="99" /> <property name="startTime" type="java.util.Date" not-null="true" /> <property name="finishTime" type="java.util.Date" /> </class> 

He randomly selects a set of questions and sets the start time and several other properties, and then redirects the user to the TakeTheQuiz page after saving the object through sleep mode.

On the TakeTheQuiz page, it loads the attempted object with its identifier, which is passed as a request parameter, then prints and formats it in html form so that the user can fill out the quiz. About 2/5 concurrent users will not see any questions. The attempted object is loading, and its questions are empty.

My theory is that the list of questions in the Tryempt object is either not immediately inserted into the database (this is normal as long as the object goes into the hibernate cache and I can get it from the cache, which I cannot see, to find out how to do it) OR it is stored in the database, but my object load on the TakeTheQuiz page reads an incomplete object from the cache.

Admittedly, my knowledge of Hibernate is limited, so if someone can help me understand what is going on here and how to fix it, let me know.

+4
source share
2 answers

The answer, as I found out, was simple. My save function seemed to lazily commit the database. As soon as I forcibly committed a commit for this object at the end of each transaction, the problem was resolved.

I ended up writing my own sleep mode session code, which looks like this:

 Session session = getSession(); session.beginTransaction(); session.saveOrUpdate(attempt); session.getTransaction.commit(); session.close(); 

The problem is resolved.

+1
source

My theory is that something is wrong with the piece of code that randomly selects questions. Are you sure this works? Paste part of your code.

The second theory is that something is wrong with transaction boundaries. When do you end the session? And when is your transaction completed? Try and install FlushMode in your session ALWAYS. Does it change anything?
0
source

All Articles