permaculture playing cards*
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes URLyBird 1.2.2: findByCriteria() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Bookmark "URLyBird 1.2.2: findByCriteria()" Watch "URLyBird 1.2.2: findByCriteria()" New topic
Author

URLyBird 1.2.2: findByCriteria()

Rajesh Moorthy
Ranch Hand

Joined: Sep 23, 2008
Posts: 30
Hello folks,

The findByCriteria() method in the data class returns an array of record numbers. The GUI receives these record numbers and searches the database (or cache) again for the actual records to be displayed.

Before the GUI reads the database, if another client changes the record, how will we make sure of the data integrity?

Thanks & regards,
Rajesh.
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 5126
    
  12

you have several possibilities:
- on the gui side, you have to check for exact matches, so if a record got changed you will filter it out by checking for an exact match
- make an atomic operation of the find and multiple reads
- create an extra method in your own interface (that extends sun's interface) that will return the records instead of the record numbers

So choice is up to you

Good luck!
Kind regards,
Roel


SCJA, SCJP (1.4 | 5.0 | 6.0), SCJD
http://www.javaroe.be/
Rajesh Moorthy
Ranch Hand

Joined: Sep 23, 2008
Posts: 30
Thanks for your reply, Roel.

I am thinking of implementing the 3rd choice.

create an extra method in your own interface (that extends sun's interface) that will return the records instead of the record numbers


However, by the time the GUI displays the records from above, if the actual data file changes due to some other client activity, how do we handle it?

I did not understand the second choice, though. Could you please explain?
make an atomic operation of the find and multiple reads


Thanks again,
Rajesh.
Alain Dickson
Ranch Hand

Joined: Dec 08, 2008
Posts: 53
Second choice:
make an atomic operation of the find and multiple reads


By this Roel meant, don't send the record numbers back to the client from the server.
When server received request for a search do two things on server
1. Find the record numbers (Using the find method) which match the search criteria
2. Staying on the server put records corresponding to the record numbers found at step 1 into a string array
THEN return the array to client instead of first sending record numbers to client and then again asking for records corresponding to those recNumbers.

This will be considered as good design and will also decrease the network trafic.
This is how I did it.

Roel: correct me if you meant something different.
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 5126
    
  12

Alan,

What you described is option 3. What i meant with option 2, do the following at the client:


So it's the same as 3, but 3 happens on the server and 2 happens on the client (with of course more network traffic). But that could be the solution if you want to stick to sun's interface. And performance is not a major requirement. Myself also added an extra method to my own interface (extending sun's).
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 5126
    
  12

Rajesh,

If another client changes something after a search is executed: there's nothing much you can do about it. That's a consequence of a client/server application. You can add some pushing algorithm on your server: each time something is changed, server pushes it through to all connected clients. but i think such an approach is a bridge (maybe even 5) too far for this assignment

I didn't do anything, but of course if a user wants to book a record that is deleted after the search was executed, then he will get an error message and i'll give the advice to reload his data (by executing the search again).

Regards,
Roel
Rajesh Moorthy
Ranch Hand

Joined: Sep 23, 2008
Posts: 30
Hello Dudes,

Thanks for your suggestions !!!

Regards,
Rajesh.
Johnny Sizer
Greenhorn

Joined: Jan 08, 2008
Posts: 5
I also have UrlyBird 1.2.2 and am currently working on the findByCriteria() method. I've read various post regarding extending the provided Sun interface which in my case is DBAccess. I don't quite understand how this would work. Could someone please break this down for me.

Would we not still need to provide an implementation for this method as is to honor its expected implementation. (i.e returning the recNo array)

Thanks,

Johnny


SCJP 1.4,<br />SCJD progress...
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 5126
    
  12

Hi Johnny,

It's fairly easy.

In your DBAccess you have


Then you have JohnnyDBAccess, something like


And in your implementation you will have an implementation for both methods, and find2 will look like this:
- create map
- get matching record numbers by calling find-method
- for each record number read record by calling read-method and add recNo + record to map
- return map

Kind regards,
Roel
Rajesh Moorthy
Ranch Hand

Joined: Sep 23, 2008
Posts: 30
When server received request for a search do two things on server
1. Find the record numbers (Using the find method) which match the search criteria
2. Staying on the server put records corresponding to the record numbers found at step 1 into a string array
THEN return the array to client instead of first sending record numbers to client and then again asking for records corresponding to those recNumbers.


If I use the method signature from Sun's interface, I should use the above algorithm.

Here's my thinking:
1) I will implement the functionality of the findByCriteria() method as expected by Sun.
2) However, I will not use this method.
3) I will write a separate method that is similar to the implementation of findByCriteria() method, but returns the matching records directly (not the record numbers).
4) This way, I can avoid reading the database for a second time, inorder to get the records matching the record numbers.

Is this an accepted approach?

Thanks,
Rajesh.



Johnny Sizer
Greenhorn

Joined: Jan 08, 2008
Posts: 5
Thanks. I will likely extend the supplied interface also, this appears to be the "cleanest" way to implement the search functionality.

Roel have you completed this exam yet.

Johnny
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 5126
    
  12

Johnny,

No. But i'm in the last straight line for the finish. I'll hope to end the coding part this weekend. And then i give me 1 week to complete the choices.txt, userguide,... wrapping all things up and submit.

But if you doubt about the approach and passing the exam, many others have passed the exam with creating an extra/own interface (which extends sun's). Someone who did and who is still really active on this forum, is Roberto Perillo.

Kind regards
Roel
Rajesh Moorthy
Ranch Hand

Joined: Sep 23, 2008
Posts: 30
Hi Johnny...Do you plan to use the method from Sun's interface?
Hi Roel...Do you think it is necessary to use the method from Sun's interface?
When server received request for a search do two things on server
1. Find the record numbers (Using the find method) which match the search criteria
2. Staying on the server put records corresponding to the record numbers found at step 1 into a string array
THEN return the array to client instead of first sending record numbers to client and then again asking for records corresponding to those recNumbers.


If I use the method signature from Sun's interface, I should use the above algorithm.

Here's my thinking:
1) I will implement the functionality of the findByCriteria() method as expected by Sun.
2) However, I will not use this method.
3) I will write a separate method that is similar to the implementation of findByCriteria() method, but returns the matching records directly (not the record numbers).
4) This way, I can avoid reading the database for a second time, inorder to get the records matching the record numbers.

Is this an accepted approach?
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 5126
    
  12

Rajesh,

Do you mean in your program? I implemented the find (the method from sun's interface) in my Data-class and i implemented find2 (the method from my own interface). find2 uses a call to find because it uses the same logic to find the record numbers.

In the program i use a call to find2 (and find is never called).

Regards,
Roel
Johnny Sizer
Greenhorn

Joined: Jan 08, 2008
Posts: 5
Like Roel, I am in the process of implementing the find method as it is supplied in Sun's interface. So I will extend Sun's interface and implement my own find method which will be the used by the program. Also it probably does make sense to leverage the functionality in the original find method(as it will be largely the same) so I will likely include a call to it in my version.

Johnny
sohrab khan
Greenhorn

Joined: Sep 10, 2007
Posts: 13
No need to define another method. Synchronize the method or only the two operations below:
Search for the records with a certain criteria and return the record numbers array,
Read each record by supplying the record no.



Hope this helps.
Sohrab Khan
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 5126
    
  12

Sohrab,

That's indeed another possibility, like i said in the 2nd post of this thread.

Regards,
Roel
Rajesh Moorthy
Ranch Hand

Joined: Sep 23, 2008
Posts: 30
Rajesh,

Do you mean in your program? I implemented the find (the method from sun's interface) in my Data-class and i implemented find2 (the method from my own interface). find2 uses a call to find because it uses the same logic to find the record numbers.

In the program i use a call to find2 (and find is never called).

Regards,
Roel


Sorry for the late reply, as I could not work on the topic for a few days.

As Roel said, let us assume:
find -> the method from sun's interface
find2 -> the method from my own interface

"find" returns the record numbers. As I do not want the record numbers, I write "find2" to directly return the records.

Therefore, "find" will not be called from anywhere, not even from "find2". In other words, the method from sun's interface will be implemented, but will never be called from anywhere in the application.

Is this an allowed approach?

Thanks & regards,
Rajesh.
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 5126
    
  12

Hi Rajesh,

That's the approach I followed. Can't tell if this is accepted/allowed, because i don't have submitted yet. but i don't see any reason why this should not be allowed. Because you are implement sun's interface (and that's a must), so if they testing your find-method, it will pass the test because you have implemented it

Kind regards,
Roel
Rajesh Moorthy
Ranch Hand

Joined: Sep 23, 2008
Posts: 30
Thanks Roel, for your quick reply.

I will try the approach, any way !

Regards,
Rajesh.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: URLyBird 1.2.2: findByCriteria()
 
Similar Threads
URLyBird 1.2.2: findByCriteria()
Another Question about findByCriteria
DBAccess interface question
GUI search by findByCriteria and readRecord: synchronize?
B&S locking issues & findByCriteria