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 Lock Operation 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 "Lock Operation" Watch "Lock Operation" New topic
Author

Lock Operation

John Oconnor
Ranch Hand

Joined: Jan 12, 2011
Posts: 66

Hi for all, I have create all methods who define my db Interface , but i have a question.

Becouse when I lock the record to made a reserv, all operation I have made in Server side.

like this:
Server side
data class, implements db, file is a class to access file

update method(recNo,data,cookie)
+file.lock
+file.save changes
+file.unlock
return to GUI.

So i do not need sent the lockCookie beetween gui and server.So when call update in Data class the cookie to send is 0.

Too if I will made this secuence in my gui, only generate more traffic, (first lock, second save, 3st unlock),
but in my server side i have maintain all consistency, because, the lock, only lock, the update only update and so.
But this operation can take a long time, and expose my application to failure , and force who each client when lock check if any other client dead(by threads) still have locks

I think who this manner is more atomic operation because all secuence happen in server side in a method.
This is the business layer, so is acceptable
With this approuch ,so i have minor risk for a possible lock indefine time

thanks

SCJP 5 - OCMJD 6 - OCPJME1MAD(SCMAD)
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 5273
    
  13

If you don't send a lockCookie to the update-method (or you send always 0) how are you able to guarantee that the record you want to update is locked by the same client which wants to update the record?
As far as I know this is how the required interface should work and if you break these requirements (which you do based on your explanation), you'll fail automatically.

When clientA wants to update record 1, you'll expect to see something like this:

So when clientB tries to update record 1 as well with data.update(1, newData, 10); the update-method should throw a SecurityException. With your approach clientB can easily update (book) the record although it was locked by clientA.


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

Joined: Jan 12, 2011
Posts: 66

Roel De Nijs wrote:If you don't send a lockCookie to the update-method (or you send always 0) how are you able to guarantee that the record you want to update is locked by the same client which wants to update the record?
As far as I know this is how the required interface should work and if you break these requirements (which you do based on your explanation), you'll fail automatically.



Well, I before obtain the remote o local db reference, so I do not has a side where define my bussines logic.
I made, gui-controller-dataaccess-db(interface)-fileaccess, was a thick client.

And now i have create a bussines layer, where I connect mi gui with this service (local or remote) and the service layer is who performs the action atomically, with out generate more trafic and faster.
Now i have gui-controller-service(interface)-db(interface)-fileaccess. obvius, when is stand alone mode , my service work on db local, but in remote, my service obtain a socketClient. Later in my DbSocketResponse are located all logic for example:

because i have work with socket



Obvius, in my service i do not require define lock unlock method, because when call the GUI to service ,
ergo, in my socket server i do not need define lock, unlock, so my socket server is more performatn and more easy to read, too there are minor trafic, because all operations are made in server, by network only is sent a request and obtain the response to update, the service define all logical needed for perform this action, is who lock, update, unlock on db iterface provided by Sun (Oracle )

Now my client side work like a browser internet(i explorer,firefox), this approuch i have used very time, (jeje using jboss, webphere, and so , but now I have create this service thanks Roberto Perillo , viewing your paper about certification),

Now i can say who the response in a thin client is very faster ( i have test in core I3 but when was working with thick client i could see the logs lines pass on screen, and now, in a thin client, i cant view lines pass in console, the response is immediate) so the lock time is very tiny.

In my first approuch with a service layer i have a mix, because i try use the db implements how service layer!!! , but i have separates the behavior and now a have definied clearly limits to gui layer,service layer and persistence layer

I hope is a clear my explain, but I now i have used the approch like Roberto Perillo, like a thin client(but not in RMI, in my case in Sockets )
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 5273
    
  13

Which approach you use (thin or thick/fat client) makes no difference in the calls you need to make. You always have to perform 3 calls to successfully update a record:


With a thin client this code will be in a method in your business service (on the server) and from the client just 1 call is made. With a thick/fat client these calls are performed from the client. That's the only difference.

In the snippet you provided, you pass 0l to the update-method. That's not according to how your locking mechanism should work (but this could be a typo you made).

And finally a small tip: when you use an l to indicate a long (in a constant value), use the upper case L, because it's hard to see the difference between the lower case l and the number 1.
John Oconnor
Ranch Hand

Joined: Jan 12, 2011
Posts: 66

Roel De Nijs wrote:
In the snippet you provided, you pass 0l to the update-method. That's not according to how your locking mechanism should work (but this could be a typo you made).



OHHHH!!! EXCUSEME, I is wrong my snippet, because my browser not refresh well, the snipet correct is the next





Ufff, i can pass who a dumb!!! jajaja, clear , my atomic operations in bussines layer respects the steps to update, delete , and so
I was wrong
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 5273
    
  13

And did you add logic to your business service to prevent double bookings?
John Oconnor
Ranch Hand

Joined: Jan 12, 2011
Posts: 66

Roel De Nijs wrote:And did you add logic to your business service to prevent double bookings?


yes, of course. How i work with cache, before update, first check who for recNo indicate, the customer is empty, if not throws a exception (exception declared in DB) , if I can uptade the record, so the cache is refresh and re-load from databasefile.db

Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 5273
    
  13

John Oconnor wrote:if not throws a exception (exception declared in DB)

That's a business exception, not a database exception. Your update-method just needs to update the appropriate fields, it doesn't care about a customer id which is empty or not.
John Oconnor
Ranch Hand

Joined: Jan 12, 2011
Posts: 66

ok, so i have who only update (save the data) , and before check in my busines layer who the customer is empty.

The exception to thows in this case will be from service.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Lock Operation