Hi Derek,
Yes I don't really understand the lifecycle of stateful snippet. Only
understand what I've seen in demo code, and really don't fully
understand.
My thinking is that its an object that hangs around as long as its
used on the page, but hangs around on the server side as-if it is in a
http session. Also it contains data that is shown on the page.
Well I will play around with extended in code in Liftweb and see what
happens, thats the only want I can be sure. From what I see in SEAM,
they like it a lot and I have to do a SEAM project starting today so
I'll know more about it.
I used to work for a document management company, we did Hibernate
years ago in the server side, a few things annoyed me. Also especially
on a golf related project had simular problems.
1. When user A reads document, user B writes document, user A now
attempts to write - using merge it over-writes user B's changes.
2. Couldn't navigate lazy associations on subsquent call-backs without
re-loading from the database
Thanks, Philip
On Dec 10, 4:25 am, "Derek Chen-Becker" <[EMAIL PROTECTED]> wrote:
> I think you misunderstand the lifecycle of a stateful snippet. A stateful
> snippet has specific techniques to keep it resident (using this.link,
> this.redirect, etc) so you have to be careful about how you use it or you
> lose the instance. The HTTP session would be the most appropriate place for
> a JPA EM that crosses request boundaries because it will *always* be
> available to snippets (I had forgotten about the EXTENDED type, I've never
> used it).
>
> Another issue, orthogonal to how you keep the session around, is that when
> you hold a session open you're also holding a transaction open unless you
> use RESOURCE_LOCAL transactions (unmanaged JDBC connections, essentially).
> That has implications on data visibility to other connections, how you
> handle exceptions, etc.
>
> Don't get me wrong, I think that there's a place for extended JPA EMs, I
> just want to make sure you're aware of all of the tradeoffs so that you
> don't get any nasty surprises. It's my opinion that for most use cases
> opening a session for each request (and the merge/getReference that doing it
> this way implies) is the best way to do it. JPA providers are usually pretty
> intelligent about efficiently using database operations to keep things
> coherent, so I don't think that "avoiding merge" is necessarily a worthwhile
> goal in and of itself.
>
> Cheers,
>
> Derek
>
>
>
> On Tue, Dec 9, 2008 at 6:39 AM, philip <[EMAIL PROTECTED]> wrote:
>
> > Hi Derek,
>
> > I think I found the solution to the problem I am thinking of.
> > Its called JPA Extended Persistence Context. Its suggested that a
> > extended persistence context be created which starts at some time and
> > ends at some time and is stored in HttpSession. From the bits I read
> > they said its no good to store in a Stateful Session Bean because the
> > bean may be passivated and the context lost. Well we are not using
> > stateful session bean so it doesn't matter, we can put it in the
> > session or put it on some object that hands around like a session such
> > as a ... in our case I believe a Snippet.
>
> > So in the JPA demo, you would then open as
> > override def openEM () = {
> > val em = factory.createEntityManager
> > (PersistenceContextType.EXTENDED)
> > em.getTransaction().begin()
> > em
> > }
>
> > So then I would suggest the em be stored in the snippet which is a
> > stateful thing that hangs around.
> > According to the docs, many transactions can happen on the em, until
> > em.close() is called and therefore it is then flushed to the database.
> > NORMALLY Extended persistence context is managed by the container,
> > SEAM or EJB this is called container managed, but in our case we can
> > do our own management. In EJB its something insane like this
> > @Stateful
> > @Remote(ShoppingCart.class)
> > public class ShoppingCartBean implements ShoppingCart
> > {
> > @PersistenceContext(type=PersistenceContextType.EXTENDED)
> > EntityManager em;
> > ...
> > But we do it ourselves...
> > Why do this? The benefit is that you can keep using the same JPA
> > object references without doing a merge. So if you have a Employee
> > object which is a JPA/Hibernate object, and you changed its data and
> > you want to save it to the database, merge is not needed.
> > Why is that good? Because merge normally re-loads an entity from the
> > database at the merge call, then it overwrites the entity contents and
> > you might loose data that was previously written by another user!
> > (unless you used a version number). In our case without merge, then
> > the entity that was originally used by the EM is still in use. It is
> > not a detached entity. Also you can navigate lazy relationships from
> > request to request, from an Employee to a Manager which is really good
> > as you can just display a list of Employees, then when someone clicks
> > on the manager link of the employee it will just be able to navigate
> > as a object reference lazy loaded from DB.
> > Without the extended persistence context then the entity would be
> > called detached because the original persistence context of the entity
> > has been closed.
>
> > Now - I haven't actually tested this! but I feel reasonably convinced
> > I'm right - please tell me I am wrong. I want to at least solve these
> > problems before I can start on the larger project. It then makes sense
> > for me to use JPA, I'll reverse engineer first from mysql then convert
> > from Java to Scala and use the JPA example project as a starting
> > point, I can do the project in waterfall steps.
>
> > Thanks, Philip
>
> > On Dec 8, 2:01 pm, "Derek Chen-Becker" <[EMAIL PROTECTED]> wrote:
> > > I use request vars to pass these things around in my own code, so merging
> > is
> > > pretty trivial. For example:
>
> > > object employee extends RequestVar[Can[Employee]](Empty)
>
> > > def viewEmployee (xhtml : NodeSeq) : NodeSeq =
> > > employee.map(Model.merge(_)).map({ empl =>
> > > ...
> > > bind(...)
>
> > > }) openOr Text("Invalid Employee for View")
>
> > > Note the double map call. The first merges, the second actually does the
> > > binding. If you preferred to load from the DB each time you could do
>
> > > def viewEmployee (xhtml : NodeSeq) : NodeSeq =
> > > employee.map(Model.getReference(classOf[Employee], _.id)).map({ empl =>
>
> > > instead. In either case this is simple enough that I haven't really
> > bothered
> > > looking for any cleaner solution. One thing I'd note is that generally in
> > > Hibernate and JPA, keeping a session open across multiple web requests is
> > > frowned upon. I don't know enough about the guts of SEAM to know if
> > they're
> > > really doing that, or using some magic to make it appear that they are.
> > If I
> > > remember correctly, in SEAM your objects are injected and outjected by
> > SEAM
> > > itself, so they could easily be intercepting things to make it kosher. In
> > > any case, using the EM this way is outside my experience so if things
> > start
> > > going screwy I don't know how much I can help.
>
> > > Derek
>
> > > On Sun, Dec 7, 2008 at 8:44 AM, philip <[EMAIL PROTECTED]> wrote:
>
> > > > Hi Derek,
>
> > > > The problem I am thinking about is if you loaded for example, a list
> > > > of "Employees" your going to show in a table.
> > > > In your table you have a view button to view the employee, since you
> > > > used JPA to load the list in the first place but now since the EM is
> > > > finished from the first request and now we are on a second request
> > > > with a new EM instance. If we try to navigate the lazy loading
> > > > relationships, it won't work, it will throw a LazyLoadingException.
>
> > > > So what you do - is your merge the entity back and the load it back
> > > > from the database and then navigate the relationship.
> > > > But if the EM was around all the time, cross requests, over the
> > > > session then you could navigate the relationships without merge. I
> > > > was suggesting the stateful snippit which is holding these lazy
> > > > loading relationships should hold the EM since the entitys are
> > > > normally on the stateful snippet.
>
> > > > Thanks, Philip
>
> > > > On Dec 7, 10:47 pm, "Derek Chen-Becker" <[EMAIL PROTECTED]> wrote:
> > > > > I've been doing Hibernate and JPA long enough that reattaching
> > instances
> > > > at
> > > > > method entry is somewhat habitual for me. Josh makes a good point
> > about
> > > > > being aware of the transaction, but in my experience the request
> > method
> > > > is
> > > > > short-lived and it's usually not a problem. As for using a Stateful
> > > > session
> > > > > snippet to hold the EM, I think you want to look at the Model class;
> > we
> > > > use
> > > > > a request var that essentially exists for the lifetime of the request
> > to
> > > > > hold the EM. That avoids tying the EM to a single snippet class
> > (which
> > > > may
> > > > > or may not exist for every request) and makes it globally available
> > to
> > > > all
> > > > > of your code if you want to use separate logic handling classes.
>
> > > > > Derek
>
> > > > > On Sun, Dec 7, 2008 at 6:18 AM, Josh Suereth <
> > [EMAIL PROTECTED]
> > > > >wrote:
>
> > > > > > As a side note...
>
> > > > > > The LazyInitialization exception wasn't an issue for me when using
> > the
> > > > > > "OpenSessionInView" pattern. However it became a large issue when
> > > > trying
> > > > > > to use hibernate to back a thick-client GUI. With what I've used
> > of
> > > > lift,
> > > > > > I can't see JPA being any more difficult to manage then using
> > another
> > > > > > web-framework (perhaps even simpler in some regard). The key is
> > just
> > > > to
> > > > > > understand what leaving the hibernate session open does to your
> > > > transaction
> > > > > > length and how you cna remedy troubles as you see them.
>
> > > > > > -Josh
>
> > > > > > On Sat, Dec 6, 2008 at 9:59 PM, philip <[EMAIL PROTECTED]>
> > wrote:
>
> > > > > >> Hi Derek,
>
> > > > > >> Thanks, these are good reasons to use the JPA.
>
> > > > > >> About the use of the JPA in SEAM - the SEAM in Action (http://
> > > > > >> manning.com/dallen/) book says
> > > > > >> "Lazy loading of entity associations has unfortunately established
> > a
> > > > > >> bad reputation. The first thing that comes to mind in most
> > developers'
> > > > > >> minds when you talk about lazy loading is Hibernate's
> > > > > >> LazyInitializationException. The culprit is the detached entity
> > > > > >> instance.
> > > > > >> ...
> > > > > >> If the persistence manager is closed after the action method is
> > > > > >> invoked, the Course instance is detached when the view is
> > rendered.
> > > > > >> ...
> > > > > >> A call to the method getHoles() triggers an exception because the
> > > > > >> EntityManager that loaded the Course instance is no longer
> > available
> > > > > >> to further communicate with the database. The same problem arises
> > on
> > > > > >> postback, even if a lazy association wasn't hit in the view."
>
> > > > > >> So within Liftweb it would then make sense to place the JPA
> > > > > >> persistence manager within a stateful snippet. Since the snippet
> > is
> > > > > >> bound to the session, then navigation of the lazy loading entitys
> > > > > >> wouldn't cause a LazyInitializationException?
>
> ...
>
> read more »
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Lift" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/liftweb?hl=en
-~----------~----~----~----~------~----~------~--~---