...Each transfer needs a unique ID for tracking purpose. The unique ID is generated by an existing system which is also
used by other applications. For performance reasons, the transaction that gets the unique ID should be as short as possible.
The scenario is implemented in four steps which are implemented in four business methods in a CMT session bean:
1. checkGoods - Checks goods in a database
2. getUniqeId - Retrieve the unique ID
3. checkAmount -Checks the amount in a non-transactional system
4. storeTransfer - Stores the transfer in a database as part of the calling transaction.
These methods are called by the addTransfer method of a second CMT session bean in the following order:
checkGoods, getUniqueId, checkAmount, storeTransfer
Assuming no other transaction-related metadata, which is the correct set of transaction, attributes for the methods in the session
0.addTransfer - Required
1.checkGoods - Required
2.getUniqueId - Required_New
3.checkAmoutns - Not_Supported
4.storeTransfer - Mandatory
The unique ID is generated by an existing system which is also used by other applications. For performance reasons,
the transaction that gets the unique ID should be as short as possible.
So, we should use REQUIRES_NEW finish the method as short as possible.
The question specified that getUniqueId should be done as soon as possible for higher performance. But the suggested solution is to use REQUIRES_NEW for getUniqueId().
When this getUniqueId is run in a new transaction, the overhead to create that new transaction is high and may affect the performance.
So, I think getUniquedId should use REQUIRED, SUPPORTS or even NOT_SUPPORTED. If it uses REQUIRED OR SUPPORTS, it will be run in the transaction of addTransfer.
If it uses NOT_SUPPORTED, it will run in a non-transactional context.
Since getUniqueId is used for getting the id, not committing any data , it is fine to run it in a non-transactional context.