3 Replies Latest reply on May 29, 2009 1:33 PM by Joshua Davis

    restart transaction

    Benito Juarez Newbie

      Hello,


      i store a bunch of entities to a hibernate database using entityManager.persist(item).
      In some circumstances it throws a ConstraintViolationException and the transaction rolls back. Its quite difficult to check the constraint by code. So is there a possibility to keep the transaction alive or reanimate it in an exception handling?


      thx 4 help
      BJ

        • 1. Re: restart transaction
          Joshua Davis Expert

          I don't think JTA supports that, but even if it did the constraint violation would happen again unless you did something differently (e.g. make sure the constraint isn't violated).  You're going to have to figure out how to avoid violating the constraint anyway, so why not just do it in the code explicitly rather than catching exceptions?

          • 2. Re: restart transaction
            Benito Juarez Newbie

            Thats simply impossible because u cant guarantee, what another process don't change the data between the check and the db-insert.


            Where must be a useful exception handling for this case.

            • 3. Re: restart transaction
              Joshua Davis Expert

              Benito Juarez wrote on May 29, 2009 10:25:


              Thats simply impossible because u cant guarantee, what another process don't change the data between the check and the db-insert.


              I've used database locks to do that.  Not impossible, but it has some consequences.  Anyway, that's off topic.


              Hibernate also supports optimistic locking, and will throw a specific exception when a conflict occurs (another transaction changed the data while the current transaction is in progress).  In this case, you usually want to notify the user and give them the opportunity to overwrite the data.   Of course this would happen in a new transaction.


              You could do a similar thing with constraint violations.