aspose file tools*
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes urlybird 1.3.3 lock/unlock 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 "urlybird 1.3.3 lock/unlock" Watch "urlybird 1.3.3 lock/unlock" New topic
Author

urlybird 1.3.3 lock/unlock

andrew gutnik
Greenhorn

Joined: Nov 19, 2013
Posts: 5

Hi all ! after long time spent reading the forum i finally implemented locking mechanism that seems to work after few tests i did. But i have no clue why it works only when i use
such a map for locked records :



DBLocker is my locker manager , Integer is recNo and i populate map like this



the Map<DBLocker, Integer> lockTable; is not working , any help to explain why case with Data.DBLocker wrapped in WeakReference works ?




Pawel Pawlowicz
Ranch Hand

Joined: Apr 18, 2013
Posts: 596
    
  11

What do you mean by "not working"?
Roberto Perillo
Bartender

Joined: Dec 28, 2007
Posts: 2247

Howdy, Andrew! Welcome to CodeRanch!

Champ, please take a look at our FAQ and try to run the tests that we made available there. There is a test that specifically tests the locking mechanism.

Other than that, I'd say that it looks like your locking mechanism is a little more complex than it should be. For instance, how about keeping the locked records in a Map<Long, Long> (where the key is the record
number and the value is a number that identifies the client who locked the record)? The value can be the current Thread's ID, for instance.

And also, I don't know if you already took a look here, but if not, please do. I think it might be helpful!


Cheers, Bob "John Lennon" Perillo
SCJP, SCWCD, SCJD, SCBCD - Daileon: A Tool for Enabling Domain Annotations
andrew gutnik
Greenhorn

Joined: Nov 19, 2013
Posts: 5

Hi ! Thank you for replies glad to be in such friendly company.

Yes i did tests and read "Demystifying the OCMJD" , sure i'll not bet i understood it 100% . Test results are fine .

About
For instance, how about keeping the locked records in a Map<Long, Long> (where the key is the record
number and the value is a number that identifies the client who locked the record)?
. Yes you are right i just trying different ways to implement the assignment.

About
What do you mean by "not working"?
.

My case is as follows : i'm running about from 1 to 100 separate threads

that creates network clients and each client call the book method that is sequence of lock - update - unlock (i have no cookie to identify clients).

Now about "not working" - it means that call to unlock results in exception "Can not unlock record.Possibly record was not locked." ( line 12 )

part of unlock method



with code i posted it works great but if i change to

it throws exception.

by the way do you have difficulties to understand my english , i need to understand if i can express myself more or less correctly?

thank you in advance

Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 4913
    
  10

andrew gutnik wrote:Yes you are right i just trying different ways to implement the assignment.

What happened with the KISS principle? To be honest I have been around this forum since 2009 and it's the very first time that I see someone using WeakReference and ReferenceQueue. I don't really know if that's a good thing. If I use the search function on this forum I notice there are a few threads mentioning these classes.

Back to your problem. If I understand correctly when you make the code change (not using the WeakReference anymore in your lockTable) an exception is thrown to indicate the locked record can't be unlocked. So how does your implementation of the isRecordLocked look like, because that method is returning the unexpected value (you expect true, but instead it's false)?

As a side note: in my opinion you can't throw a RecordNotFoundException if the record was not locked prior to calling update and/or unlock. Simply because that's not a RecordNotFoundException (this exception should only be used if the record is not found, e.g. invalid record number or deleted record). When a record is not locked when you call unlock, that's wrong API usage (because you have to call lock-method prior to update/unlock), so a runtime exception (like IllegalStateException or IllegalOperationException) would be more appropriate.

andrew gutnik wrote:by the way do you have difficulties to understand my english , i need to understand if i can express myself more or less correctly?

In these forums the native language of most people is different from English (like me). So no need to worry: we do understand you. But to get an appropriate answer you must provide as much details as possible. For example in your 1st post you write "not working", that's just too general (could be everything: compiler error, runtime exception,...) and then you might not get the answer you are looking for. Your second post is much better! But be careful when you post actual code from your assignment. Try to limit the amount of code you post or instead of actual code just post some pseudo-code. We don't allow posting complete code snippets (they will be removed). Otherwise someone else can just copy/paste the complete assignment (and that can't be the purpose of this certification).

Good luck!


SCJA, SCJP (1.4 | 5.0 | 6.0), SCJD
http://www.javaroe.be/
andrew gutnik
Greenhorn

Joined: Nov 19, 2013
Posts: 5

What happened with the KISS principle? To be honest I have been around this forum since 2009 and it's the very first time that I see someone using WeakReference and ReferenceQueue. I don't really know if that's a good thing. If I use the search function on this forum I notice there are a few threads mentioning these classes.


1. I use mentioned above goodies to track disconnected clients in case to avoid deadlock, but since i am implementing thin client and after reading the forum i decided that check for lost connection is not must .

And it's the reason for my question in the first place because after i change my code back to use just a HashMap with key as Integer and value as Integer the same tests fail. Tests are for network clients(each client has reference to static Data instance). Clients are created by factory as proxies to RMI server.

2. Exception handling in my code is still under construction and i agree with you it's not appropriate.

3.
isRecordLocked is something simple like


and you are right i'm expecting that it will return true becase i did call to lock() method before calling unlock()

also tests from The FAQ done fine , but they tests only instances of Data class.
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 4913
    
  10

Can I ask how much experience you have as a java programmer/developer? Because based on the method implementation you provided, I'm a bit confused. Why do you use Iterator and entrySet, if you have a containsKey method to check if a given key is in the map

Back to the problem. If you expect this method needs to return true and it does not, then you have to start debugging your code/application. That's not something we can do for you. Did you already inspect the content of the map you are iterating through in the isRecordLocked method? Was the record number you are trying to unlock in this map? I would guess it is not (otherwise you would got true from this method). Maybe the lock-implementation has a bug (e.g. map.put(uniqueClientIdentifier, recNo); instead of map.put(recNo, uniqueClientIdentifier);
andrew gutnik
Greenhorn

Joined: Nov 19, 2013
Posts: 5

thank you all , very helpfull.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: urlybird 1.3.3 lock/unlock
 
Similar Threads
What exactly is enqueued in a ReferenceQueue
garbage collection
I got a Type-Safe Warning
synchronized revisted...
NX: URLy Bird 1.3.1 Explicit Fatal Exception Handling