Rollback does not work on MyISAM storage engine with Hibernate and MySQL

I am doing some tests with hibernate but the rollback seems to not be working with the Mysql database MyISAM.

I have an Entity called “Person”, that have a property poiting to same class (Person)

like this:

@Entity
public class Person {


    @OneToOne(cascade = CascadeType.ALL)
    @JoinColumn(name = "pessoa_id")
    private Person conjuge;
    
    @Column(unique = true)
    private String document; 


}

Then I have a generic DAO:

 public void save( T entity ){
          try {
            
              getCurrentSession().beginTransaction();
              getCurrentSession().saveOrUpdate( entity );
              getCurrentSession().getTransaction().commit();
          } catch (Exception e) {
              getCurrentSession().getTransaction().rollback();
              System.out.print(e.getMessage());
              System.out.print("error............");
          }

   }

When I set the Person.conjuge property to another Person and call this save method, and if I set the main person with the same document property it will throw an excpetion, because the field “document” is unique, and it is ok.

But that rollback is not working and the conjuge Person continues being inserted into the table,
What am I missing here?

Ok, I changed the mysql dialect to InnoDB, and it worked.

MyISAM does not support transactions. Only InnoDB is a transactional storage engine. Anyway, InnoDB is the default storage engine, so why did you choose MyISAM in the first place?

1 Like