6.3. Business Tier
Let's review the three basic tiers of J2EE development. The Web tier handles the look and feel of the application. It is what the user interacts with. The Persistence tier handles long-term data storage. The Business tier handles coarse-grained business rules.
For example, consider the "Add Car" user story that we implemented in the last chapter. The user clearly has a way to enter new cars into the systemthe Web tier provides an HTML form. The new car has a way to be persisted for the long-termour DAOs in the Persistence tier. But what about the business rules for adding a new car to the JAW inventory?
When a dealer gets a new car on the lot, many actions need to happen. Someone needs to physically receive the car. Someone else needs to affix the dealer logo to the back of the car. Accounting needs to add it to the books as an asset. Marketing needs to add it to the "New Arrivals" listing in the newspaper ads.
While these are actions that would be done by humans in the real world, the same types of things usually need to happen in a J2EE application as well. In a J2EE application, we call a grouping of activities a Transaction. Transactions should be atomicif one of the steps fails, all grouped activities should roll back to their initial state.
The classic example is a bank transactionif you transfer money from your checking account to your savings account, the two distinct activities are treated as an atomic transaction. If your SavingsAccountDAO.depositMoney( ) method fails, you want your CheckingAccountDAO.withdrawMoney( ) method call to roll back as well. Otherwise, the money you took out will be lost.
Transactions are usually synchronous and represent a series of sequential steps to carry out a business process. Another type of activity occurs over a longer course of timean asynchronous activity. These types of activities tend to be a series of individual steps that are performed in a specific order, but don't necessarily cause the previous step to roll back. Think "work-flow" instead of "transaction." Think macro-view instead of micro-view. For example, if marketing doesn't get the new car in this week's newspaper ad, we don't need to roll back the dealer logo application and the accounting activities.
The EJB specification provides specific technologies that handle both synchronous and asynchronous activities.