One more note regarding transactions with JPA: The actual DB changes aren't
made until the current transaction completes. Because of this, in my current
code I actually aggressively close the transaction so that I can catch
things like constraint violations. If you wait until the session ends to
close the transaction (via cleanup) then you won't be able to handle cases
like this. I have a helper method on my Model class that lets me run a
method inside its own transaction and handle any JPA-related exceptions. I'm
going to put it into the JPA tutorial, but here it is for now:

  /**
   This method allows me to clean up my code a bit and only handle
JPA-related exceptions.
   An example usage would be:

   def addFood(newFood : Food) =
     wrapEM({
       Model.persist(newFood)
       S.redirectTo("/food/list")
     }, {
       case cve : ConstraintViolationException => S.error("That food already
exists!")
       case _ => S.error("Internal error adding food")
     })

   Note that if I used normal try/catch then the wildcard match would trap
the RedirectException
   thrown by S.redirectTo.
  */
  def wrapEM(f : => Unit) : Unit = wrapEM(f, { case _ => /* nop */ })
  def wrapEM[A](f : => A, handler : PartialFunction[Throwable, A]) : A = {
    try {
      val tx = getEM.getTransaction()

      if (! tx.isActive() ) { tx.begin() }
      try {
    val ret : A = f
    ret
      } catch {
    case he : HibernateException => {
      this.error("Hibernate error", he)
      handler(he)
    }
    case pe : PersistenceException => {
      this.error("EM Error", pe)
      handler(pe)
    }
    case sqle : java.sql.SQLException => {
      this.error("SQL Exception", sqle)
      handler(sqle)
    }
      } finally {
    // make sure that we commit even with a redirectexception
    if (tx.isActive() && ! tx.getRollbackOnly()) {
      tx.commit()
    } else if (tx.getRollbackOnly()) {
      tx.rollback()
    }
      }
    } catch {
      // Special case. Usually we want to know why it failed to commit, not
just that it failed
      case re : RollbackException => {
    val (cause,message) = if (re.getCause() == null) {
      (re,"No cause")
    } else {
      (re.getCause(), re.getCause().getMessage())
    }
    this.error("EM Commit error: {}", message)
    this.error("Full trace", re)
    handler(cause)
      }
    }
  }



On Tue, Sep 30, 2008 at 5:25 PM, Derek Chen-Becker <[EMAIL PROTECTED]>wrote:

> I may have some time tomorrow to get it working. My initial goal is
> actually to get JNDI and JTA working in Jetty so that we have a dev
> environment that matches the production env.
>
> Derek
>
>
> On Tue, Sep 30, 2008 at 5:01 PM, Tim Perrett <[EMAIL PROTECTED]> wrote:
>
>>
>> Awesome - I personally would be very interested to hear how you got on
>> wtih hibernate proper.
>>
>> Perhaps you can bosh up a quick sample ;-)
>>
>> Cheers Kris
>>
>> Tim
>>
>> On Sep 30, 11:14 pm, "Kris Nuttycombe" <[EMAIL PROTECTED]>
>> wrote:
>> > Yup, Hibernate & JTA on Glassfish, also with EJB remoting.
>>
>> >>
>>
>

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Lift" group.
To post to this group, send email to liftweb@googlegroups.com
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
-~----------~----~----~----~------~----~------~--~---

Reply via email to