[FIX] clean the last one connection error

This commit is contained in:
Edouard DUPIN 2024-12-16 00:02:02 +01:00
parent 30ef425d75
commit df12cd7413
3 changed files with 22 additions and 30 deletions

View File

@ -24,47 +24,45 @@ public abstract class DbIo implements Closeable {
@Override @Override
public synchronized final void close() throws IOException { public synchronized final void close() throws IOException {
LOGGER.error("[{}] >>>>>>>>>>> Request close count={}", this.id, this.count); LOGGER.trace("[{}] Request close count={}", this.id, this.count);
if (this.count <= 0) { if (this.count <= 0) {
LOGGER.error("[{}] >>>>>>>>>>> Request one more close: {}", this.id, this.getClass().getCanonicalName()); LOGGER.error("[{}] Request one more close", this.id);
return; return;
} }
this.count--; this.count--;
if (this.count == 0) { if (this.count == 0) {
LOGGER.warn("v>>>>>>>>>>> close: {}", this.id, this.getClass().getCanonicalName()); LOGGER.trace("[{}] close", this.id);
closeImplement(); closeImplement();
} else { } else {
LOGGER.debug("v>>>>>>>>>>> postponed close: {}", this.id, this.getClass().getCanonicalName()); LOGGER.trace("[{}] postponed close", this.id);
} }
} }
public synchronized final void closeForce() throws IOException { public synchronized final void closeForce() throws IOException {
LOGGER.warn("[{}] >>>>>>>>>>> Request Force close count={}", this.id, this.count); LOGGER.trace("[{}] Request Force close count={}", this.id, this.count);
if (this.count == 0) { if (this.count == 0) {
LOGGER.info("[{}] >>>>>>>>>>> Nothing to do in force close, DB is already closed", this.id); LOGGER.trace("[{}] Nothing to do in force close, DB is already closed", this.id);
return; return;
} }
if (this.config.getKeepConnected()) { if (this.config.getKeepConnected()) {
if (this.count >= 2) { if (this.count >= 2) {
LOGGER.error("[{}] >>>>>>>>>>> force close: {} with {} connection on it", this.id, LOGGER.error("[{}] Force close: with {} connection on it", this.id, this.count - 1);
this.getClass().getCanonicalName(), this.count - 1);
} }
} else if (this.count >= 1) { } else if (this.count >= 1) {
LOGGER.error("[{}] >>>>>>>>>>> force close: {} with {} connection on it", this.id, LOGGER.error("[{}] Force close: with {} connection on it", this.id, this.count);
this.getClass().getCanonicalName(), this.count);
} }
this.count = 0; this.count = 0;
LOGGER.warn("[{}] >>>>>>>>>>> force close: {}", this.id, this.getClass().getCanonicalName()); LOGGER.trace("[{}] Force close", this.id);
closeImplement(); closeImplement();
} }
public synchronized final void open() throws IOException { public synchronized final void open() throws IOException {
LOGGER.warn("[{}] >>>>>>>>>>> Request open count={}", this.id, this.count); LOGGER.trace("[{}] Request open count={}", this.id, this.count);
if (this.count == 0) { if (this.count == 0) {
LOGGER.warn("[{}] >>>>>>>>>>> open: {}", this.id, this.getClass().getCanonicalName()); LOGGER.trace("[{}] open", this.id);
openImplement(); openImplement();
} else { } else {
LOGGER.debug("[{}] >>>>>>>>>>> already open: {}", this.id, this.getClass().getCanonicalName()); LOGGER.trace("[{}] open: already done", this.id);
} }
this.count++; this.count++;

View File

@ -34,7 +34,6 @@ public class DbIoFactory {
dbIo.open(); dbIo.open();
dbIoStored.add(dbIo); dbIoStored.add(dbIo);
} }
dbIo.open();
return dbIo; return dbIo;
} }

View File

@ -11,18 +11,17 @@ import org.slf4j.LoggerFactory;
public class DbIoSql extends DbIo { public class DbIoSql extends DbIo {
final static Logger LOGGER = LoggerFactory.getLogger(DbIoSql.class); final static Logger LOGGER = LoggerFactory.getLogger(DbIoSql.class);
private Connection con = null; private Connection connection = null;
public DbIoSql(final DbConfig config) throws IOException { public DbIoSql(final DbConfig config) throws IOException {
super(config); super(config);
} }
public Connection getConnection() { public Connection getConnection() {
if (this.con == null) { if (this.connection == null) {
LOGGER.error("[{}] >>>>>>>>>> Retrieve a closed connection !!!", this.id); LOGGER.error("[{}] Retrieve a closed connection !!!", this.id);
} }
LOGGER.error("[{}] ++++++++++ Retrieve connection {}", this.id, this.con); return this.connection;
return this.con;
} }
@Override @Override
@ -30,30 +29,26 @@ public class DbIoSql extends DbIo {
final String dbUrl = this.config.getUrl(); final String dbUrl = this.config.getUrl();
final String login = this.config.getLogin(); final String login = this.config.getLogin();
final String password = this.config.getPassword(); final String password = this.config.getPassword();
LOGGER.error("[{}] >>>>>>>>>> openImplement ", this.id);
try { try {
this.con = DriverManager.getConnection(dbUrl, login, password); this.connection = DriverManager.getConnection(dbUrl, login, password);
LOGGER.error("[{}] >>>>>>>>>> openImplement ==> {}", this.id, this.con);
} catch (final SQLException ex) { } catch (final SQLException ex) {
LOGGER.error("Connection db fail: " + ex.getMessage() + " On URL: " + dbUrl); LOGGER.error("Connection db fail: " + ex.getMessage() + " On URL: " + dbUrl);
throw new IOException("Connection db fail: " + ex.getMessage() + " On URL: " + dbUrl); throw new IOException("Connection db fail: " + ex.getMessage() + " On URL: " + dbUrl);
} }
if (this.con == null) { if (this.connection == null) {
LOGGER.error("Request open of un-open connection !!!"); throw new IOException("Connection db fail: NULL On URL: " + dbUrl);
return;
} }
} }
@Override @Override
synchronized public void closeImplement() throws IOException { synchronized public void closeImplement() throws IOException {
LOGGER.error("[{}] >>>>>>>>>> closeImplement ", this.id); if (this.connection == null) {
if (this.con == null) {
LOGGER.error("Request close of un-open connection !!!"); LOGGER.error("Request close of un-open connection !!!");
return; return;
} }
try { try {
this.con.close(); this.connection.close();
this.con = null; this.connection = null;
} catch (final SQLException ex) { } catch (final SQLException ex) {
throw new IOException("Dis-connection db fail: " + ex.getMessage()); throw new IOException("Dis-connection db fail: " + ex.getMessage());
} }