Deployment

April 22, 2008 at 3:52 am (EJB, Unit 3)

Deployment

  1. Must include the names of the home interface, remote interface, and implementation class.
  2. Set the isolation level and transaction attribute for the bean.
  3. Deployment Descriptor must be of type javax.ejb.deloyment.EntityDescriptor
  4. Include the name of the primary key class.
  5. Provide the list of container-managed fields
  6. Must specify whether the bean is reentrant or not.
  7. Provide a description for the finder methods, so that the deployer can use the container to generate them.

 

  • BEA Weblogic uses findByCustID(), custom finder method, as follows:

    “findByCustID(int cusidnum)” “(=custid $custidnum)”

  • This method takes one integer argument called custidnum.
  • The expression “(=custid $custidnum)” is a prefix-notation expression that indicates that the method should return all instances where the database column that maps to the field custid matches the argument of the finder method exactly.

Permalink Leave a Comment

Entity Bean Life Cycle

April 22, 2008 at 3:49 am (EJB, Unit 3)

 

  • An entity bean can have only three states:
    • Non-existence
    • Pooled
    • Ready
  • Non-existence
    • In this state, the bean doesn’t exist.
    • A bean leaves this state and enters the polled state when it is added into the pool by the container, at which point newInstance() and setEntityContext() are invoked on it.
    • It enters the state of non existence when the container removes it from the pool, at which point unsetEntityContext() and finalize() are called on it.
  • Pooled state
    • In this state, entity beans have no identity. So all are identical.
    • When an ejbFind() method is invoked, the container uses an EJB instance from the pooled state to handle the request.
    • A bean transitions from the poled state to the ready state when one of two things happen:
      • The container selects the instance to handle an ejbCreate() request, at which point ejbCreate() and ejbPostCreate() are invoked on the object.
      • The container selects the instance to be activated, at which point ejbActivate() is invoked on the instance.
    • The ejbPostCreate() method takes the same arguments as the ejbCreate() method. It is invoked only after ejbCreate() has reached completion successfully.
    • ejbPostCreate() coded to perform any special processing needed after the beanis created, but before it becomes available to the client.
    • A bean transitions from the ready state to the pooled state when one of two things happens:
      • The container selects the instance for passivation at which point ejbPassivate() is called on the object.
      • The client invokes a remove() method on the instance, at which point the instance’s ejbRemove() method is invoked.
    • When a bean returns to pooled state, it once again loses its identity and becomes one of several available instances in the pool.
  • Ready State:
    • In this state, beans accept invocations of its business methods. It can also process invocations of its ejbLoad() and ejbStore() methods.
    • The container keeps the state of the bean with its representation in the database.
    • ejbLoad() reads data from the database and uses it to popular instance variables, and ejbStore() writes data from the instance variables to the database.
    • When a bean is activated, the container automatically invokes its ejbLoad() method.
    • Similarly, before the bean becomes passivated, its ejbStore() method is invoked.
    • So, no logic needed in the ejbActivate() and ejbPassivate() to handle state synchronization between the objectand the database.
  • Reentrant Instances
    • It is possible, in narrow circumstances, to make an entity EJB reentrant.
    • This situation occurs when the entity bean uses another entity bean as a client, and the remote entity bean must perform a callback parent bean.

 

Key points:

  • All entity beans must provide a findByPrimaryKey() method in their home interface. Clients use this finder method to locate existing entity beans based on their primary key.
  • The implementation details of the finder methods also vary depending on whether the bean has bean-managed or container-managed persistence.
  • If the bean has bean-managed persistence the developer must implement a finder method that is declared in the home interface.
  • If the bean has container-managed persistence, the container will provide implementations of the finder methods; developers need not to implement them.
  • The container can easily generate an implementation of the findByPrimaryKey() method, because it know exactly what it needs to do and receives the primary key class.
  • getEJBObject() returns a reference to the Entity bean’s EJBObject.
  • getPrimaryKey() returns an object that contains the primary key for this Entity Object.
  • In the client side, each method invocation must perform the following tasks
    • Serialize all arguments on the client side.
    • Transmit the arguments to the server
    • Deserialize the arguments on the server
    • Invoke the remote method
    • Serialize the return value on the server
    • Transmit the return value to the client
    • Deserialize the return value on the client side.

Permalink Leave a Comment

Primary Keys

April 22, 2008 at 3:46 am (EJB, Unit 3)

Primary Keys

  • Each entity bean instance has an associated primary key.
  • Logically, a primary key is a value or combination of values that allows you to uniquely specify a row of data.
  • In EJB, the primary key is represented by a Java class that contains whatever unique data are necessary to lookup a particular entity EJBs.

    Example:

        pubic class CustomerPK implements java.io.Serializable

        {

            public int custid;

        }

  • According to the EJB specification, a primary key class must implement the java.io.Serializable interface.
  • This class simply carries the value for the primary key from the client to the server.
  • The server inspects the class and uses the values it contains to perform a database lookup for the object of the requested primary key.
  • If two entity beans have the same home interface and the same primary key, they are considered to be identical.

Permalink Leave a Comment

BMP and CMP

April 22, 2008 at 3:44 am (EJB, Unit 3)

Bean Managed and Container-Managed Persistence

  • There are two types of entity beans:
    • Entity beans with bean-managed persistence
    • Entity beans with container-managed persistence
  • An entity bean with bean-managed persistence contains code that updates the underlying database.
  • An entity bean with container-managed persistence contains no such code; instead, it relies on the container to update the database as necessary.
  • This is simpler to implement but require a high degree of functionality from their container.
  • Beans with bean-managed persistence are slightly more complex to implement, but do not require extra support from the container.

Permalink Leave a Comment

When to use Entity Beans

April 22, 2008 at 3:38 am (EJB, Unit 3)

Entity Beans

When to use Entity Beans:

  • To understand entity beans, a brief recap of session beans is helpful. Session beans are:
    • Intended to be used by a single client.
    • Relatively short-lived – they are intended to last for the duration of the client’s session with the server.
    • Not able to survive a server crash
    • Capable of interacting with shared data in a database.
  • Entity beans by contrast,
    • Can be used concurrently by several clients
    • Are relatively long-lived – they are intended to exist beyond the lifetime of any single client
    • Will survive a server crash.
    • Directly represent data in a database.

 

  • Concurrent use by Several clients
    • More than one client will have access to the bean concurrently.
    • Although the clients all think that they’re accessing the bean freely, in reality this interaction is interposed by the container so as to guarantee the integrity of the database.
    • A container may interpose on client interactions with an entity bean in two ways to guarantee database integrity.
      • The guarantee can directly interpose, queuing client request and allowing only one request at a time to be executed.
      • The container can create an instance of the bean for each client that wishes to use it, and then allow the underlying database to deal with synchronization issues.
  • Long lifetime:
    • Entity beans represent data in an underlying database. Therefore, their lifetime matches the lifetime of the underlying data itself.
    • Entity EJBs may continue to exist for days, months or even years.
    • No upper limit exists on the lifetime of entity EJBs and they can be removed in only two ways:
      • A client explicitly invokes the remove() method on the bean’s remote interface or home interface.
      • Someone deletes the bean’s data from the underlying database.
    • Another interesting fact about an entity bean’s lifetime is that an Entity bean does not necessarily need to provide a create() method, a client will bet be able to create new entity EJB via the bean’s home interface.
    • Nevertheless, one can create a new entity EJB by directly inserting a row of data into its underlying database.
    • Even without a create() method, a client can still obtain a reference to an existing Entity EJB by using one of its finder methods.
  • Survival of Server Crashes:
    • The session bean could be irrevocably destroyed in three ways:
      • The client connection to the bean can timeout
      • The server can be restarted
      • The server can crash
    • None of these three problem applies to entity beans:
      • There is no timeout period associated with entity beans.
      • Entity beans are guaranteed to survive an orderly server restart.
      • Entity beans are guaranteed to survive a server crash.
  • Direct representation of data in an underlying database:
    • Typically an entity bean maps directly to a row in an underlying database.
    • For example, a table called CUSTOMERS with the following columns:
      • An integer called CUST_ID that represents a unique ID for each customer.
      • A string called CUST_NAME that contains the customer’s name.
      • A string called CUST_ADDR that contains the customer’s address.
    • Now an entity bean called “Customer” can be created that contains three instance variables:
      • Cust_id
      • Cust_name
      • Cust_addr
    • These variables map directly to the columns in the underlying CUSTOMERS table.
    • A client application can then interact with the customer EJBs to modify these fields, update the underlying database, or perform any other business logic necessary on the customer object.

Permalink Leave a Comment

Transaction Isolation Levels

April 22, 2008 at 3:32 am (EJB, Unit 3)

Transaction Isolation Levels:

  • This allows to specify a bean how much or how little of other transactions can be viewed during the course of its own transaction execution.
  • An inverse relationship exists between higher levels of isolation and the level of concurrency. That is, an application that requires a high degree of isolation must maintain more locks for a longer period of time than an application for which a lower degree of isolation is acceptable.
  • Two programs attempting to simultaneously access a database can have essentially three types of problem interactions.
    • Transaction A modifies a row; transaction B reads it; transaction A does a rollback and the row disappears. This situation is called “dirty read”.
    • Transaction A reads a row; transaction B modifies the row; transaction A reads the row again and sees a different value. This case is called a “non-repeatable read”.
    • Transaction A selects a set of rows that satisfy a given search condition; Transaction B inserts rows that satisfy the same condition. If transaction A executes the same query again, it will receive additional data. This situation is called a “phantom read”.
  • Four levels of transaction isolations are possible:
    • TRANSACTION_READ_UNCOMMITTED – Permits dirty reads, non-repeatable read and phantom reads.
    • TRANSACTION_READ_COMMITTED – Permits only non-repeatable reads and phantom reads.
    • TRANSACTION_REPEATABLE_READ – Permits only phantom reads.
    • TRANSACTION_SERIALIZABLE – does not permit any of these situations to occur.

Permalink Leave a Comment

Transactions and EJB

April 22, 2008 at 3:29 am (EJB, Unit 3)

Transactions and EJB

  • Table shows the various transaction specifiers, illustrating which transaction is in effect in various situations.
  • The table includes two rows for each type of transaction support.
  • The first row indicates what occurs if the client calls the bean without a transaction context; the second row indicates what happens if the client calls the bean with a transaction in effect.

 

Transaction Attribute

Client’s TX

Bean’s TX

TX_NOT_SUPPORTED

None

None

T1

None

TX_REQUIRED

None

T2

T1

T1

TX_SUPPORTS

None

None

T1

T1

TX_REQUIRES_NEW

None

T2

T1

T2

TX_MANDATORY

None

Error

T1

T1

 

  • TX_NOT_SUPPORTED means that the bean should never be called within a transaction
    • If a client calls the bean without a transaction in effect, the bean method is invoked.
    • If the client calls the bean while the client has a transaction in effect, the container suspends the client’s transaction temporarily, invokes the bean method, and after the method has finished executing resumes the client’s transaction.
  • TX_REQUIRED means that the bean’s methods must always be invoked within a transaction.
    • If the client does not currently have a transaction in effect, the container creates a new transaction, invokes the method within the transaction, and then terminates the transaction prior to returning to the client.
    • If the client does have a transaction in effect, it serves as the transaction context for the bean’s method invocation.
  • TX_SUPPORTS means that the bean can be invoked in a client supplied transaction context.
    • If the client does not supply a transaction context, it invokes the bean’s method with no transaction context.
    • If the client supplies a transaction context, this context is used as the transaction context for the bean’s method.
  • TX_REQUIRES_NEW means that the bean’s method must always be invoked in a new transaction context.
    • If the client has not supplied a transaction context, the EJB container creates a new one.
    • If the client has supplied a transaction context, the EJB container creates a new one anyway, suspending the client’s transaction context until the bean’s transaction has been completed.
  • TX_MANDATORY means that the client is required to supply a transaction context
    • If the client attempts to invoke a TX_MANDATORY bean without a transaction context, the container will throw a TransactionRequiredException to the client.
    • If the client supplies a transaction context, the bean will use this context to invoke the method.

 

  • TX_BEAN_MANAGED is a bit of a special case:

Client’s TX

Instance’s Current TX

TX Used by method

None

None

None

T1

None

None

None

T3

T3

T1

T3

T3

  • In the first case, neither the client nor the bean has a transaction context open. The bean’s method is therefore invoked without a transaction context.
  • In the second case, the client has a transaction context open, but the bean does not. The client’s transaction is suspended, and the bean is invoked with no transaction context.
  • In the third case, the client has no transaction context open, but the bean does. The bean’s method is invoked in its own transaction context.
  • In the fourth case, the client has a transaction context open, as does the bean. The client’s transaction is suspended, the method is invoked in the bean’s transaction context, and the client’s transaction context is resumed after the method completes.
  • TX_BEAN_MANAGED transaction also differs depending on whether the session bean is either stateful or stateless:
    • A stateful bean can maintain an open transaction across method invocations. That is, it can open a transaction in one method, respond to several method invocations within that transaction, and then close the transaction in yet another method. In this situation, the container suspends the bean’s transaction context after each invoked method completes, then resumes it when the next method is invoked.
    • A stateless bean cannot maintain an open transaction across method invocations. These can potentially be invoked by many different clients during the course of their life, so it would be erroneous to attempt to maintain an open transaction context across different method invocations. If a stateless bean uses transactions, each transaction must either commit or rollback before the method returns.

Permalink Leave a Comment

Session Bean Life Cycles

April 22, 2008 at 3:26 am (EJB, Unit 3) ()

Session Bean Life Cycle

  • Stateless session bean has only two states:
    • Non-existence
    • Method-ready state

  • newInstance() – Allocates space for the bean and brings it into existence state.
  • setSessionContext() – Gives the bean a Session Context.
  • ejbCreate() – Allows the bean to perform any necessary initializations
  • SessionContext points to a dynamic object that the container updates each time a stateless session bean’s methods are invoked.
  • So, even though the bean is stateless, the access to the useful method of SessionContext interface is retained.
  • A stateless session bean can have only one ejbCreate() method, and it must take no arguments.
  • This initializes the instance’s internal state variables. Because a stateless session bean has no specific state variables, it does not require any arguments.
  • Even though the client invokes create() and remove(), these methods do not necessarily result in an actual instance being created or removed in the container.
  • The container will maintain a pool of stateless beans. This pool will be large enough to ensure that any client that needs to invoke a method receives an instance.
  • The system administrator will normally set the number of stateless session beans in the instance pool, adjusting this number as necessary to provide the required level of service.
  • A container might also manage the pool of instances itself, creating and removing instances as necessary to serve the current client load.

  • newInstance() – Allocates space for the bean and brings it – into existence.
  • setSessionContext() – Gives the bean a SessionContext
  • ejbCreate() – Allows the bean to perform any necessary initializations.
  • Because stateless beans have no state, they can be reused by many clients in the course of their existence.
  • A stateful session bean can implement any of several ejbCreate() methods that take different sets of arguments.
  • Only one of the available ejbCreate() methods will be invoked at creation time, however.
  • Once the ejbCreate() method executes the bean enters the “method ready” state.
  • It remains in this state as long as only non-transactional methods are invoked on it.
  • Three transitions out of this state are possible.
    • The bean can enter a transaction
    • It can be passivated
    • It can be removed.
  • The bean enters a transaction if one of its transactional methods is invoked. (should contain transaction declaratory TX_BEAN_MANAGED, TX_REQUIRED, TX_REQUIRES_NEW, TX_MANDATORY, for TX_SUPPORTS caller should have a transaction)
  • If the bean implements the SessionSynchronization interface, the container invokes its afterBegin() method to notify it that it is entering a transaction.
  • The bean then moves to the “method-ready in transaction” state, where it remains until the transaction concludes.
  • If the transaction was created specifically for the method invocation, the bean will exit the “method-ready in transaction” state immediately.
  • For the TX_BEAN_MANAGED, the bean remains in this state until a commit or rollback occurs.
  • If a commit occurs while the bean is in the “method-ready in transaction” state, the beforeCompletion() method of the SessionSynchronization interface is invoked with a boolean value of true as the argument.
  • This sequence of invocations allows the bean to perform any database updates, before the transaction commits, and provides it with definitive notification that the transaction has successfully committed. After these methods finish the bean returns to the “method-ready” state.
  • On the other hand, if a rollback occurs while the bean is in the “method-ready in transaction” state, beforeCompletion() is not invoked; rather afterCompletion is invoked with a boolean argument of false.
  • If the bean is in the “method-ready in transaction” state, don’t attempt to call the bean in a different transaction context, this will lead to an error state.

  • When the bean is passivated, the container invokes its ejbPassivate() method and then writes it out to some type of persistent storage; it also removes the bean from memory.
  • A bean in the “method-ready in transaction” state cannot be passivated; only beans in the “method-ready” state are subject to passivation.
  • If the bean have opened any database or socket connections from the ejbCreate() method, the code should included in the ejbPassivate() method to close these connections.
  • A bean cannot maintain open database or socket connections when it is passivated.
  • These connections are reestablished again when the beans are activated.
  • The final transaction is out of the “method-ready” state is caused by the removal of the bean.
  • Unlike stateless session beans, stateful session beans are actually removed by the container in response to a client’s call to the remove() method on the bean’s remote interface.
  • Before removing the bean, the container calls the beans ejbRemove method, which gives it one last opportunity to write out database updates before it passes out of existence. This will close all the opened database and socket connections.
  • After ejbRemove() has executed, the bean is destroyed and cannot be resurrected.

Permalink Leave a Comment

Session Beans Constraints

April 22, 2008 at 3:13 am (EJB, Unit 3)

Session Beans Constraints

 

  • Session beans disappear from the server, if
    • The session bean’s timeout value expires
    • The server crashes and is restarted
    • The server is shutdown and restarted
  • If any of these event occurs, state information of the bean is lost.

Solutions:

  1. Session timeout value is configurable so the timeout value can be increased.
  2. Server crashes and restarts are fairly rare events.
    1. The user can be notified that non-recoverable error has occurred and ask the user to try again later.
    2. A copy of the state of the bean can be cached at the client side and a new bean can be recreated in the event of original bean dies.
    3. Use a database on the server side to hold the state of the client’s conversation. If the session bean dies during the conversation, the client can reconnect and pass in a unique customer identifier to query the database to retrieve whatever state information was saved prior to the failure of the session beans.

 

  • A session bean’s state is not transactional. If the internal state of the bean is modified, the rollback will not affect the session bean’s internal state.

Solution:

  • The session bean’s state should be reset manually to whatever it was before the transaction began.
  • A stateless session bean cannot implement the SessionSynchronization interface. Because stateless session beans lack a conversational state and cannot hold a transaction across method invocations.
  • The final constraint on session beans is that they are not reentrant. If one thread is currently using a session bean, the container will throw a java.rmi.RemoteException in response to any other requests to connect to the session bean.

 

Permalink Leave a Comment

Constraints on Beans

April 22, 2008 at 3:08 am (EJB, Unit 3)

Common Constraints for all beans:

  • EJB cannot start new threads or use thread synchronization primitives.
    • Container is responsible for creating and managing new bean instances
    • EJB beans are not allowed to spawn threads themselves, but the container can run multiple beans concurrently.
    • This prevention is needed not to interfere the container’s management of its beans
  • EJB cannot directly access the underlying transaction manager
    • Because the container has the responsibility for managing transactions.
    • If bean declares its transaction management as TX_BEAN_MANAGED, is allowed to use a javax.jts.UserTransaction to manage the state of its own transaction.
  • The bean cannot use the JDBC commit and rollback primitives.
    • This constraints exists because the container is responsible for issuing the commit and rollback instructions for transaction-enabled beans.
    • A bean with a transaction attribute of TX_NOT_SUPPORTED, however, can use JDBC commit or rollback, because no external transaction manager exists.
  • Beans are not allowed to change their java.security.Identity at runtime.
    • The identity is typically set up at deployment time by the system administrator
    • Runtime changes are disallowed to prevent malicious beans from acquiring unintended privileges.
  • EJB cannot have read/write static variables. But “static final” variables are allowed.

Permalink Leave a Comment

Next page »