Help needed with transactions


EJB design: Help needed with transactions

  1. Help needed with transactions (1 messages)

    I have a container managed stateless session bean that has a 'registerCustomer' method whose transaction attribute is set to 'Required'.

    This does lots of inserts and some updates and has lots of business logic. (In other words, i want to re-use this code, not re-write it!) It can throw some custom exceptions (eg 'DuplicateAliasException'), and calls sessionContext.setRollbackOnly() before doing so.

    That's been working fine for quite some time, but now we have an additional requirement for 'bulk registration'. The way this works is that lots of customer details get pre-loaded into a db. There is a status for each customer (which can take 3 states: not registered, registered and error).

    So what i want to do is this:
     - get a list of pre-loaded customers with status 'not registered' (obviously not a problem)
     - do the 'registerCustomer' for each one.
     - update the status of the pre-loaded customer to 'registered' or 'error'.

    If an individual registerCustomer succeeds, i want to update the status. This would imply a solution where i just create another method, set its transaction attribute to 'Required' and call the 'customerRegistration' followed by a status update so both items will work as one transaction.

    If the status update fails, i want to rollback the 'customerRegistration' stuff, which also implies the above solution.

    However, if the 'customerRegistration' fails, i still want to update the status (to error). This contradicts the above solution because the rollback will also rollback my status update!

    Is there anyway i can build it to reuse the customerRegistration code meet the goals listed above? My container is websphere v5 if that makes a difference.

    Any help much appreciated...
      - Richard.
  2. You just have to call from transaction 1 a method that have the attribute RequiredNew so this will start a second transaction, so even if transaction 1 is rollback the second transaction will not. So the method to call may be just a wrapper that call a method that have another attribute like "Required".