Why auto-increment field keeps value after rollback?

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Why auto-increment field keeps value after rollback?

Franzisk
Why even after I rollback a transaction the auto-increment field (in this case is MySQL) keeps the value inserted?
Shouldn't be removed after rollback?

    public boolean save(T obj) throws HorusDAOException {
        EntityTransaction transaction = this.manager.getTransaction();
        transaction.begin();
        try {
            this.manager.persist(obj);
            transaction.commit();
        } catch (RuntimeException ex) {
            if (transaction.isActive()) {
                try {
                    transaction.rollback();
                } catch (RuntimeException e) {
                }
            }
            throw new HorusDAOException(ex);
        } finally {
            manager.close();
        }
        return true;
    }


In this case the insert is not performed because there is a duplicated key, so I rollback and I expected to not increment the auto-increment counter for the auto-increment field.

What am I doing wrong here?
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Why auto-increment field keeps value after rollback?

Mitesh Meswani
I am assuming you are using GenerationType.TABLE/SEQUENCE  (Or AUTO that
defaults to TABLE).

You are not doing anything wrong here. The auto-increment field is
incremented using non-tx connection and hence it does not get rolled
back when you roll back your main transaction.

Regards,
Mitesh

On 12/18/2009 5:27 AM, Franzisk wrote:

> Why even after I rollback a transaction the auto-increment field (in this
> case is MySQL) keeps the value inserted?
> Shouldn't be removed after rollback?
>
>      public boolean save(T obj) throws HorusDAOException {
>          EntityTransaction transaction = this.manager.getTransaction();
>          transaction.begin();
>          try {
>              this.manager.persist(obj);
>              transaction.commit();
>          } catch (RuntimeException ex) {
>              if (transaction.isActive()) {
>                  try {
>                      transaction.rollback();
>                  } catch (RuntimeException e) {
>                  }
>              }
>              throw new HorusDAOException(ex);
>          } finally {
>              manager.close();
>          }
>          return true;
>      }
>
> In this case the insert is not performed because there is a duplicated key,
> so I rollback and I expected to not increment the auto-increment counter for
> the auto-increment field.
>
> What am I doing wrong here?
>    
Loading...