Hello,
I have a managed bean that calls a session ejb which itself does a
search. The problem is that it is supposed to retrieve a resultset
ONCE and the displayed behavior is that it never stop doing so. It
fetches the resultset more than 15 TIMES.

Here is the managed bean and the method to look at is the performSearch method:

********************************************
package arcoiris;

import java.util.ArrayList;
import java.util.List;
import javax.faces.model.SelectItem;

public class SearchManagedBean {


   //Collected from search form
   private String keyword;
   private String country;
   private Integer[] postcode;
   private boolean commentExists;
   private Integer rating;
   private boolean websiteExists;

   //Used to populate form
   private List<SelectItem> availableCountries;
   private List<SelectItem> availablePostcodes;
   private List<SelectItem> availableRatings;

   //Retrieved from ejb tier
   private List<EstablishmentLocal> retrievedEstablishments;

   //Service locator
   private arcoiris.ServiceLocator serviceLocator;

   //Constructor
   public SearchManagedBean() {
       System.out.println("within constructor SearchManagedBean");
   }

   //Getters and setters
   public String getKeyword() {
       return keyword;
   }

   public void setKeyword(String keyword) {
       this.keyword = keyword;
   }

   public String getCountry() {
       return country;
   }

   public void setCountry(String country) {
       this.country = country;
   }



   public boolean isCommentExists() {
       return commentExists;
   }

   public void setCommentExists(boolean commentExists) {
       this.commentExists = commentExists;
   }

   public Integer getRating() {
       return rating;
   }

   public void setRating(Integer rating) {
       this.rating = rating;
   }

   public boolean isWebsiteExists() {
       return websiteExists;
   }

   public void setWebsiteExists(boolean websiteExists) {
       this.websiteExists = websiteExists;
   }

   public List<SelectItem> getAvailableCountries() {
       List<SelectItem> countries = new ArrayList<SelectItem>();

       SelectItem si_1 = new SelectItem();
       SelectItem si_2 = new SelectItem();
       SelectItem si_3 = new SelectItem();

       si_1.setValue("2");
       si_1.setLabel("ecuador");
       si_2.setValue("1");
       si_2.setLabel("colombia");
       si_3.setValue("3");
       si_3.setLabel("peru");

       countries.add(si_1);
       countries.add(si_2);
       countries.add(si_3);
       return countries;
   }

   public void setAvailableCountries(List<SelectItem> countries) {
       this.availableCountries = availableCountries;
   }

   public List<SelectItem> getAvailablePostcodes() {
       List<SelectItem> postcodes = new ArrayList<SelectItem>();
       SelectItem si_1 = new SelectItem();
       SelectItem si_2 = new SelectItem();
       SelectItem si_3 = new SelectItem();

       si_1.setValue(new Integer(75001));
       si_1.setLabel("75001");
       si_2.setValue(new Integer(75002));
       si_2.setLabel("75002");
       si_3.setValue(new Integer(75003));
       si_3.setLabel("75003");

       postcodes.add(si_1);
       postcodes.add(si_2);
       postcodes.add(si_3);

       return postcodes;
   }

   public void setAvailablePostcodes(List<SelectItem> availablePostcodes) {
       this.availablePostcodes = availablePostcodes;
   }

   public List<SelectItem> getAvailableRatings() {
       List<SelectItem> ratings = new ArrayList<SelectItem>();
       SelectItem si_1 = new SelectItem();
       SelectItem si_2 = new SelectItem();
       SelectItem si_3 = new SelectItem();

       si_1.setValue(new Integer(1));
       si_1.setLabel("1");

       si_2.setValue(new Integer(2));
       si_2.setLabel("2");

       si_3.setValue(new Integer(3));
       si_3.setLabel("3");

       ratings.add(si_1);
       ratings.add(si_2);
       ratings.add(si_3);

       return ratings;
   }

   public void setAvailableRatings(List<SelectItem> availableRatings) {
       this.availableRatings = availableRatings;
   }

   public Integer[] getPostcode() {
       return postcode;
   }

   public void setPostcode(Integer[] postcode) {
       this.postcode = postcode;
   }

   public List<EstablishmentLocal> getRetrievedEstablishments() {
       return retrievedEstablishments;
   }

   public void setRetrievedEstablishments(List<EstablishmentLocal>
retrievedEstablishments) {
       this.retrievedEstablishments = retrievedEstablishments;
   }

   //Business methods
   public String performSearch(){
       System.out.println("performSearchManagedBean begin");
       SearchRequestDTO searchRequestDto = new
SearchRequestDTO(this.keyword,this.country,this.postcode,this.commentExists,this.rating,
this.websiteExists);
       System.out.println("java bean "+this.keyword+"
"+this.country+" "+this.postcode+" "+this.commentExists+"
"+this.rating+" "+this.websiteExists);
       SearchSessionLocal searchSession = lookupSearchSessionBean();
       List<EstablishmentLocal> retrievedEstablishments =
searchSession.performSearch(searchRequestDto);
       this.setRetrievedEstablishments(retrievedEstablishments);
       System.out.println("performSearchManagedBean end");
       return "success";
   }

   private arcoiris.ServiceLocator getServiceLocator() {
       if (serviceLocator == null) {
           serviceLocator = new arcoiris.ServiceLocator();
       }
       return serviceLocator;
   }

   private arcoiris.SearchSessionLocal lookupSearchSessionBean() {
       try {
           return ((arcoiris.SearchSessionLocalHome)
getServiceLocator().getLocalHome("java:comp/env/ejb/SearchSessionBean")).create();
       } catch(javax.naming.NamingException ne) {
           
java.util.logging.Logger.getLogger(getClass().getName()).log(java.util.logging.Level.SEVERE,"exception
caught" ,ne);
           throw new RuntimeException(ne);
       } catch(javax.ejb.CreateException ce) {
           
java.util.logging.Logger.getLogger(getClass().getName()).log(java.util.logging.Level.SEVERE,"exception
caught" ,ce);
           throw new RuntimeException(ce);
       }
   }






}

********************************************


Here is the object request passed:

********************************************

package arcoiris;

import java.io.Serializable;

public class SearchRequestDTO implements Serializable {

   private String keyword;
   private String country;
   private Integer[] postcode;
   private boolean commentExists;
   private Integer rating;
   private boolean websiteExists;


   public SearchRequestDTO() {
   }

   public SearchRequestDTO(String keyword, String country, Integer[]
postcode, boolean commmentExists, Integer rating, boolean
websiteExists){
       this.keyword=keyword;
       this.country= country;
       this.postcode = postcode;
       this.commentExists = commentExists;
       this.rating = rating;
       this.websiteExists = websiteExists;
   }




   public String getKeyword() {
       return keyword;
   }

   public void setKeyword(String keyword) {
       this.keyword = keyword;
   }

   public String getCountry() {
       return country;
   }

   public void setCountry(String country) {
       this.country = country;
   }

   public Integer[] getPostcode() {
       return postcode;
   }

   public void setPostcode(Integer[] postcode) {
       this.postcode = postcode;
   }

   public boolean isCommentExists() {
       return commentExists;
   }

   public void setCommentExists(boolean commmentExists) {
       this.commentExists = commmentExists;
   }

   public Integer getRating() {
       return rating;
   }

   public void setRating(Integer rating) {
       this.rating = rating;
   }

   public boolean isWebsiteExists() {
       return websiteExists;
   }

   public void setWebsiteExists(boolean websiteExists) {
       this.websiteExists = websiteExists;
   }


}


********************************************


and here is the session ejb:

********************************************

package arcoiris;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import javax.ejb.*;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;

public class SearchSessionBean implements SessionBean,
SearchSessionLocalBusiness {
   private SessionContext context;
   private arcoiris.ServiceLocator serviceLocator;


   // <editor-fold defaultstate="collapsed" desc="EJB infrastructure
methods. Click the + sign on the left to edit the code.">
   // TODO Add code to acquire and use other enterprise resources
(DataSource, JMS, enterprise bean, Web services)
   // TODO Add business methods or web service operations
   /**
    * @see javax.ejb.SessionBean#setSessionContext(javax.ejb.SessionContext)
    */
   public void setSessionContext(SessionContext aContext) {
       context = aContext;
   }

   /**
    * @see javax.ejb.SessionBean#ejbActivate()
    */
   public void ejbActivate() {

   }

   /**
    * @see javax.ejb.SessionBean#ejbPassivate()
    */
   public void ejbPassivate() {

   }

   /**
    * @see javax.ejb.SessionBean#ejbRemove()
    */
   public void ejbRemove() {

   }
   // </editor-fold>

   /**
    * See section 7.10.3 of the EJB 2.0 specification
    * See section 7.11.3 of the EJB 2.1 specification
    */
   public void ejbCreate() {
       // TODO implement ejbCreate if necessary, acquire resources
       // This method has access to the JNDI context so resource aquisition
       // spanning all methods can be performed here such as home interfaces
       // and data sources.
   }



   // Add business logic below. (Right-click in editor and choose
   // "EJB Methods > Add Business Method" or "Web Service > Add Operation")

   public List<EstablishmentLocal> performSearch(SearchRequestDTO
searchRequestDto) {
       System.out.println("within performSearch");
       List<EstablishmentLocal> establishments = new
ArrayList<EstablishmentLocal>();
       try {
           Context ctx = new InitialContext();
           DataSource ds = (DataSource)ctx.lookup("java:/guia");
           Connection con = ds.getConnection();
           Statement stm = con.createStatement();
           String query = buildQueryFromSearchRequestDTO(searchRequestDto);
           ResultSet rs = stm.executeQuery(query);
           int rowCount = 0;
           while(rs.next()){
               rowCount = rowCount + 1;
               System.out.println("while");
               try {
                   EstablishmentLocal establishment =
lookupEstablishmentBean().findByEstablishmentId(rs.getInt("establishment_id"));
                   establishments.add(establishment);
                   System.out.println("est name
"+establishment.getEstablishmentName());
               } catch (FinderException ex) {
                   ex.printStackTrace();
               } catch (SQLException ex) {
                   ex.printStackTrace();
               }
           }
           System.out.println("rowCount = "+rowCount);
           rs.close();
           stm.close();
           con.close();

       }
       catch (SearchRequestMalformedException ex){
           ex.printStackTrace();
       }catch (NamingException ex) {
           ex.printStackTrace();
       }catch (SQLException ex){
           ex.printStackTrace();
       }

       //TODO implement performSearch
       return null;
   }

   private String buildQueryFromSearchRequestDTO(SearchRequestDTO
searchRequestDto) throws SearchRequestMalformedException {

       StringBuffer sb = new StringBuffer();

       //Head of the query
       sb.append("SELECT ");
       sb.append("e.establishment_id, ");
       sb.append("e.establishment_name, ");
       sb.append("avg(r.rating) as avg_rating ");
       sb.append("FROM ");
       sb.append("establishment e, ");
       sb.append("rating rat, ");
       sb.append("comment com, ");
       sb.append("establishment_country ec, ");
       sb.append("country_ref cou ");
       sb.append("where ");
       sb.append("e.establishment_id=ec.establishment_id and ");
       sb.append("ec.country_id=cou.country_id and ");
       sb.append("e.establishment_id=com.establishment_id and ");
       sb.append("r.establishment_id=e.establishment_id and ");

       //Conditional parts of the query

       //If user wants comment to be present
       if(searchRequestDto.isCommentExists()){
           sb.append("e.establishment_id=com.establishment_id and ");
       }
       //If user has typed in a keyword
       if(!searchRequestDto.getKeyword().equals("") &&
!searchRequestDto.getKeyword().equals(" ")&&
searchRequestDto.getKeyword()!=null){
           sb.append("e.establishment_name like '%"+
searchRequestDto.getKeyword() +"%' and ");
       }
       //If user has choosen a country
       if(searchRequestDto.getCountry()!=null &&
!searchRequestDto.getCountry().equals("") &&
!searchRequestDto.getCountry().equals(" ")&&
searchRequestDto.getCountry()!=null){
           sb.append("ec.country_id = " +
searchRequestDto.getCountry() + " and ");
       }

       //If user wants website to be present
       if(searchRequestDto.isWebsiteExists()){
           sb.append("e.establishment_website is not null and ");
       }



/* conditions

si utilisateur a renseigné un premier code postal
"e.establishment_postcode = 'postcode_one' --si racine
       si autre code postal "or e.establishment_postcode = 'postcode_two'
       ainsi de suite

and--vient de si racine
*/
       //Tail of the query
       sb.append("0=0 ");
       sb.append("group by e.establishment_id ");

       //If the user wants a minimum rating
       if(searchRequestDto.getRating() != null &&
searchRequestDto.getRating().intValue()!=0){
           sb.append("having avg(r.rating) >= " +
searchRequestDto.getRating());
       }

       sb.append(" ");
       sb.append("order by e.establishment_id ");

       System.out.println("****************************");
       System.out.println(sb);
       System.out.println("****************************");

       return sb.toString();
   }


   private arcoiris.ServiceLocator getServiceLocator() {
       if (serviceLocator == null) {
           serviceLocator = new arcoiris.ServiceLocator();
       }
       return serviceLocator;
   }

   private EstablishmentLocalHome lookupEstablishmentBean() {
       try {
           return (arcoiris.EstablishmentLocalHome)
getServiceLocator().getLocalHome("java:comp/env/ejb/EstablishmentBean");
       } catch(javax.naming.NamingException ne) {
           
java.util.logging.Logger.getLogger(getClass().getName()).log(java.util.logging.Level.SEVERE,"exception
caught" ,ne);
           throw new RuntimeException(ne);
       }
   }



}


********************************************


I just don't understand why it does so. The peformSearch should be
executed ONCE only.

Here is my face config file:

********************************************

<?xml version='1.0' encoding='UTF-8'?>


<!DOCTYPE faces-config PUBLIC
 "-//Sun Microsystems, Inc.//DTD JavaServer Faces Config 1.1//EN"
 "http://java.sun.com/dtd/web-facesconfig_1_1.dtd";>

<faces-config>
   <managed-bean>
       <managed-bean-name>SearchManagedBean</managed-bean-name>
       <managed-bean-class>arcoiris.SearchManagedBean</managed-bean-class>
       <managed-bean-scope>request</managed-bean-scope>
   </managed-bean>


   <navigation-rule>
      <description></description>
       <from-view-id>/welcomeJSF.jsp</from-view-id>
       <navigation-case>
       <from-outcome>success</from-outcome>
       <to-view-id>index.jsp</to-view-id>
       </navigation-case>
   </navigation-rule>


</faces-config>


********************************************


Can anyone help?

Thanks in advance,

Julien.

Reply via email to