It looks like the test will be run in embed mode and not be run using the 
network server.

I have coded up a watchdog that looks like:

class DeadlockWatchdog implements Runnable {

    private boolean stopped = false;
    private long timeout;

    public DeadlockWatchdog(long timeout) {
        this.timeout = timeout;
    }

    public synchronized void start() {
        stopped = false;
        Thread t = new Thread(this, "WATCHDOG");
        t.setDaemon(true);
        t.start();
    }

    public synchronized void stop() {
       stopped = true;
        notifyAll();
    }

    public synchronized void run() {
        final long until = System.currentTimeMillis() + timeout;
        long now;
        while (!stopped && until > (now = System.currentTimeMillis())) {
            try {
                wait(until - now);
            } catch (InterruptedException e) {
            }
        }
        if (!stopped) {
            try {
                boolean res = AccessController.doPrivileged(
                        new PrivilegedExceptionAction<Boolean>() {
                    public Boolean run() throws IOException, 
MalformedObjectNameException, InstanceNotFoundException, MBeanException, 
ReflectionException {
                        return checkForDeadlock();
                    }
                });

                if (res) {
                    System.err.println("Deadlock detected");
                    System.exit(1);
                }
            } catch (Exception x) {
                System.err.println("Watchdog failed: " + x.toString());
                System.exit(1);
           }
        }
    }

    boolean checkForDeadlock() throws MalformedObjectNameException, 
InstanceNotFoundException, MBeanException, ReflectionException, IOException {
        ThreadMXBean bean = ManagementFactory.getThreadMXBean();
        long[] findDeadlockedThreads = bean.findDeadlockedThreads();
        if (null != findDeadlockedThreads && 0 != findDeadlockedThreads.length) 
{
            return true;
        }

        return false;
    }
}

But this is failing with an "java.security.AccessControlException: access 
denied ("java.lang.management.ManagementPermission" "monitor")

Any ideas?   There is probably a security policy that I need to update but have 
no idea where that would be?

From: Bergquist, Brett [mailto:[email protected]]
Sent: Friday, June 24, 2016 4:08 PM
To: [email protected]
Subject: How should one handle a test case that when it fails will cause a 
deadlock in Derby Netwok Server

I am writing  a test case for

https://issues.apache.org/jira/browse/DERBY-6879

Which if written correctly will cause a deadlock in the derby network server.   
How should this test case recover from this?   System.exit(1)?

The test case will never continue because of the deadlock and the derby network 
server that the test case is running against will not be able to be shutdown 
either.   Probably Derby should have a piece of self-monitoring code that looks 
periodically for a deadlock and does a "panic" type of shutdown.



________________________________
Canoga Perkins
20600 Prairie Street
Chatsworth, CA 91311
(818) 718-6300

This e-mail and any attached document(s) is confidential and is intended only 
for the review of the party to whom it is addressed. If you have received this 
transmission in error, please notify the sender immediately and discard the 
original message and any attachment(s).

________________________________
Canoga Perkins
20600 Prairie Street
Chatsworth, CA 91311
(818) 718-6300

This e-mail and any attached document(s) is confidential and is intended only 
for the review of the party to whom it is addressed. If you have received this 
transmission in error, please notify the sender immediately and discard the 
original message and any attachment(s).

Reply via email to