• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Methods in RMI and Multithreading

 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Ranch Hand
Posts: 623
1
IntelliJ IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!
 
Eduard Mamedov
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 623
1
IntelliJ IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 :-)
 
Bartender
Posts: 2292
3
Eclipse IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!
 
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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).
 
Roberto Perillo
Bartender
Posts: 2292
3
Eclipse IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I opted for the same approach as you: singleton Data class with all methods marked synchronized
 
Eduard Mamedov
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok, thank you all friends!
 
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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)
 
Ranch Hand
Posts: 590
Eclipse IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic