public class QueryAgent extends Object
This class provides a mechanism which allows the client not to block indeffinately on the event dispatcher thread. This means we can give feedback to the user if a query is taking a long time (progress bar, hour-glass, etc) and components will repaint correctly. It also allows us to cancel any query in progress (because the event dispatcher isn't locked we can handle UI events and the interface won't be frozen).
We acheive this behaviour with a hack of the system EventQueue (the same way modality works in swing.JInternalFrame). The low down is, we emulate the Java event dispatcher inner loop so that all AWT events (repaints/ component events/etc) are dispatched in our own controlled loop that is blocked with respect to the callee. (Find this blocking behaviour in SwingBlockUtil)
I consider this a mild hack. This class may be incompatible with future versions of Java if the AWT event mechanism is altered. It may also not work happily with non-Sun based implementations of Java.
NOTE: Other ways of acheiving non-AWT locking queries is with a delegate implementation. The method that executes the query returns immediately and the result is sent to a delegate. While this system sounds nice in theory, it's not pretty in practice. Especially if you need to execute many queries in a specific sequence. Also, handling exceptions is a nightmare with this idea.
Constructor and Description |
---|
QueryAgent(Connection connection)
Constructs the query agent.
|
Modifier and Type | Method and Description |
---|---|
static void |
cancel()
Cancels the query that is currently being executed (if any).
|
void |
cancelQuery()
Cancels any query that is currently being executed by this agent.
|
Connection |
connection()
Returns the connection for the JDBC interface.
|
static ResultSet |
execute(Query query)
Executes a query on the default query agent for this VM.
|
ResultSet |
executeQuery(Query query)
Executes a query, blocking until a response from the server has been
received.
|
static QueryAgent |
getDefaultAgent()
Returns the QueryAgent for this VM.
|
static void |
initAgent(Connection connection)
Initialises the QueryAgent with the given JDBC Connection.
|
public QueryAgent(Connection connection)
public Connection connection()
public ResultSet executeQuery(Query query) throws SQLException, InterruptedException
This is based on the 'setModal' method found in JInternalFrame. It is up to the developer to block the user interface elements from being used while we are waiting for a query result.
Throws an InterruptedException if the query is cancelled via the 'cancelQuery' method.
SQLException
InterruptedException
public void cancelQuery()
public static void initAgent(Connection connection)
public static QueryAgent getDefaultAgent()
public static ResultSet execute(Query query) throws SQLException, InterruptedException
This will block until the server has responded to the query and a result has been obtained. While this method does block, it still will service events on the event dispatcher queue. This means, UI elements will still work (buttons/text fields/etc) at the same time the server is fetching the result. And, we still have blocking behaviour for the callee.
What this ultimately means, is that components can be repainted and we can have animations indicating the progress (feedback from the UI to the user that it hasn't frozen up) and buttons to cancel the query if it's taking too long.
SQLException
InterruptedException
public static void cancel()
This will throw an InterruptedException from the 'execute' method if it is waiting.
Copyright © 2015. All rights reserved.