0

I have a pretty simplistic JavaFX application. In it, I have a Java object for handling database activities, mainly executing queries. To prevent my UI from completely freezing while the query executes, I've implemented a background thread using the javafx.concurrent.Service. This works great on my connect method, which doesn't return anything. However, in my query method it immediately jumps to the return line, and of course returns null. Then it goes back and runs the query, but it's already returned an empty arraylist.

What am I doing wrong?

Here's my method:

public ArrayList<Foo> runQuery() throws SQLException {
        ArrayList<Foo> result = new ArrayList<Foo>();
        backgroundThread = new Service<Void>() {
            @Override
            protected Task<Void> createTask() {
                return new Task<Void>() {
                    protected Void call() throws Exception {
                        stmt = conn.createStatement();
                        String query = "Select stuff...
                        rs = stmt.executeQuery(query);
                        return null;
                    }
                };
            }
        };
        backgroundThread.setOnSucceeded(new EventHandler<WorkerStateEvent>() {
            @Override
            public void handle(WorkerStateEvent argo) {
                try {
                    while (rs.next()) {
                        result.add(new Foo(rs.getString(1)));
                    }
                } catch (SQLException e) {
                    e.printStackTrace();
                }
                controller.addLogEntry("done.\n");
            }
        });
    backgroundThread.restart(); 
    return result;
    }
Andrew
  • 8,445
  • 3
  • 28
  • 46
  • Why are using a Service at all here? You are only executing the Service once. You may as well just use a Task instead of a Service. – jewelsea Apr 24 '17 at 23:22
  • Related [sample](https://gist.github.com/jewelsea/4957967) for [JavaFX - Background Thread for SQL Query](http://stackoverflow.com/questions/14878788/javafx-background-thread-for-sql-query) – jewelsea Apr 24 '17 at 23:31
  • 1
    Also related (and a better answer than mine): [Using threads to make database requests](http://stackoverflow.com/questions/30249493/using-threads-to-make-database-requests) – jewelsea Apr 25 '17 at 00:10
  • You might want to add a [setOnFailed](https://docs.oracle.com/javase/8/javafx/api/javafx/concurrent/Task.html#setOnFailed-javafx.event.EventHandler-) handler in case the database query is failing and throwing an exception. – jewelsea Apr 25 '17 at 00:15
  • "Returning before the background thread completes" is more or less the definition of running something in a background thread (you start the task in another thread, and the current thread proceeds immediately). You need to return the list from the task's `call` method, not from the current method. See either of the linked questions for details. – James_D Apr 25 '17 at 00:36

0 Answers0