4 Replies Latest reply on Aug 6, 2007 12:05 PM by vphagura

    Em does not rollback

      Folks,

      I have the following code;

      @Enitity
      public class Cruise
      {
       private Collection<Reservation> reservations;
      ...
       @OneToMany(cascade = CascadeType.ALL, mappedBy="cartridge")
       public Collection<Reservation> getReservations() {
       return reservations;
       }
      
       public void setReservations(Collection<Reservation> reservations) {
       this.reservations= reservations;
       }
      ...
      }
      
      @Enitity
      public class Reservation
      {
       private Cruise cruise;
      ...
       @ManyToOne(cascade = {CascadeType.ALL})
       @JoinColumn(name="cruiseId", nullable=true)
       public Cruise getCruise() {
       return cruise ;
       }
      
       public void setCruise(Cruise cruise ) {
       this.cruise = cruise;
       }
      ...
      }
      
      @Stateless
      public class PersistBean
      {
      
       /** Injected EntityManger */
       @PersistenceContext( unitName="eg")
       protected EntityManager em = null;
      
      ...
      
       public void persist( Set reservations, Curise cruise )
       {
       .....
       Iterator it = reservations.iterator();
       while( it.hasNext() )
       {
       Reservation r = (Reservation)it.next();
       r.setCruise( cruise );
       em.persist( r );
       }
       ......
       }
      
      ....
      }


      Now, I'm trying to write just 4 reservations. And, on the 4th one I get a db error about a contraint being voilated! I find that the first 3 records are writen to the Reservation table.

      My point is that, if my persist() method is running in a transaction, which I believe it is, then the first 3 records should have been rolledback, following an error on the 4th record!!!!

      Please help! Any help will be appreciated. Thx in adv!


        • 1. Re: Em does not rollback
          karl.martens

          I ran into a similar problem when I started using EJB3. As it turned out it wasn't a problem with EJB3 but a configuration issue with my database.

          I was using MySql which by default uses a non transactional storage engine (at least in the Linux distribution). Here is some documentation from MySQL http://dev.mysql.com/doc/refman/4.1/en/storage-engines.html

          Once I altered the database configuration to use the innodb storage engine my transaction rolled back as expected.

          The default behaviour for the for the stateless session bean is to wrap each method call in a transaction or participate in an existing transaction (should it already exist). In the case where a transaction had not been created when you called the persist method on the entity manager you would have received a TransactionRequiredException.

          Not sure if this is your particular problem but gives you something to check into.

          • 2. Re: Em does not rollback

            Thx very much for the response! I think that may the issue as you mentioned. As, I'm using MySQL on Linux. I may have to read a little about its configuration.

            I'll post my more findings...

            Thx for your time.

            • 3. Re: Em does not rollback
              alrubinger

              Also, make sure your database is not set to auto-commit.

              S,
              ALR

              • 4. Re: Em does not rollback

                Okay, here is what is happening;

                Even though the MySQL documentation says that innodb is enabled by default from version 4.0 onwards, it is partially true! On Linux the MySQl version 4.0.18(which I'm using) does not have innodb turned on. It is only true on Windows!!

                So that is the source of my problem!!

                This URL may also help anybody in the same boat...

                http://forums.mysql.com/read.php?11,4044,4044