background image

The TxControllerBean Session

<< Session Beans | Java Persistence Entities >>
<< Session Beans | Java Persistence Entities >>

The TxControllerBean Session

The AccountControllerBean session bean has two get methods. The getAccountsOfCustomer
method returns all of the accounts of a given customer by invoking the getAccounts method of
the Account entity. Instead of implementing a get method for every instance variable, the
AccountControllerBean
has a getDetails method that returns an object (AccountDetails)
that encapsulates the entire state of an Account entity. Because it can invoke a single method to
retrieve the entire state, the client avoids the overhead associated with multiple remote calls.
The CustomerControllerBean Session Bean
A client creates a Customer entity by invoking the createCustomer method of the
CustomerControllerBean
session bean. To remove a customer, the client calls the
removeCustomer
method, which invokes the EntityManager.remove method on the Customer
instance.
The CustomerControllerBean session bean has two methods that return multiple customers:
getCustomersOfAccount
and getCustomersOfLastName. getCustomersOfAccount calls the
getCustomers
method of the Account entity. getCustomersOfLastName uses the
Customer.FindByLastName
named query to search the database for customers with a matching
last name, which is a named parameter to the query.
The TxControllerBean Session Bean
The TxControllerBean session bean handles bank transactions. In addition to its get methods,
getTxsOfAccount
and getDetails, the TxControllerBean bean has several methods that
change the balances of the bank accounts:
withdraw
deposit
makeCharge
makePayment
transferFunds
These methods access an Account entity to verify the account type and to set the new balance.
The withdraw and deposit methods are for standard accounts, whereas the makeCharge and
makePayment
methods are for accounts that include a line of credit. If the type method
argument does not match the account, these methods throw an
IllegalAccountTypeException
. If a withdrawal were to result in a negative balance, the
withdraw
method throws an InsufficientFundsException. If a credit charge attempts to
exceed the account's credit line, the makeCharge method throws an
InsufficientCreditException
.
The transferFunds method also checks the account type and new balance; if necessary, it
throws the same exceptions as the withdraw and makeCharge methods. The transferFunds
method subtracts from the balance of one Account instance and adds the same amount to
another instance. Both of these steps must complete to ensure data integrity. If either step fails,
the entire operation is rolled back and the balances remain unchanged. The transferFunds
Enterprise Beans
The Java EE 5 Tutorial · September 2007
1060