|
31) Can beans use stored procedures in a
database?
Stored procedures can be used by session beans
that access the database using JDBC and bean-managed entity beans that use
JDBC to manage their own persistence. JDBC provides a call interface for using
stored procedures. An example is provided below:
InitialContext cntx = new InitialContext( );
DataSource dataSource = (DataSource)
cntx.lookup("java:comp/env/jdbc/mydatabase");
Connection con = dataSource.getConnection( );
CallableStatement storedProcedure =
con.prepareCall("{? = call someprocedure [(?,?)]}");
32) Is method overloading allowed in
EJB?
Yes you can overload
methods.
33) How can JMS be used from EJB
1.1?
The same as any client would use JMS. At this
point there is no integration, but it is planned for a future release of the
EJB spec.
34) Can primary keys contain more than one
field?
Yes, a primary key can have as many fields as
the developer feels is necessary, just make sure that each field you specify
as the primary key, you also specify a matching field in the bean class. A
primary key is simply one or more attributes which uniquely identify a
specific element in a database. Also, remember to account for all fields in
the equals() and hashCode() methods.
35) How does Container Managed Persistence
work with automatically generated database ID fields? Should I map the ID
field explicitly or leave it unspecified?
In the Deployment Descriptor, map the normal
fields appropriately, but don't specify the auto-id field as one of the
container managed fields.
36) Let's assume I use a JavaBean as a
go-between a JSP and an EJB, and have, say, 50 concurrent clients that need to
access the EJB functionality. Will the JSP container actually instantiate 50
instances of the bean, or can it reuse a single instance to access the EJB?
It depends on the scope you associate with the
JavaBean. If you assign the bean with page (which is the default) scope or
request scope, a new bean will be instantiated for each incoming request.
If you assign the bean with session scope, you
will still have 50 instances loaded in memory (assuming each incoming request
is triggered by a distinct client), although some may have been instantiated
from an earlier request from the same client. However, you may not want to use
the session scope for a high-volume site as these beans will continue to
reside in memory, long after the request has been serviced, consuming valuable
resources until they are invalidated either explicitly or due to a session
timeout.
You can also assign the bean with application
scope, in which case it is instantiated just once before being placed into the
servlet context of the container. It can then be accessed at a later time, as
long as the server is up and running. Although this may sound like an
attractive proposition, do note that you will have to contend with significant
multithreading issues. For instance, you'll have to ensure that the bean is
accessed in a thread-safe manner from each of the JSP files. While you can do
this using explicit synchronization from within the JSP file, do note that
your application may take a significant performance hit because of this -
especially if you expect tens or hundreds of concurrent clients accessing your
pages.
So, in short, your best bet may be to assign
the bean with request scope.
37) What happens when two users access an
Entity Bean concurrently?
Taken from Enterprise JavaBeans by Richard
Monson-Haefel, "EJB, by default, prohibits concurrent access to bean
instances. In other words, several clients can be connected to one EJB object,
but only one client thread can access the bean instance at a time. If, for
example, one of the clients invokes a method on the EJB object, no other
client can access that bean instance until the method invocation is complete."
So, to answer your question, two users will
never access an Entity Bean concurrently.
38) What's the reason for having two
interfaces -- EJBHome for creating, finding & removing and EJBObject for
implementing business methods. Why not have an single interface which supports
both areas of functionality?
This design reflects the common "Factory"
Design pattern. The EJBHome interface is the Factory that creates EJBObjects.
EJBObject instances are the product of the factory. The reason for having two
interfaces is because they are both responsible for different tasks. The
EJBHome is responsible for creating and finding EJBObjects, whilst the
EJBObject is responsible for the functionality of the EJB.
39) Which fields in beans should be
public?
All Container Managed Fields in an Entity Bean
must be public.
Ejb 1.1 spec section 9.4.1 - "The fields must
be defined in the entity bean class as public, and must not be defined as
transient."
40) How do you implement callbacks in
EJB?
If your client is an EJB, it can pass a
reference to itself to the method of the bean that it is calling. The EJB can
then call methods directly on that interface.
If your client is a Java client, your client
requires some sort of object that will "listen" for call-backs. This could be
either a CORBA or RMI object. Again, you could pass references to these
objects to the EJB, which could then invoke methods on the references.
|
|
|
Bookmark This Page :
|
|
|
|
|
|
|
|
|
|
|
|
java6 ejb3 jsf hibernate eclipse ajax groovy spring seam java struts webservice j2me guice java5 jca tapestry soa linux ria |