|
|
So imagine you're comfortably sleeping at home. Suddenly you wake up
and you find out you're sitting in your car, driving at 65 MPH in
the highway. That's a dangerous situation, right?
Now imagine you're a JDBC driver and you're comfortably performing a
query against a server. You're talking, say, OCI to an Oracle server.
Suddenly someone "Thread.interrupt()"s you
and then asks you to "close()" the JDBC connection. You may be
able to successfully close the connection. Or you may not.
Maybe you can close the connection, but the server is left in
a weird state, thinking you're still asking for the query.
|
What I mean is that Thread.interrupt() (or SwingWorker.cancel()) certain
operations may not be a very good idea. By interrupting threads you
may be interrupting network protocols, for instance, and that may
be asking for trouble with some JDBC drivers. You can build a SQL application
and interrupt and close the connections perfectly well with a given
driver, and completely fail with another driver.
What I really mean is that if you want to suddenly cancel a running
process (whatever that is) you should try to do that in the safest,
cleanest way. An idea is to use a "boolean wantsToRun=true" flag
somewhere in your SwingWorker constructors and keep the things rolling
in the "doInBackground()" method as long as "wantsToRun" is true.
So there comes our SwingWorker suggestion number II:
SwingWorker suggestion II: try to cancel() as cleanly as possible
Use a flag in your "doInBackground()" loops to check if your SwingWorker
is still expected to be run. Since you cannot override "cancel(boolean)" in
your SwingWorker (it's final :-( ) then write a method to gracefully
ask for cancellation. If you use SwingWorker.cancel(true) to stop your
running process doublecheck that everything is correctly cancelled by
thoroughly testing your application.
Note that this suggestion applies to SwingWorkers and to any Thread you build.
So, to summarize, think it twice before suddenly stopping trains
Threads. Results might be quite different from what you expect.
Happy Swinging,
Antonio
|
Enviado por Davis en agosto 17, 2005 a las 07:46 AM CEST #
What I mean is that it may fail. Depends on the JDBC driver you're using and on when you invoke interrupt(). You need to test it: it's a way to stop an ongoing JDBC call, but it's not a clean way to do it.
Enviado por Antonio en agosto 17, 2005 a las 07:53 PM CEST #
Enviado por Neil Weber en agosto 19, 2005 a las 11:16 PM CEST #
Enviado por Chris Nokleberg en septiembre 05, 2005 a las 01:06 PM CEST #