Java EE 6 Enterprise JavaBeans Developer Certified Expert Exam
Question No: 11
Which two statements are correct about stateless session beans? (Choose two.)
The bean class may declare instance variables.
The lifetime of the bean instance is controlled by the client.
The container may use the same bean instance to handle multiple business method invocations at the same time.
The container may use the same bean instance to handle business method invocations requested by different clients, but not concurrently.
Explanation: * A: Stateless session beans are EJB#39;s version of the traditional transaction processing applications, which are executed using a procedure call. The procedure executes from beginning to end and then returns the result. Once the procedure is done, nothing about the data that was manipulated or the details of the request are remembered. There is no state.
These restrictions don#39;t mean that a stateless session bean can#39;t have instance variables and therefore some kind of internal state. There#39;s nothing that prevents you from keeping a variable that tracks the number of times a bean has been called or that tracks data for debugging. An instance variable can even hold a reference to a live resource like a URL connection for writing debugging data, verifying credit cards, or anything else that might be useful.
C:A stateless session bean is relatively easy to develop and also very efficient. Stateless session beans require few server resources because they are neither persistent nor dedicated to one client. Because they aren#39;t dedicated to one client, many EJB objects can use just a few instances of a stateless bean. A stateless session bean does not
maintain conversational state relative to the EJB object it is servicing, so it can be swapped freely between EJB objects. As soon as a stateless instance services a method invocation, it can be swapped to another EJB object immediately. Because there is no conversational state, a stateless session bean doesn#39;t require passivation or activation, further reducing the overhead of swapping. In short, they are lightweight and fast!
The Lifecycle of a Stateless Session Bean
Because a stateless session bean is never passivated, its lifecycle has only two stages: nonexistent and ready for the invocation of business methods.
The EJB container typically creates and maintains a pool of stateless session beans, beginning the stateless session bean’s lifecycle. The container performs any dependency injection and then invokes the method annotated @PostConstruct, if it exists. The bean is now ready to have its business methods invoked by a client.
At the end of the lifecycle, the EJB container calls the method annotated @PreDestroy, if it exists (not B). The bean’s instance is then ready for garbage collection.
Question No: 12
A developer wants to release resources within a stateless session bean class. The cleanup method should be executed by the container before an instance of the class is removed.
The deployment descriptor is NOT used.
Which three statements are correct? (Choose three.)
The cleanup method may declare checked exceptions.
The cleanup method must have no arguments and return void.
The cleanup method is executed in an unspecified transaction and security context.
The developer should mark the cleanup method with the @PreDestroy annotation.
The developer should mark the cleanup method with the @PostDestroy annotation.
The cleanup method is executed in the transaction and security context of the last business method Invocation.
Question No: 13
A developer creates a stateless session bean. This session bean needs data from a remote system. Reading this data takes a long time. Assume that the data will NOT change during the life time of the bean and that the information to connect to the remote system is defined in JNDI.
Which statement describes how to manage the data correctly?
Read the data in the bean#39;s constructor.
The data can only be read in the bean#39;s business methods.
Read the data in a method which is annotated with @PrePassivate.
Read the data in a method which is annotated with @PostActivate.
Read the data in a method which is annotated with @PostConstruct.
Question No: 14
Suppose an EJB named HelloWorldBean is deployed as a standalone ejb-jar. Assuming the HelloWorldBean is implemented as follows:
Which HelloWorldBean methods are accessible by another EJB within the same ejb-jar?
All of the methods declared in HelloWorldBean
All of the methods declared in HelloWorld and HelloWorldBean
All of the public methods declared in HelloWorldBean
All of the public methods declared in HelloWorld andall of the methods declared in HelloWorldBean
Question No: 15
Given the following stateless session bean:
How would you change the EJB to prevent multiple clients from simultaneously accessing the sayHello method of a single bean instance?
Convert sayHello into a synchronized method
Execute the call to generateLocalizedHello in a synchronized block
Convert generateLocalizehello into a synchronized method
Convert HelloWordBean into a singleton bean
No changes are needed
Explanation: * It is not possible for two invocations of synchronized methods on the same object to interleave. When one thread is executing a synchronized method for an object, all other threads that invoke synchronized methods for the same object block (suspend execution) until the first thread is done with the object.
When a synchronized method exits, it automatically establishes a happens-before relationship with any subsequent invocation of a synchronized method for the same object. This guarantees that changes to the state of the object are visible to all threads.
Reference: The Java Tutorial, Synchronized Methods
Question No: 16
Given singleton bean FooEJB:
How many distinct FooEJB bean instances will be used to process the code on the lines 101-105?
Explanation: Java has several design patterns Singleton Pattern being the most commonly used. Java Singleton pattern belongs to the family of design patterns, that govern the instantiation process. This design pattern proposes that at any time there can only be one instance of a singleton (object) created by the JVM.
Question No: 17
A developer writes a Singleton bean that holds state for a single coordinate:
An update thread acquires an EJB reference to CoordinateBean and alternates between invoking SetCoordinate (0, 0) and SetCoordinate (1, 1) in a loop.
At the same time, ten reader threads each acquire an EJB reference to CoordinateBean and invoke getCoordinate () in a loop.
Which represents the set of all possible coordinate values [X, Y] returned to the reader threads?
A. [0, 0]
B. [1, 1]
C. [0, 0], [1, 1]
D. [0, 0], [0, 1], [1, 0], [1, 1]
Explanation: The value could be either [0,0] or [1,1].
Java has several design patterns Singleton Pattern being the most commonly used. Java Singleton patternbelongs to the family of design patterns, that govern the instantiation process. This design pattern proposes that at any time there can only be one instance of a singleton (object) created by the JVM.
Question No: 18
Assume a client will be accessing a Singleton bean.
Which client views is a Singleton bean capable of exposing? (Choose two)
EJB 2.x Remote Home
EJB 3.x local business
Java Persistence API entity
Explanation: Singleton session beans are appropriate in the following circumstances.
State needs to be shared across the application.
A single enterprise bean needs to be accessed by multiple threads concurrently.
The application needs an enterprise bean to perform tasks upon application startup and shutdown.
The bean implements a web service. (A)
B: An interceptor method you define in a separate interceptor class takes an invocation context as argument: using this context, your interceptor method implementation can access details of the original session bean business method or message-driven bean message listener method invocation.
If your interceptors are stateless, you can use an OC4J optimization extension to the EJB
3.0 specification that allows you to specify singleton interceptors. When you configure a session bean or message-driven bean to use singleton interceptors and you associate the bean with an interceptor class, OC4J creates a single instance of the interceptor class that all bean instances share. This can reduce memory requirements and life cycle overhead.
Singleton session beans offer similar functionality to stateless session beans but differ from them in that there is only one singleton session bean per application, as opposed to a pool of stateless session beans, any of which may respond to a client request. Like stateless session beans, singleton session beans can implement web service endpoints.
Reference: The Java EE 6 Tutorial, What Is a Session Bean?
Reference: Oracle Containers for J2EE Enterprise JavaBeans Developer#39;s Guide, How do you use an Enterprise Bean in Your Application
Question No: 19
A developer writes a Singleton bean that uses the java Persistence API within a business method:
Two different concurrently executing caller threads acquire an EJB reference to PersonBean and each invoke the getPerson () method one time. How many distinct transaction are used to process the caller invocations?
Explanation: Only one transaction is required. LockType READ allows simultaneous access to singleton beans.
public static final LockType READ
For read-only operations. Allows simultaneous access to methods designated as READ, as long as no WRITE lock is held.
Reference: javax.ejb, Enum LockType
Question No: 20
Given the following client-side code that makes use of the session bean Foo:
10. @EJB Foo bean1;
12. @EJB Foo bean2;
// more code here
boolean test1 = beanl.equals(bean1);
boolean test2 = beanl.equals(bean2) ; Which three statements are true? (Choose three)
If Foo is stateful, test1 is true, and test2 is true.
If Foo is stateful, test1 is true, and test2 is false.
If Foo is stateless, test1 is true, and test2 is true.
If Foo is stateless, test1 is true, and test2 is false.
If Foo is singleton, test1 is true, and test2 is true.
If Foo is singleton, test1 is true, and test2 is false.
|Lowest Price Guarantee||Yes||No||No|
|Free VCE Simulator||Yes||No||No|