3 Replies Latest reply on Nov 18, 2004 1:53 PM by Russ Loucks

    TransactionRolledbackLocalException while creating bean inst

    Russ Loucks Newbie

      I have a fairly straight forward product catalog data model that includes categories,
      products, items. Categories can have zero or more products, products can have zero
      or more items.

      I have a single (integer) primary key on all beans and have basic CMR mapping between
      the beans. The primary keys are auto_increment fields in the DB. All beans also include
      a character 'ID' field. The ID field is not unique at each level in the hierarchy (e.g., product
      P0 can have item I0 associated with it as well as product P1 can have an Item I0 associated
      with it).

      I'm using MySQL 4.0.21, JBoss 3.2.3 on Linux RedHat 9.

      The issue I'm having is how to have the system (JBoss, MySQL, etc.) handle more of the
      'uniqueness' issue here. I've implemented an 'addChild()' business method on the category
      and product beans to add children to their nest. This method determines if the new child
      already exists (based on the ID field) in their nest. If so, then toss an exception.

      This is, however, a bit heavy on the processing side........ I would like the system to
      handle more of this processing. I tried composite keys on all of the beans but that
      pretty much failed. I could create composite keys at the item level, but specifying
      a composite key as a foreign key in the CMR relationship I just couldn't get to work.
      Bummer.....

      Soooo, I tried setting a UNIQUE INDEX (itemID, parentKey) on the database and having
      the 'addChild()' method just call, for example, ItemLocalHome.create() with the new
      Item child. The 'create()' succeeded but when the new Item bean was persisted, the
      DB server threw a nasty exception back to JBoss
      (TransactionRolledbackLocalException). Unfortunately, I couldn't trap this in any of
      the beans' methods and return a nice error back to the client.

      I understand that with CMP-based beans, I write less code but I also lose significant
      processing control, especially in situations like this.

      Is there any way of specifying something in the system to allow me to (not have
      composite keys defined in the beans but) force JBoss or something else to provide
      me better control in this situation (e.g., synchronize create() and persistence processing)
      so I can handle error conditions better?

      Thanks.

        • 1. Re: TransactionRolledbackLocalException while creating bean
          Alexey Loubyansky Master

          You can addChild in RequiresNew, catch TransactionRollbackException and assuming it's unique key violation handle it appropriately.

          • 2. Re: TransactionRolledbackLocalException while creating bean
            Russ Loucks Newbie

            loubyansky,

            Thanks for the message. I tried adding the following code to my 'ejb-jar.xml' file:

             <container-transaction >
             <method >
             <ejb-name>Product</ejb-name>
             <method-intf>Local</method-intf>
             <method-name>addChild</method-name>
             <method-params>
             <method-param>com.tm.wineStore.ejb.catalog.ItemValue</method-param>
             </method-params>
             </method>
             <trans-attribute>RequiresNew</trans-attribute>
             </container-transaction>
            


            (Actually, I'm using Xdoclet so I added a '@ejb.transaction type="RequiresNew" to the
            'addChild()' method.)

            The result? The same as before. I added a 'catch (Exception E) { log.debug(...); }'
            around the ItemLocalHome.create() code, but this bit of exception code was never tickled. The
            'addChild()' method completed successfully, but some milliseconds later the
            JBoss log file contained the same traceback as before...... Hmmmm.

            Any more help? Thanks, RB

            • 3. Re: TransactionRolledbackLocalException while creating bean
              Russ Loucks Newbie

              I tried wrapping the 'addchild()' method with a 'RequiresNew' transaction attribute -- no avail.....

              Here's what I did to resolve this issue:

              1) I created a 'UNIQUE INDEX (entityId, parentKey)' in the (child) entity bean. This will
              prohibit bad data being slammed into the table underneath the beans.

              2) In my child bean, I added the following finder query (using Xdoclet tag)

               * @ejb.finder
               * signature="java.util.Collection findDuplicate (java.lang.String iId, java.lang.Integer pKey)"
               * query="SELECT OBJECT(a) FROM Item AS a WHERE (a.itemId = ?1) AND (a.product.productKey = ?2)"
               * view-type="local"
              

              3) In my parent (Product) bean addChild() method, I just call this 'findDuplicate()' method
              to determine if a duplicate exists. If so, I throw an exception, else, I continue adding the
              child.

              Seems to work pretty well.

              RB