In the previous section, the capsule you created was a stateless session bean. Now you can use the same ideas to create a more complex capsule. This one is a stateful session bean. This tutorial walks you through building a session bean called SimpleBankEJB. It needs to track some properties for a user, so a stateful session bean is an appropriate design choice. This means that the bean needs a few data fields that hold state information between method calls. This section of the tutorial shows you how to add these variables and pull information from a database to fill them.
You now have an empty capsule that is a stateful session EJB. It currently has no functionality other than the default, but you could deploy it without error to an EJB application server.
Since this is a stateful session bean, it can retain information (state) that is particular for a client. This bean needs to track the account number, the customer's name and the account balance for the current client. To hold this information, add String and double variables from the Insert menu or Palette.
Your capsule outline should now look like this:
Whenever a client wants to use an instance of a session bean, it must first call create on the home interface of the bean. This create method call tells the application server to get an instance of the session bean ready for use. When the client calls this method, it gets a reference to an instance of that session bean from the EJB server. For a stateful session bean capsule, you need to define the create method. If you did not define one, the compiler would create a default method with no arguments. Since the SimpleBank bean needs an account number argument to access the correct bank account, you have to define a create method that can pass this argument. You cannot use the default method that the compiler generates.
You must name the method ejbCreate in order for it to correspond to the create method in the EJB's home interface. If you do not define a method called ejbCreate, the compiler generates a default one that takes no arguments. In general, ejbCreate methods have a transactional attribute of Supports because their behavior inside of a transaction has no specific requirements according to the EJB specification. In this case, use the default Supports transaction level.
If you are familiar with EJBs, you may notice that as currently defined, this ejbCreate method returns void instead of a reference to the EJB instance. When AppComposer generates the bean code, the home interface automatically includes the proper return type.
Now that the capsule has this required create method defined, you need to add some behaviors that generate the EJB instance correctly when the client calls the ejbCreate method. The behaviors need to use the account number passed into the method to look up the customer in a database, and should cache the current account balance and customer's name. This information is the state of the stateful session bean.
Since the account information resides in a database, you need to add an SQLSelect actor to the capsule to retrieve it. Not all ejbCreate methods need to retrieve information from a database, this is simply how we are doing it in this example.
You have configured a query to retrieve the user's name and balance from the database, using the accountNum parameter. Now you need to add behaviors to the session bean capsule that can execute this query.
Your capsule outline should now look like this:
SQL queries return data in the form of rows, and in this case is going to be a single row since the accountNum parameter is a unique primary key. Your capsule needs to get the customer's name and account balance from the result row. When a row is retrieved successfully from a database, it generates a rowRetrieved event. In this case, since only one row should match the parameter, only one event gets generated. Use this event to set the variables representing the state of the stateful session bean to the values that are retrieved from the database.
This finishes the ejbCreate implementation. At the end of this method call, the session bean has some initial state set. At this point, your capsule should look like this:
So far, you have created your session bean capsule, and added actors, behaviors and data items that allow the bean to get information from a database and store it as the session's state. It can get and hold onto this information, but does not yet do anything with it. The next section of the tutorial shows you how to implement a simple withdrawal transaction that uses the state held in the session bean to update the information in the account record.