13.07.2015 Views

Java™ Application Development on Linux - Dator

Java™ Application Development on Linux - Dator

Java™ Application Development on Linux - Dator

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

21.2 Expanding to EJBs479Here, items like the identity of the customer and the list of items in thecart must be preserved between method invocati<strong>on</strong>s. Obviously, it is possibleto present such an interface through a stateless bean by creating some sort of asessi<strong>on</strong> identifier token and passing that in to every method, thus allowing thestateless sessi<strong>on</strong> bean to save this data to a database and then load it back, butthe primary advantage of a stateful sessi<strong>on</strong> bean is that this work is d<strong>on</strong>e foryou through the setting of the bean’s c<strong>on</strong>text.So, the primary advantage of stateful sessi<strong>on</strong> beans is that the server sidecan keep track of client data for you. The primary disadvantage is that thec<strong>on</strong>tainer will try its best to keep an instance of the Bean around for everyclient, so it must sometimes swap an idle instance out to make room for an activeinstance, which is an expensive operati<strong>on</strong>. But—and this is important tokeep in mind—it is much less expensive than reading and writing this data <strong>on</strong>every call to a stateless bean! You have to understand what is happening underthe hood if you want to produce an optimal design. If you need state betweenmethod calls, a stateful bean is likely to be the most effective way to go.21.2.2.2Entity BeansWhat they are. An entity bean is often described as an object that representsa row in a database table. This is the most typical case, but it isn’t always so.We have worked <strong>on</strong> a J2EE applicati<strong>on</strong> where the entity bean represented anXML document in the filesystem.The general idea is that enterprise applicati<strong>on</strong>s tend to work <strong>on</strong> lists ofsimilar things: customers, employees, locati<strong>on</strong>s, accounts, servers, inventoryitems, and so <strong>on</strong>. An entity bean is an object that represents a single item insuch a list. In other words, it is an interface to a data item. And, yes, in practicethere is <strong>on</strong>e entity bean class for a table and <strong>on</strong>e instance of the class foreach row.Obviously, a J2EE c<strong>on</strong>tainer doesn’t maintain an in-memory instance forevery row of every table. In fact, you can think of both entity beans and sessi<strong>on</strong>beans as ways to automate keeping the optimal balance between in-memoryinstances for speed and data storage for memory utilizati<strong>on</strong>.Entity beans can be written to manage the persistent storage itself, usingcode added to the bean implementati<strong>on</strong> by the bean author (this is known asbean-managed persistence, or BMP), or they may be written to allow thec<strong>on</strong>tainer to automatically manage the data in the underlying database for you(this is known as c<strong>on</strong>tainer-managed persistence, or CMP). Which you use may

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!