230 likes | 405 Views
EJB Overview: Constraint Types and Enforcement in JBoss. Elissa Newman Fluid Meeting 6/3/04. EJB 2.0 Overview. Bean Types Entity (EB) – represent persistent state that is stored in a database or other storage. Usually represent “nouns”. Can be accessed concurrently. Long-lived.
E N D
EJB Overview:Constraint Types and Enforcement in JBoss Elissa Newman Fluid Meeting 6/3/04
EJB 2.0 Overview • Bean Types • Entity (EB) – represent persistent state that is stored in a database or other storage. Usually represent “nouns”. Can be accessed concurrently. Long-lived. • Session (SB) – models a workflow, or set of actions that a user may want to perform (actions) • Stateless – • Return values from methods do not depend on instance state • Instance state may be recorded, but it will not be persisted • A single instance can service multiple clients (long-lived) • Stateful – • Maintains conversational state between method calls • Dedicated to a single client for life of an instance • Cannot be accessed concurrently (vendor-specific) • Message-Driven (MDB) – process asynchronous (remote) messages for multiple clients, one message at a time
Ways to Access Beans • Home Interfaces – Lifecycle methods for use by client (remote or local) • Bean Interfaces – Business methods available for use by client (remote or local) • Handle – a serializable reference to a remote EJB object • Can get back same type of SB or unique instance of EB
The Big Picture: Remote Invocation • The container provides varying levels of services for the bean, including persistence, transaction management, naming directory service, and more
Implementation Options • Container (CMP) vs. Bean managed persistence (BMP) • Responsibility for writing data to database, maintaining relationships between entity beans, implementing finder methods • Container (CMT) vs. Bean managed transactions (BMT) • Responsibility for transaction support and defining transaction boundaries
Local vs. Remote Interfaces • Local – intra-JVM • Pros: saves overhead of remote calls, pass by reference • Cons: • restricts deployment configurations • pass by reference semantics can lead to data corruption • Remote – inter-JVM, uses RMI • Pros: Location transparency, pass by value, MetaData access • Cons: • huge overhead (even intra-JVM) • casting requires use of narrow() method • parameter types must be Serializable • need to handle RemoteExceptions
Lifecycle Methods • All: • ejbCreate(…) • ejbRemove() • For SBs and EBs: • ejbFindByPrimaryKey() • EB only: • ejbPostCreate() -- 1 per create() method • ejbFind*() methods • ejbSelect*() methods • ejbLoad(), ejbStore() – for BMP only • ejbSet/UnsetEntityContext() • ejbHome() • Stateful SB only: • setSessionContext() • ejbActivate() • ejbPassivate() • MDB only: • onMessage()
Major EJB Constraint Types • A _____ must inherit from _____ interface/class or one of its children • Certain lifecycle methods (per bean type) must exist and have (or not have) certain modifiers, parameters, return types • You shouldn’t use this <set> of methods for this <purpose>, instead use <other set> • Never call ____ from _____ • When ___ is called, do something
Some “Interesting” Constraints and Guidance • Remote Interfaces (and related) • Having both a remote and local interface is allowed, but not recommended • Question: When/why would this happen? • Should use EJBObject.isIdentical() instead of Object.equals() • Because remote objects can have additional networking state, but may still represent the same object • Remote interfaces may access persistence fields in a Bean class, but not relationship fields (references to other Beans) • BMP should use Collection type, although they are allowed to use Enumeration type • Make more compatible with CMP, which only uses Collection
“Interesting” (cont.) • Primary Keys (and related) • PKs only set once • In CMP, by container during creation • In BMP, in ejbCreate() method • ejbCreate() has a return type same as the Primary Key type (specified in XML file) • Always returns null for CMP • Must return the primary key for BMP • For BMP, should use EntityContext object to get the primary key, even if it is just a field in the EB • Data in bean could be stale • Rarely used outside of ejbLoad() and ejbStore() methods • PK classes must implement equals() and hashCode()
“Interesting” (cont.) • Inter-EB Relationships (and related) • If an entity bean is the target of a container-managed relationship, then it must have local interfaces and be collocated with the related bean(s) • Inter-EB relationships are not initialized until the ejbPostCreate() method • Primary key is required for use as a foreign key in other EBs, and may not be initialized until ejbCreate() has been called • Relationship fields can only access local interface of a bean • When creating related entities, need to preserve referential integrity by assigning relationship fields after new object creations • Otherwise, unconnected entries in database • Relationship fields with >1 multiplicity may never return null • Instead, they return an empty Collection
“Interesting” (cont.) • Stateful Session Beans • Must be activated and passivated to preserve conversational state of instance • On calls to ejbPassivate(), Bean should • close any open resources • Set all non-transient, non-serializable fields to null
“Interesting” (cont.) • Stateless Session Beans • Stateless SBs can only retain information between method calls via SessionContext and JNDI ENC • Also can have instance variables that won’t be persisted if instance is removed • Stateless SBs don’t ever have their create() method called, so no local object created, only one for the client
“Interesting” (cont.) • Etc. • In BMP, use only ejbLoad() and ejbStore() for synchronizing state with database • Do not use ejbActivate(),ejbPassivate(),setEntityContext(), or unsetEntityContext() for this purpose • If a system exception is thrown by a bean method, the ejbRemove() method is not invoked • Constructors should never be defined in an EB class • You should never throw RemoteExceptions from callback methods
Ensuring Constraints in JBoss • Package org.jboss.verifier provides implementation of an EJB checker • Verifier is (optionally) run during the Deployment process • Deployer may decide whether or not deployment should be terminated if an error is found by the verifier in any of the EJBs in a Jar file
Ensuring Constraints in JBoss (2) • strategy.EJBVerifier20 is most relevant class • Uses class loader to load bean class • Checks for appropriate restrictions based on if it’s a Session, Entity, or MD Bean • Checks subclassing of correct interfaces/classes by loading “to” class and calling Class.isAssignableFrom() • Checks that method names/arguments match expected • E.g., between local/remote interfaces and bean • Checks for correct return types • Checks for correct Exceptions thrown (or not) • Checks for no constructor
Future Work • Identify and describe all “interesting” EJB constraints, explicit and implicit, positive and negative • Find in JBoss implementation where these are checked, or what will happen if one of these constraints is violated • Using queries to identify possible constraint violations