From: Jeremy Quinn
> Dear All,
>
> I am using Woody (for the first time), with Hibernate, to
> edit Beans. Below is my first stab at a function for updating
> one of my Beans (a
> User Bean).
>
> The issue you may be able to offer advice on is this:
>
> How can I manage my HibernateSession, while using the Woody Framework?
>
> HibernateSessions need to be closed before a SendPageAndWait sends
> response, and re-opened on the returning Request. In the past I have
> handled this successfully via a ServletFilter, though I was led to
> understand at the time that this is not DIR [1].
>
> Christopher kindly added two special 'catch' handlers to implement
> this, catch (break) and catch (continue), see
> <http://wiki.cocoondev.org/Wiki.jsp?page=RhinoWithContinuations>.
>
> Unfortunately I do not think I understand their usage (see below) and
> had to comment them out, as the function does not compile
> with them in.
> Depending on whether I place them before or after the catch
> (e), I get
> different compile errors.
Hi Jeremy,
Try it this way:
function updateUser (form) {
var factory = cocoon.getComponent (PersistenceFactory.ROLE);
var session = factory.createSession ();
var userid = cocoon.parameters["userid"];
var successURI = cocoon.parameters["successURI"];
var formURI = cocoon.parameters["formURI"];
var user = UserPeer.load (session, userid);
if (user != null) {
while (true) {
try {
form.load (user);
form.showForm (formURI);
form.save (user);
var id = UserPeer.save (session, user);
if (id != null) {
cocoon.sendPage (successURI, { bean: user });
break;
} else {
// get woody to report the error on the form
}
} catch (e) {
cocoon.log.error (e);
cocoon.sendPage("screen/error", {message: e});
break;
} finally { // dispose of components
session.close();
cocoon.releaseComponent(factory);
}
} // end while
} else {
cocoon.sendPage("screen/error",
{
message: "Unable to retreive your user information"
}
);
}
catch (break) {
session.close();
}
catch (continue) {
session.reconnect();
}
}
IIRC catch(break) is must be used as "top-level" statement
(... but this is only a guess - never tried catch(continue) but
catch(break)
works for me.)
Cheers,
Reinhard