• 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
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

how to avoid this? thanks!

 
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm going to use multiable Data object in server part, each thread has their own Data object.
I'm wondering under this situation if there are two client need to modify different record in database at the same time what will happend?
Does SUN allow several file handlers to modify the different part of the dbfile at same time?

such as client-no.1 need to update record[1] , client-no.2 need to update record[3]
because they are updating different record , we needn't use locking mechanism to lock record[1] or record[3].
becasue I ust mutilable data object to implement my project , each client has their own Data object and file handler.
And because each thread has their own Data object the "synchronize" will not work for different Data object ,even I synchronize all write() , update() , modify() method.
That means , different client could modify data file at the same time and It can't be avoid.
is it allowed, or is it safe. if not, is there a good way to avoid this happened.
I can only find one way to avoid this situation, declare a static file handler object in Data class, so all different data object will share same file handler and I can synchronize this file handler object in write() update() method .
I don't know is it a good way or not, anyone has a better idea about it.

thank you very much.
 
Ranch Hand
Posts: 247
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why do you need to have multiple Data objects??

If you do have multiple how can you lock a record to prevent one Thread updating while another is Deleting?
 
author and jackaroo
Posts: 12200
280
Mac IntelliJ IDE Firefox Browser Oracle C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Mark,

Originally posted by mark ken:
I'm going to use multiable Data object in server part, each thread has their own Data object.

I'm wondering under this situation if there are two client need to modify different record in database at the same time what will happend?

Does SUN allow several file handlers to modify the different part of the dbfile at same time?



From memory there are no guarantees as to what will happen here. Current Sun implementations of the JVM appear to maintain their own file descriptors (with associated file position pointers), which will allow you to get away with doing this. However this is all system dependant (indeed if you look at the source code for RandomAccessFile you will find that the methods are implemented native to the OS), so it is possible that changing OS could break your expected behaviour.

Originally posted by mark ken:
because they are updating different record , we needn't use locking mechanism to lock record[1] or record[3].



You still need the locking mechanism to ensure that two different clients do not attempt to update record 3 simultaneously. That is:
  • Client 1 checks that record 3 is available for booking - OK
  • Client 2 checks that record 3 is available for booking - OK
  • Client 1 updates record 3 with the CSR number - OK
  • Client 2 updates record 3 with the CSR number -

  • Originally posted by mark ken:
    becasue I ust mutilable data object to implement my project , each client has their own Data object and file handler.
    And because each thread has their own Data object the "synchronize" will not work for different Data object ,even I synchronize all write() , update() , modify() method.



    You can still synchronize blocks of code if you wish - all you need is a common object that all your instances of the Data class can access. The Data.class itself comes to mind .

    Originally posted by mark ken:
    That means , different client could modify data file at the same time and It can't be avoid.
    is it allowed, or is it safe. if not, is there a good way to avoid this happened.



    It is allowable - I know of others who have passed their assignments doing this. Whether it is safe or not is up to how you code it .

    Originally posted by mark ken:
    I can only find one way to avoid this situation, declare a static file handler object in Data class, so all different data object will share same file handler and I can synchronize this file handler object in write() update() method .



    That is what I did - it saves some of the OS dependant issues I mentioned earlier. But as mentioned earlier, there are other static objects you can use for your synchronized blocks.

    Regards, Andrew
     
    mark ken
    Ranch Hand
    Posts: 47
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    thank you so much Andrew for the detailed explanation.
     
    mark ken
    Ranch Hand
    Posts: 47
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Hello Kris,
    because I think multiple Data object will be more efficient to handle the client request, the server is build by multiple data object should be faster than which build by single data object.(it's just my opinion)

    For knowing how to lock record to prevent the problem you mentioned.
    I suggest you read the book <The Sun Certified Java Developer Exam with J2SE 1.4> it will explain it very well.


    Originally posted by Kris Reid:
    Why do you need to have multiple Data objects??

    If you do have multiple how can you lock a record to prevent one Thread updating while another is Deleting?

     
    Ranch Hand
    Posts: 145
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    I used multiton in my data implementation and used static object to generate a common lock object for the multiton objects to synchronize.
     
    Think of how dumb the average person is. Mathematically, half of them are EVEN DUMBER. Smart tiny ad:
    We need your help - Coderanch server fundraiser
    https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
    reply
      Bookmark Topic Watch Topic
    • New Topic