• 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

How to test lockmanager?!!

 
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Guys how can i test the lockmanager?
I lock a record. And then lock the same record again...
In my code i will have to wait :

But when it reaches wait i generates an exception....
!!

java.lang.IllegalMonitorStateException: current thread not owner
at java.lang.Object.wait(Native Method)
at java.lang.Object.wait(Object.java:429)
at suncertify.db.LockManager.lockRecord(LockManager.java:50)
at suncertify.db.Data.lockRecord(Data.java:247)
at suncertify.server.DatabaseRMIServer.closeServer(DatabaseRMIServer.java:106)
at suncertify.server.ServerGUIController.closeServer(ServerGUIController.java:81)
at suncertify.server.ServerGUIController.actionPerformed(ServerGUIController.java:69)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1786)
at javax.swing.AbstractButton$ForwardActionEvents.actionPerformed(AbstractButton.java:1839)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:420)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:258)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:245)
at java.awt.AWTEventMulticaster.mouseReleased(AWTEventMulticaster.java:231)
at java.awt.Component.processMouseEvent(Component.java:5100)
at java.awt.Component.processEvent(Component.java:4897)
at java.awt.Container.processEvent(Container.java:1569)
at java.awt.Component.dispatchEventImpl(Component.java:3615)
at java.awt.Container.dispatchEventImpl(Container.java:1627)
at java.awt.Component.dispatchEvent(Component.java:3477)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:3483)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:3198)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:3128)
at java.awt.Container.dispatchEventImpl(Container.java:1613)
at java.awt.Window.dispatchEventImpl(Window.java:1606)
at java.awt.Component.dispatchEvent(Component.java:3477)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:456)
at java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchThread.java:201)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:151)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:145)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:137)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:100)


What is wrong with my code?!!


Additional question :
If i try to lock a record and the database is locked, the client waits for ever... I guess that shouldn't be but how can i return from the lockManager and state the database is locked. Else it will wait forever i guess...
And how did you guys test your LockManager?!!
Thanks
[ May 05, 2004: Message edited by: Ricardo Estafan ]
[ May 05, 2004: Message edited by: Ricardo Estafan ]
 
Ranch Hand
Posts: 1066
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ricardo,
I am working on the BS assignment, which doesn't have the entire db locking requirement, so cannot give you an exact answer. Maybe one of the first things that you can check is if the entire database is locked thru a flag and throw a runtime exception before proceeding to attempt to lock the particular record.
wait() and notify() methods can only be called in sychronized blocks.Your wait() method is not in a synchronized block, and hence the exception!
 
Vishwa Kumba
Ranch Hand
Posts: 1066
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Also in your catch block for InterruptedException, it is better to do something (display an error message or throw a runtime exception to indicate to the CSR that an error has occurred) instead of nothing...
Swallowing exceptions is not a good practice.
[ May 05, 2004: Message edited by: Vishwa Kumba ]
 
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 Ricardo,
You need to call wait() on an object that you own the mutex on. In other words, you need to be inside the synchronized block, and if you are explicitly naming the object you are synchronizing on (which you are doing: you are synchronizing on lockedRecords) then you need to explicitly call wait() on that object.
Your exception is because you are calling wait() on an object you do not own the mutex on. Specifically you are calling wait() on the instance of Data that you are running in (effectively you are calling this.wait()).

If i try to lock a record and the database is locked, the client waits for ever...


That sounds right. Your instructions should tell you that the lock method should block until the lock is granted. So returning without the lock being granted would be a mistake.

And how did you guys test your LockManager?!!


Why not tell us some of the ideas you have had about testing this, and we will suggest areas that you might also want to consider testing?
Regards, Andrew
 
Ricardo Estafan
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I wasn't thinking about using JUnit. I use the debug mode to see what happens.
* I call lockRecord two times (with the same record number)
* I call lockRecord two times (with a different record number)
* I call lockRecord and afterwards call lockDatabase
* I call lockDatabase and afterwards call lockRecord
That's about the options i have and in debug mode i can see what happens.
Tips anyone or do you suggest a different approach?
 
Andrew Monkhouse
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 Ricardo,
Just to work through this a bit further...

* I call lockRecord two times (with the same record number)


I would assume that you are checking that the second call blocks.
Are you also checking what happens when the first client releases it's locks?
Then taking that a step further - what if 3 clients try to lock the same record, and then one releases it - do the two waiting clients behave reasonably?

I use the debug mode to see what happens.


You also need to run your tests without debug mode. In debug mode you are controlling when each line of code gets executed. Running external to debug mode means that it is up to the JVM when your lines of code get executed.
Speaking of which - it can be a good idea to set up multiple threads which will all try simultaneously (or as close to it as practical) to get the same lock - this may behave differently than even two threads as the JVM may slice the threads more agressively since they are all attempting to work at the same time. I normally set up 100 threads, get them right to the point where they are about to call the lock() method, then pause them all at that point. Then wake them all simultaneously so that they all hammer the lock method at once.
You should also try combinations of those tests. So where you have:


* I call lockRecord two times (with the same record number)
* I call lockRecord two times (with a different record number)


Try running the same tests at the same time.
Later you will need to test the lock method as it gets called in your book method. So if two (or 100) clients try to book the same record simultaneously, do they get handled correctly.
Regards, Andrew
 
Ricardo Estafan
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Mark,
Is there a convenient way to start multiple threads instead of starting multiple clients?
Thanks!!
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic