The container-managed search described in Chapter 2 is based on a
finder method mechanism where the deployment
descriptor, rather than the Bean, specifies the finder
method behavior. While the finder mechanism works well for simple
queries and searches, it cannot handle complex operations that span more
than one Bean type or database table. Also, the Enterprise
JavaBeansTM 1.1 specification currently provides no specification for
putting finder rules in the deployment descriptor.
So, for more complex queries and searches, you have to write Bean-managed
queries and searches. This section explains how to write a Bean-managed
version of the auction house search facility from Chapter 2. The
Bean-managed search involves changes to the
AuctionServlet.searchItems method
and a new session Bean, SearchBean .
AuctionServlet.searchItems
The search begins when the end user submits a search string
to the search facility on the auction house home page, and
clicks the Submit button. This invokes
AuctionServlet , which retrieves the
search string from the HTTP header and passes it to the
searchItem method.
Note:
The search logic for this example is fairly simple.
The purpose is to show you how to move the search
logic into a separate Enterprise Bean so you
can create a more complex search on your own.
The searchItem operation is in two parts: 1) Using the
search string to retrieve primary keys, and 2) Using primary
keys to retrieve auction items.
Part 1:
The first thing the searchItems
method does is pass the search string submitted by the end user
to the SearchBean session Bean.
SearchBean
(described in the next heading) implements a Bean-managed search that
retrieves a list of primary keys for all auction items whose
Summary fields contain characters matching the search string.
This list is returned to the searchItems
method in an Enumeration variable.
Enumeration enum=(Enumeration)
search.getMatchingItemsList(searchString);
Part 2:
The searchItems method then uses the
returned Enumeration list from Part 1
and
AuctionItemBean
to retrieve each Bean in turn by calling
findByPrimaryKey on each primary
key in the list. This is a container-managed
search based on the finder mechanism described in Chapter 2.
//Iterate through search results
while ((enum != null) &&
enum.hasMoreElements())) {
while(enum.hasMoreElements(in)) {
//Locate auction items
AuctionItem ai=ahome.findByPrimaryKey((
AuctionItemPK)enum.nextElement());
displayLineItem(ai, out);
}
}
SearchBean
The SearchBean.java
class defines a Bean-managed search for the primary keys
of auction items with summary fields that
contain characters matching the search string. This Bean
establishes a database connection, and
provides the getMatchingItemsList and
EJBCreate methods.
Database Connection
Because this Bean manages its own database access and search, it
has to establish its own database connection. It cannot rely on the
container to do this.
The database connection is established by instantiating a static
Driver class and providing the
getConnection method.
The getConnection method queries the static
DriverManager class for a registered database driver
that matches the Uniform Resource Locator (URL) . In this case, the URL is
weblogic.jdbc.jts.Driver .
//Establish database connection
static {
new weblogic.jdbc.jts.Driver();
}
public Connection getConnection()
throws SQLException {
return DriverManager.getConnection(
"jdbc:weblogic:jts:ejbPool");
}
Get Matching Items List
The getMatchingItemsList method
looks up AuctionItemsBean
and creates a PreparedStatement object for querying
the database for summary fields that contain
the search string. Data is read from the database into a
ResultSet , stored in a Vector , and
returned to AuctionServlet .
public Enumeration getMatchingItemsList(
String searchString)
throws RemoteException {
ResultSet rs = null;
PreparedStatement ps = null;
Vector v = new Vector();
Connection con = null;
try{
//Get database connection
con=getConnection();
//Create a prepared statement for database query
ps=con.prepareStatement("select id from
auctionitems where summary like ?");
ps.setString(1, "%"+searchString+"%");
//Execute database query
ps.executeQuery();
//Get results set
rs = ps.getResultSet();
//Get information from results set
AuctionItemPK pk;
while (rs.next()) {
pk = new AuctionItemPK();
pk.id = (int)rs.getInt(1);
//Store retrieved data in vector
v.addElement(pk);
}
rs.close();
return v.elements();
}catch (Exception e) {
System.out.println("getMatchingItemsList:
"+e);
return null;
}finally {
try {
if(rs != null) {
rs.close();
}
if(ps != null) {
ps.close();
}
if(con != null) {
con.close();
}
} catch (Exception ignore) {}
}
}
Create Method
The ejbCreate method creates an
javax.naming.InitialContext object. This is a
JavaTM Naming and Directory (JNDI) class that lets
SearchBean access the database without
relying on the container.
public void ejbCreate() throws CreateException,
RemoteException {
Properties p = new Properties();
p.put(Context.INITIAL_CONTEXT_FACTORY,
"weblogic.jndi.TengahInitialContextFactory");
try{
ctx = new InitialContext(p);
}catch(Exception e) {
System.out.println("create exception: "+e);
}
}
[TOP]
|