I have a situation where I am updating a database record via my CMP Entity
Bean ... I have several non-bean operations that i'd like to wrap in a
transaction such that, if the update to the Bean or any of the other
operations fail, the database update rolls back.

Here's the problem: For INSERTs and DELETEs to the database, the operations
are executed immediately, but for UPDATEs, the operations are queued and
don't get executed until a commit() is issued

My environment:

Sun JDK 1.3
JBOSS 2.3 Beta (5-23-01 build) ... it has also been tested on JBOSS 2.2.1
final and still fails
PostgreSQL 7.1.2 on Redhat 7.1

Here's the pseudocode:

TransactionManager trans = null;

try
{
  trans =
(TransactionManager)getHome("java:/TransactionManager",TransactionManager.cl
ass);
  trans.begin();
  log("tx has begun...");

  try
  {
    SupplierOrder supOrder=null;

    supOrder =
supOrderHome.findBySupplierOrderIDSupplierID(supplierOrderID.intValue(),
params.memberCompanyID);

    //Read the DB record
    SupplierOrderRecord supOrderRec = supOrder.getRecord();

    //Not a problem
    supOrderRec.order_payment_method_id = 44;

    //ROLLBACK TEST! (This will cause a RI constraint violation)
    supOrderRec.order_freight_carrier_id=new Integer(33333333);

    statusChange = SupplierOrderConstants.SUPPLIER_ORDER_STATUS_SHIPPED;

    //Write the changes to the Bean
    log("Updating supplier_order record...");
    supOrder.setRecord(supOrderRec);

    //If there was a change in status of the order, send an email to the
user
    if(statusChange > 0)
      sendNotificationEmail(params,statusChange,supplier_id);

  }
  catch(FinderException fe){
    log(LOG_INFO, "User attempted to load either a non-existent order, or an
order for a different supplier");
    params.ctx.put("noSuchOrder",new Boolean(true));

    fe.printStackTrace();
  }

  log("about to commit tx...");
  trans.commit();
  log("tx committed");
}
catch(Exception e){
  log(LOG_ERROR,"Serious error editing order ... changes are being rolled
back! " + e);
  trans.rollback();
}


Here's what happens (the lines that DON'T begin with [DEBUG] are from inside
the bean):

[Default] lookup name: java:/TransactionManager
[Default] [DEBUG] tx has begun...
[Default] [DEBUG] Loading SupplierOrder as SUPPLIER
[Default] in setEntityContext() - isModified() = false
[Default] in ejbActivate() - isModified() = false
[Default] in ejbLoad() - isModified() = false
[Default] begin getRecord() - isModified() = false
[Default] end getRecord() - isModified() = false
[Default] [DEBUG] Updating supplier_order record...
[Default] begin setRecord() - isModified() = false
[Default] begin setModified() - isModified() = false
[Default] end setModified() - isModified() = true
[Default] end setRecord() - isModified() = true
[Default] [DEBUG] Email subject: Your order has been APPROVED
[Default] [DEBUG] Email body: <blah blah blah>
[Default] [DEBUG] about to commit tx...
[Default] in ejbStore() - isModified() = true
[EmbeddedTomcat] java.rmi.ServerException: Store failed; nested exception
is:
[EmbeddedTomcat]        java.sql.SQLException: ERROR:  <unnamed> referential
integrity violation - key referenced from supplier_order not found in
freight_carrier
<blah blah a big stack trace>
[Default] [ERROR] Serious error editing order ... changes are being rolled
back!
 javax.transaction.RollbackException: Unable to commit, tx=XidImpl
[FormatId=257
, GlobalId=newyankeewrkshp//3076, BranchQual=] status=STATUS_ROLLEDBACK
[Default] [ERROR] Rollback failed! java.lang.IllegalStateException: No
transaction.


The problem is that the attempt to UPDATE the database (ejbStore()) doesn't
happen until the call to commit() ... it's as if the container 'queues' up
the list of operations that it should perform on the DB and executes them
all in one shot at the end of the transaction ... however, create() or
remove() operations seem to happen immediately upon the call to the
respective method ... why are UPDATEs treated differently? This behavior is
not desireable as it means that I will not be able to detect my RI (or
whatever) failure until after i've attempted to commit. (this is related to
my posting last week titled "TransactionManager bug? or is there something
i'm missing?") ...

How have other people coped with this problem?

Thanks in advance!

-Dave


_______________________________________________
JBoss-user mailing list
[EMAIL PROTECTED]
http://lists.sourceforge.net/lists/listinfo/jboss-user

Reply via email to