Can I cancel a JDBC connection?

0

I'm using the following code in a Thread:

            try {
                Class.forName("org.postgresql.Driver");
                DriverManager.setLoginTimeout(60);
                conexao = DriverManager.getConnection(url,usuario,senha);
            }catch(Exception e){
                mensagemErro = e.getMessage();
            }

And on another Thread (from the GUI) has a window like cancel button, but I do not know how to cancel the DriverManager. connection is a public variable.

    
asked by anonymous 22.02.2016 / 14:06

1 answer

0

Specifically for the PostgreSQL Driver, according to changelog , at least from the 9.3-1101 version of 2/14/2014 You can call Thread.interrupt() on Thread trying to make the connection.

This is a technique known in Java to "wake up" threads that are waiting on some I / O operation. And that's when the InterruptedException exception (the one you need to handle every time I call the Thread.sleep ) makes some sense.

Basically, the interrupt() method activates a flag on the thread and unlocks it (if it is locked), so it has the change to treat the flag and stop processing.

According to the change log link, the driver checks the interrupt bit and throws an exception. So just ignore it.

    
23.02.2016 / 02:27