Use of Swingworker in connection with standalone bank

3

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

One problem that is not critical, but a little annoying, is the fact that the cited bank delayed the opening of the app (probably because it is in a network folder and this bank loads the entire DB file into memory before opening) , after analyzing the Netbeans profile determiner, I came to this conclusion as well.

I researched swingworker and then adapted my connection factory class () to perform the opening and closing of connection and features in Parallel Thread, and this made the program have a better answer.

My question is how parallelism is implemented in this class can cause some problem I'm not seeing right now?

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;
    }
}
    
asked by anonymous 18.08.2015 / 12:28

1 answer

5

After reviewing a few topics and reading the documentation and testing, I came to the conclusion that using SwingWorker is not recommended for this case since the application is StandAlone / em>, and the use of threads would only be reflected on the computer of the one executing it at the moment, as well as stumbling on a HSQLDB restriction, where StandAlone connections only work only one at a time, so if multiple people access at the same time, the singleton method will only work for the machine one at a time.

    
31.08.2015 / 15:51