File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes Methods in RMI and Multithreading
Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Bookmark "Methods in RMI and Multithreading
" Watch "Methods in RMI and Multithreading
" New topic
Author

Methods in RMI and Multithreading

Eduard Mamedov
Greenhorn

Joined: May 20, 2010
Posts: 23
Hi, finally complete my Data class. Now working on business service.
My Data class is singleton with all public methods marked as synchronized.

In my book method i have following code:



Am I correct, that local variables saves in the method stack...and I don't need synchronize this method, as all my method in Data class already synchronized?


SCJP 6
Piotr Nowicki
Ranch Hand

Joined: Jul 13, 2010
Posts: 610

Hi Eduard!

The code you posted should be fine.

Well, you don't have to be worried about Data class method invocations, but if you don't plan to create new Business layer instance for every client (like in RMI Factory), but rather reuse the same object (just simply bind it to the RMI registry), you should pay some extra attention to the non-data operations.

From my personal experience - watch out for using some Utility class from the business logic. I've had one, which had only public static methods (it is a Utility class after all) and was used to transform String[] to Room. One of the static variables was SimpleDateFormat reused to translate string -> date and other way around.

Multiple clients were executing business logic layer operations at the same time and used Utility class and this SimpleDateFormat object which isn't thread safe at all! It end in throwing (quite randomly) some perfectly legal date parsing errors.

It wasn't easy to spot, so treat it just as a friendly warning :-)

Good luck!


OCP Java SE 6 Programmer, OCM Java SE 6 Developer, OCE Java EE 6 JSPSD, OCE Java EE 6 EJBD, OCE Java EE 6 JPAD, Spring 3.0 Core Professional.
Eduard Mamedov
Greenhorn

Joined: May 20, 2010
Posts: 23
Thanks, buddy !
No I will not use RMI Factory, only one object registered through RMI registry.
static I have already the same problem, that you described, in my working experience

I also release strict search method in this layer:



It's not synchronized, also reason to synchronize method, that if someone add record with match criteria while we looping...but I think it's over complicated, as we haven't create operation in business logic?
Piotr Nowicki
Ranch Hand

Joined: Jul 13, 2010
Posts: 610

I've got a similar code to yours :-)

I've pointed in choices.txt that it is possible for someone to add/delete/update/etc. database file between find() and read() methods invocations.

I justified it by saying that the business logic layer search mechanism will filter out such cases (I am using separate recordMatches() function in BLL). This will not ensure you that at the moment you get the results that was the actual state of the database (i.e. some records might be added, and right now more records matches your criteria).

To be prepared for such situations, an atomic findAndRead() method should be introduced to the DB interface. At this moment, I justified that in my opinion it's behind the scope of the assignment to cope with it :-)
Roberto Perillo
Bartender

Joined: Dec 28, 2007
Posts: 2266
    
    3

Howdy, Eduard!

It's not synchronized, also reason to synchronize method, that if someone add record with match criteria while we looping...but I think it's over complicated, as we haven't create operation in business logic?


Well, my search method in my business layer class is not synchronized as well. In order to perform an atomic operation, you would have to synchronize on the Data object, not synchronize the search method. I didn't also synchronize on the Data object. You can say that you're not providing any addRoom()/removeRoom() methods in your business layer, so that's why what you said can never happen, and thus synchronizing on the Data object is needless.

Good luck, champions!


Cheers, Bob "John Lennon" Perillo
SCJP, SCWCD, SCJD, SCBCD - Daileon: A Tool for Enabling Domain Annotations
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 5389
    
  13

Pedro Kowalski wrote:To be prepared for such situations, an atomic findAndRead() method should be introduced to the DB interface. At this moment, I justified that in my opinion it's behind the scope of the assignment to cope with it :-)

I added such method to my own interface (extending the required-to-implement one).


SCJA, SCJP (1.4 | 5.0 | 6.0), SCJD
http://www.javaroe.be/
Roberto Perillo
Bartender

Joined: Dec 28, 2007
Posts: 2266
    
    3

Roel De Nijs wrote:I added such method to my own interface (extending the required-to-implement one).


Oh! I forgot to say that you can also go for this approach!
Eduard Mamedov
Greenhorn

Joined: May 20, 2010
Posts: 23


reasonable.... but what about Business layer, strictSearch() must be synchronized on data in that way, as in future someone can add method that will create/delete record... Roel, do you synchronize your strictSearch() method, or only add findAndRead() to data class....?
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 5389
    
  13

I opted for the same approach as you: singleton Data class with all methods marked synchronized
Eduard Mamedov
Greenhorn

Joined: May 20, 2010
Posts: 23
I know that I mean that if we not synchronize for loop in that method (from BLL),


threre is chance, that someone add/delete record while we on looping....for data layer findAndRead() is good, but for business layer method strictSearch still can return "non-fresh" result.




I think to do so.....or simply omit this case
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 5389
    
  13

In your business layer you still have to do some filtering (because you only want the exact matches), so if a record is updated/deleted between the find and the read, it will get filtered out. If it is updated after the read, a non-exact match is returned to the client.
That's why I decided to add the extra method to my interface But I guess it's a minor issue. You can opt for synchronizing on Data ("data correctness") or opt not to dit ("performance").
Eduard Mamedov
Greenhorn

Joined: May 20, 2010
Posts: 23
Ok, thank you all friends!
Georgiana Lungu
Ranch Hand

Joined: May 17, 2010
Posts: 34
I did not synchronize this method at all, i am just swallowing the RecordNotFound exception that can occur, explaining that it is cause by some deletition between find and read, and i am not adding the record to the map. I am not sure if this is quite ok.
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 5389
    
  13

Swallowing an exception is not a good idea, but in this situation it is ok (don't forget to document it in the code and decisions document)
Sean Keane
Ranch Hand

Joined: Nov 03, 2010
Posts: 581

Roel De Nijs wrote:
Pedro Kowalski wrote:To be prepared for such situations, an atomic findAndRead() method should be introduced to the DB interface. At this moment, I justified that in my opinion it's behind the scope of the assignment to cope with it :-)

I added such method to my own interface (extending the required-to-implement one).


So you must have extended the Sun interface with quite a few methods ?

- findAndRead() - to find an read records in an atomic operation.
- init() - to load your cache from the file.
- exit() - to write your cache back to the file.


SCJP (1.4 | 5.0), OCJP (6.0), OCMJD
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Methods in RMI and Multithreading