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;
}
}