persistence@glassfish.java.net

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

From: Mitesh Meswani <Mitesh.Meswani_at_Sun.COM>
Date: Wed, 06 Jan 2010 17:14:21 -0800

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?
>