Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

records not fetching correctly after remote call

Posted on 2005-04-22
5
Medium Priority
?
166 Views
Last Modified: 2010-03-31
in the servlet i am calling remote method

    ArrayList totalList                  = remote.getBreakBulkReportInfo(consolSearchModel,helper.eSupplyGlobalParameters.getLocationId(),shipmentMode);
             
            System.out.println(consolSearchModel.totalRecords);

             if(totalList != null && totalList.size() > 0)
                  consolSearchModel=      (ConsolSearchModel)totalList.get(1);
                 
               if(consolSearchModel.totalRecords > 0)
                    {
                      ArrayList      ICStatusList  =(ArrayList)totalList.get(0);
                      consolSearchModel=      (ConsolSearchModel)totalList.get(1);
                      request.setAttribute("ICStatusList",ICStatusList);
                    }
                 helper.session.setAttribute("consolSearchModel",consolSearchModel);
             
                  doDispatcher(helper.request, helper.response, "etrans/air/ETACorrectionStatusReport.jsp");
******************************************************


 i am passing consolSearchModel to the remote method;java is pass-by value (i thought after remote call i will get consolSearchModel.totalRecords=15)
in the remote method i put sop at the bottom of getBreakBulkReportInfo method i got 15 records after that i put sop after remote call in the controller its displaying consolSearchModel.totalRecords=0;


      Is this neccasary to write below condition to fetch the consolSearchModel(if i write like this i am getting 15 records;working fine)
       if(totalList != null && totalList.size() > 0)
                  consolSearchModel=      (ConsolSearchModel)totalList.get(1);

             

             Is this the correct way of what i am doing???Is there any easy solution other than this?



      public ArrayList getBreakBulkReportInfo(ConsolSearchModel consolSearchModel,String station,String shipmentMode)
      {
            PreparedStatement                                          pStmt                                    = null;
            PreparedStatement                                          pStmt1                                    = null;
            Connection                                                      connection                              = null;
            ResultSet                                                      rs                                          = null;
            ResultSet                                                      rs1                                          = null;
            FinancialCorrectionsDOB                                    model                                    = null;
            FinancialCorrectionsDOB                                    model1                                    = null;
            ArrayList                                                      modellist                              = null;
            ArrayList                                                      modellist1                              = null;
            ArrayList                                                      submodel                              = null;
            String                                                            temp                                    = "";
            String                                                            query                                    = "";
                boolean dataExists            = false;
            int            recordsRead            = 0;
            int            recordStart            = 0;
            int            recordEnd            = 0;
   
                ArrayList                        returnList            =      new      ArrayList();
            
   
             if(consolSearchModel==null)
                consolSearchModel = new ConsolSearchModel();

      

            try
            {
                  if(shipmentMode.equals("Air"))
                  {
                        temp="1";
                        query                  ="SELECT DISTINCT STA.ID,STA.INVOICE_NBR,STA.INVOICE_NBR,FRT.HOUSEDOCID , "
                                                +"(SELECT STATUS FROM FS_FR_FRTINVOICEMASTER WHERE FRTINVOICEID=STA.INVOICE_NBR ) "
                                                +"STATUS,STA.ORG_STN,CORR.ORG_APPROVAL_STATUS,CORR.DEST_APPROVAL_STATUS,"
                                                +"(SELECT MST.BREAKBULK_STATUS FROM FS_FR_HOUSEDOCHDR H, FS_FR_MAWB_CONSOLE_HDR CH, "
                                                +"FS_FR_MASTERDOCHDR MST WHERE H.CONSOLE_SEQ_NO=CH.CONSOLENO "
                                                +"AND CH.MAWB_SEQ_NO=MST.MAWB_SEQUENCE_NO AND H.HAWB_NO=FRT.HOUSEDOCID)"
                                                +"BB_STATUS, STA.SUPPLEMENTARY_YN FROM TLN_IC_INVCE_STATUS STA, FS_FR_FINANCIAL_CORRECTION CORR, FS_FR_FRTINVOICEMASTER FRT "
                                                +"WHERE STATUS_OC='O' "
                                                +"AND  CORR.REF_ID=STA.INVOICE_NBR "
                                                +"AND FRT.FRTINVOICEID=STA.INVOICE_NBR "
                                                +"AND STA.DEST_STN= ? "
                                                +"AND FRT.SHIPMENTMODE=? "
                                                +"AND CORR.STATUS<>'D'";
                  }
                  

                  connection            =  dataSource.getConnection();      
   
                  pStmt                  =  connection.prepareStatement(query,ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
                  
                  pStmt.setString(1,station);
                  pStmt.setString(2,temp);
                  
                  
                  rs = pStmt.executeQuery();

            
                  recordStart      = (consolSearchModel.pageNumber-1)*consolSearchModel.displaySize;

                  if(recordStart > 0)
                        rs.absolute(recordStart);
                  else
                        rs.beforeFirst();
     
                  modellist            =  new ArrayList();
                  while(rs.next() && recordsRead < consolSearchModel.displaySize)
                  {
                  
                              model = new FinancialCorrectionsDOB();
                              model.setId(rs.getLong(1));
                              model.setRefId(rs.getString(2));
                              model.setOriginalInvoiceId(rs.getString(3));
                              model.setHouseId(rs.getString(4));
                              model.setStatus(rs.getString(5));
                              model.setOriginStationId(rs.getString(6));
                              model.setOriginApprovalStatus(rs.getString(7));
                              model.setDestinationApprovalStatus(rs.getString(8));
                              if(rs.getString(7).equals("A") && rs.getString(8).equals("A"))
                              {
                                    model.setCorrectionType("Y");
                              }
                              else
                              {
                                    model.setCorrectionType("N");
                              }

                              model.setBreakbulkStatus(rs.getString(9));
                              model.setSupplementaryFlag(rs.getString(10));
                              model.setFreightInvFlag(freightTermChange(model.getRefId(), shipmentMode));
                                recordsRead++;      
                              modellist.add(model);
                                          
                  }
                      rs.last();
                  consolSearchModel.totalRecords= rs.getRow();
                  rs.close();      
            System.out.println(consolSearchModel.totalRecords);
            }
            catch(Exception ex)
            {
            //      System.out.println("Exception...."+ex.toString());
                  if(logger.isErrorEnabled())
                        logger.error(FILE_NAME+"getBreakBulkReportInfo()-> ",ex);
                  throw new EJBException(ex);
            }
            finally
            {
                  ConnectionUtil.closeStatement(pStmt);
                  ConnectionUtil.closeConnection(connection,pStmt1);
            }
   
                  returnList.add(modellist);
                 returnList.add(consolSearchModel);
                return returnList;
      }

0
Comment
Question by:chaitu chaitu
  • 3
  • 2
5 Comments
 
LVL 20

Author Comment

by:chaitu chaitu
ID: 13843670
other than adding  to arraylist  in the remote method?is there any other solution??.

   returnList.add(modellist);
     returnList.add(consolSearchModel);
    return returnList;

    again i am doing in controller is some what clumsy to me??

    f(totalList != null && totalList.size() > 0)
                  consolSearchModel=      (ConsolSearchModel)totalList.get(1);
                 
               if(consolSearchModel.totalRecords > 0)
                    {
                      ArrayList      ICStatusList  =(ArrayList)totalList.get(0);
                      consolSearchModel=      (ConsolSearchModel)totalList.get(1);
                      request.setAttribute("ICStatusList",ICStatusList);
                    }
0
 
LVL 19

Accepted Solution

by:
Jim Cakalic earned 1000 total points
ID: 13844992
Hi,

I think your confusion may arise from the understanding of pass-by-value semantics in a remote environment.

First let's review the method calling conventions of a local application running within a single JVM. Remember that Java _always_ passes parameters to methods by value. No exception. What happens under the covers in a local Java method call is that the parameter values corresponding to the formal arguments of the method are pushed on the stack. The method uses the stack values so any changes to the parameter values will not be visible to the client code calling the method. What seems to be an exception to this rule is mutable objects that are passed as parameters. Changes made to the object contents are visible when the method returns. But it really is still pass-by-value because what happens is that the JVM makes a copy of the object reference (not a copy of the object) on the stack. It is the object reference that is passed by value. If you attempt to change the object reference within the method, that reference change will not be visible to client code when the method completes. For example, you can't allocate and return a collection from a method like this:

    public void allocateList(List newList) {
        newList = new ArrayList();
    }

    public void getList() {
        List ref = null;
        allocateList(ref);
        System.out.println("List ref: " + ref);
    }

When this code executes, the SOP will print "List ref: null". That's because the change to the reference by the allocateList method only changes the copy of the object reference on the stack which disappears when the method stack frame is popped.

Remote calling conventions for pass-by-value are similar but even more restrictive. They are similar in that copies of the parameters to the formal arguments are made when the method is called. In the case of a remote call parameter values are serialized before being passed/sent to the remote method. In this case, the actual object really is copied. In the remote method, any changes to parameter values are not visible when the method completes -- just like in a local method call. The more restrictive part is that any changes to object parameter contents by the remote method are also not visible. The remote JVM does not serialize the parameters back to the calling JVM when the method returns. So the client method cannot rely on the remote method call to return results by changing method parameters. The remote method will be changing a full out copy of the object. The object changes will not be returned to the client.

What that means is that _all_ the client visible results of a remote method call must be passed back in the return value of the method. Upon reviewing your code, it looks like you're doing that. The totalList returned by the method contains two objects -- a list containing the data objects populated from the query and a consoleSearchModel object.

Here is where your confusion might ultimately lie:

    ArrayList totalList =
        remote.getBreakBulkReportInfo(
            consolSearchModel,
            helper.eSupplyGlobalParameters.getLocationId(),
            shipmentMode);
             
    System.out.println(consolSearchModel.totalRecords);

    if(totalList != null && totalList.size() > 0)
        consolSearchModel = (ConsolSearchModel)totalList.get(1);
                 
The placement of the System.out.println would appear to assume that consolSearchModel can be modified by the remote method. That isn't the case. Whatever the value of consoleSearchModel, both object reference and object contents, in the client JVM prior to the method call will be the value after the method call. It isn't until you later get the ConsolSearchModel object from the totalList and assign it to the consolSearchModel reference holder that you are now seeing the results of the method call in the client JVM. Reorganizing your code as follows should make this manifest (and I think you said you did this and it works as expected):

    ArrayList totalList =
        remote.getBreakBulkReportInfo(
            consolSearchModel,
            helper.eSupplyGlobalParameters.getLocationId(),
            shipmentMode);
             
    if(totalList != null && totalList.size() > 0)
        consolSearchModel = (ConsolSearchModel)totalList.get(1);

    System.out.println(consolSearchModel.totalRecords);

Reading the code fragments you posted, it seems to me that consolSearchModel is unnecessary as a parameter to the getBreakBulkReportInfo method. As for alternate designs, returning a List works OK but isn't IMO a very good API design. I would have designed the method to return a single specific object meant to encapsulate all the results in a more meaningful way. For example, maybe a SearchResults object that had a ConsolSearchModel and a List. The method would instantiate and populate a SearchResults object and return that to the client. Then the client would do something like:

    XXX locationId = helper.eSupplyGlobalParameters.getLocationId();
    SearchResults searchResults = remote.getBreakBulkReportInfo(locationId, shipmentMode);
    ConsolSearchModel consolSearchModel = searchResults.getConsolSearchModel();
    List icStatus = searchResults.getResults();
    ...

Regards,
Jim Cakalic
0
 
LVL 20

Author Comment

by:chaitu chaitu
ID: 13848477
hi jim

u explained very well ;

"it seems to me that consolSearchModel is unnecessary as a parameter to the getBreakBulkReportInfo method."

in our case it is neccassary to pass consolSearchModel  to remote method.

before sending it to remote method i will set some varaibles to consolsearchmodel;

         ConsolSearchModel consolSearchModel                   = new ConsolSearchModel();
      consolSearchModel.displaySize      = NumericUtil.parseString(request.getParameter("displaySize"), 10);
      consolSearchModel.orderByFeild      = NumericUtil.parseString(request.getParameter("orderByFeild"), 1);
      consolSearchModel.pageNumber      = NumericUtil.parseString(request.getParameter("pageNumber"), 1);
      consolSearchModel.isDescending      = (request.getParameter("isDescending") != null) ? true : false;
      
  ArrayList totalList               = remote.getBreakBulkReportInfo(consolSearchModel,helper.eSupplyGlobalParameters.getLocationId(),shipmentMode);

per page i am displaying 10 records ;when user clicks 2nd page again i will execute the query once again;for that i need to pass consolSearchModel object in the remote call;
0
 
LVL 19

Expert Comment

by:Jim Cakalic
ID: 13850267
OK. Did the rest of my response answer your question?
0
 
LVL 20

Author Comment

by:chaitu chaitu
ID: 13852112
yes thank u very much
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
Suggested Courses
Course of the Month13 days, 13 hours left to enroll

580 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question