Use of Swingworker in connection with standalone bench

Asked

Viewed 135 times

1

I have an application in java that I made in my work, to control sequence numbering of crafts, the same is in jar format in the network (I made using swing in java + HSQLDB).

One problem that wasn’t critical, but bothered a little, is the fact that the database cited delayed opening the app (probably because it was in a network folder and this database loads the entire DB file into memory before opening), after analyzing it by the Netbeans profile determinator, I was able to come to this conclusion too.

I researched about swingworker and then adapted my class connection factory() to perform connection opening and closing and resources in parallel thread, and this made the program have a better response.

My doubt is how is implemented parallelism in this class can generate some problem that I am not seeing at this time?

Factory class with swingworker

package com.dfmachado.geroficios.Models;

import com.dfmachado.geroficios.excecoes.ExcecaoErroIO;
import com.dfmachado.geroficios.excecoes.ExcecaoPadrao;
import com.dfmachado.geroficios.utils.Propriedade;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.concurrent.ExecutionException;
import javax.swing.SwingWorker;

/**
 *
 * @author diego
 */
public class ConnectionFactory {

    private static String url = null;
    private static Propriedade prop = null;
    private static boolean openConnWorkerIsDone = false;
    private static boolean closeConnWorkerIsDone = true;

    private ConnectionFactory() {
    }

    public static Connection createConnection() throws SQLException {
        try {
            prop = new Propriedade();
        } catch (IOException ex) {
            throw new ExcecaoErroIO(ex.getMessage());
        }
        url = prop.getJdbcURL() + prop.getDbName();

        try {
            if(closeConnWorkerIsDone)
            return OpenConnWorker(url, prop.getUser(), prop.getUserPass());
        } catch (InterruptedException | ExecutionException ex) {
            throw new ExcecaoPadrao(ex.getMessage());
        }
        return null;
    }

    public static void closeConnection(Connection conn) throws SQLException {
        close(conn, null, null);
    }

    public static void closeConnection(Connection conn, PreparedStatement ps) throws SQLException {
        close(conn, ps, null);
    }

    public static void closeConnection(Connection conn, PreparedStatement ps, ResultSet rs) throws SQLException {
        close(conn, ps, rs);
    }

    private static void close(final Connection conn, final PreparedStatement ps, final ResultSet rs) throws SQLException {
        if (openConnWorkerIsDone) {
            SwingWorker closeConnWorker = new SwingWorker() {
                @Override
                protected Object doInBackground() throws Exception {
                    closeConnWorkerIsDone = false;
                    if (rs != null) {
                        rs.close();
                    }
                    if (ps != null) {
                        ps.close();
                    }
                    if (conn != null) {
                        if (prop.getDbTipo().equals("HSQL")) {
                            conn.prepareStatement("shutdown compact").execute();
                        }
                        conn.close();
                    }
                    return null;
                }

                @Override
                protected void done() {
                    super.done();
                    closeConnWorkerIsDone = true;
                }
            };
            closeConnWorker.execute();
        }
    }

    private static Connection OpenConnWorker(final String url, final String user, final String pass) throws InterruptedException, ExecutionException {
        Connection conn;
        SwingWorker OpenConnWorker = new SwingWorker() {

            @Override
            protected Object doInBackground() throws Exception {
                openConnWorkerIsDone = false;
                return DriverManager.getConnection(url, user, pass);
            }

            @Override
            protected void done() {
                super.done();
                openConnWorkerIsDone = true;
            }
        };
        OpenConnWorker.execute();
        conn = (Connection) OpenConnWorker.get();
        return conn;
    }
}

1 answer

3


After analyzing some topics and reading some documentation and doing tests, I came to the conclusion that the use of Swingworker is not recommended for this case since the application is Standalone, and the use of threads would only be reflected in the computer of those who run it at the time, in addition to bumping into a restriction of HSQLDB, where Standalone connections only work one at a time, so if several people access at the same time, the Singleton method will only work for the machine one at a time.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.