I can't seem to get my optimisitic lock= version to work.
I created a simple jsp to just edit the track title. To test, I use 2 seperate browsers (IE and Firefox).
Steps taken (with User A and User B):
A) editTrack.jsp?id=1
B) editTrack.jsp?id=1
-- At this point they are both looking at the same row in the DB.
A) edits the track title -> submit button -> applyTrackChanges.jsp -> forwards to editTrack.jsp
B) (Now has old version) edits the track title. -> submit button -> applyTrackChanges.jsp -> forwards to editTrack.jsp
I see the version number changing between updates. I see the hibernate printouts of the updates, but I don't know why a stale object exception isn't being thrown. Any ideas?
Hibernate version: 3
Mapping documents:
Code:
<hibernate-mapping>
<class name="com.orielly.hh.Track" table="TRACK" optimistic-lock="version">
<meta attribute="class-description">
Represents a single playable track in the music database
@author Jonathan with Hibernate
</meta>
<id name="id" type="int" column="TRACK_ID">
<meta attribute="scope-set">protected</meta>
<generator class="native" />
</id>
<version name="version" type="int">
<meta attribute="field-description">Version Info</meta>
</version>
<property name="title" type="string">
<meta attribute="use-in-tostring">true</meta>
<column name="TITLE" not-null="true" index="TRACK_TITLE" />
</property>
</class>
</hibernate-mapping>
Code between sessionFactory.openSession() and session.close():I am using HibernateSync, so that handles the opening and closing of the session. But since I am using lazy initialization, I close the session myself.
If you're not familiar with HibernateSync, it just abstracts a lot of the session and transaction code hibernate generates.
Code:
TrackDAO dao = new TrackDAO();
Track track = dao.get(id);
track.setTitle(newTitle);
dao.saveOrUpdate(track);
TrackDAO.closeCurrentSession();
Full stack trace of any exception that occurs:No error is thrown, there should be a StaleObjectException though.
Name and version of the database you are using:hsqldb
The generated SQL (show_sql=true):Hibernate: update TRACK set version=?, TITLE=?, filePath=?, playTime=?, added=?, volume=? where TRACK_ID=? and version=?
Hibernate: select track0_.TRACK_ID as TRACK1_77_0_, track0_.version as version77_0_, track0_.TITLE as TITLE77_0_, track0_.filePath as filePath77_0_, track0_.playTime as playTime77_0_, track0_.added as added77_0_, track0_.volume as volume77_0_ from TRACK track0_ where track0_.TRACK_ID=?
editTrack.jsp:Code:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<%@ page import="java.util.*" %>
<%@ page import="com.orielly.hh.*" %>
<%@ page import="com.orielly.hh.dao.*" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Edit Track!</title>
</head>
<body>
<%
TrackDAO dao = new TrackDAO();
int trackID = (new Integer (request.getParameter("id"))).intValue();
Track track = dao.get(trackID);
TrackDAO.closeCurrentSession();
%>
<form name="editTrack" method="post" action="/apply/applyTrackChanges.jsp">
Track Title: <h4> <%=track.getTitle() %> </h4>
Version Number: <%=track.getVersion() %> <br>
Edit To: <input type="text" name="trackTitle"/>
<br/>
<input type="hidden" name="id" value="<%=trackID%>">
<button type="submit">Edit Track</button>
</form>
</body>
</html>
applyTrackChanges.jsp:Code:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<%@ page import="java.util.*" %>
<%@ page import="com.orielly.hh.*" %>
<%@ page import="com.orielly.hh.dao.*" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>testing it!</title>
</head>
<body>
<%
int id = (new Integer (request.getParameter("id"))).intValue();
String newTitle = request.getParameter("trackTitle");
TrackDAO dao = new TrackDAO();
Track track = dao.get(id);
track.setTitle(newTitle);
dao.saveOrUpdate(track);
TrackDAO.closeCurrentSession();
%>
<jsp:forward page="/edit/editTrack.jsp?id=<%=trackID%>" />
</body>
</html>