On Dec 13, 2012, at 2:41 PM, "Howard W. Smith, Jr." <[email protected]> 
wrote:

> Question...
> 
> My last/latest implementation/version of AirportShuttleProcessingBean is a
> @Stateless EJB that calls many other @Stateless EJB (DAO). Should I keep
> PersistentContext in AirportShuttleProcessingBean, or can
> AirportShuttleProcessingBean call any/all @Stateless EJB (DAO) for shortest
> transaction possiblel? I hear that a lot here in Apache/TomEE/Tomcat
> land... shortest transactions are best.

The rule of thumb is that you want the transaction to be as short as possible 
while also not jeopardizing data integrity/consistency.

As a small example, say you had to process an order from a customer that didn't 
yet exist.  Ideally, you would add the new customer information in one 
transaction, then add the order in another transaction.

The scope starts to become "too short", when we get into say processing the 
order itself with several small transactions.  You wouldn't want to insert a 
new bare "Order" as one transaction, then add each "Item" in an order as 
separate transactions.  The result of a failure to complete is a half processed 
order which is clearly a non-recoverable situation.


One somewhat unrelated note, I did notice several flush() commands in your 
code.  All data is automatically flushed at the end of the transaction, so 
there should be no need for this can and in fact calling it excessively can 
cause performance issues.  It can mean the difference between one trip to the 
database and one hundred trips to the database.

Having said that, *do not* remove them now.  Wait till we are clearly at a 
state  where we have successfully split the transactions and things work.  Once 
we reach that milestone and all works and code is checked in, then give the 
"remove excess flush() calls" optimization a try.


-David

Reply via email to