[FIX] close some statements

This commit is contained in:
Edouard DUPIN 2024-06-02 21:28:29 +02:00
parent 1c02d333a3
commit 5bbcf63c42

View File

@ -47,6 +47,7 @@ import org.slf4j.LoggerFactory;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import jakarta.ws.rs.DefaultValue;
import jakarta.ws.rs.InternalServerErrorException;
@ -797,6 +798,7 @@ public class DataAccess {
return out;
}
@SuppressFBWarnings("SQL_PREPARED_STATEMENT_GENERATED_FROM_NONCONSTANT_STRING")
public static <T> T insert(final T data, final QueryOption... option) throws Exception {
final Class<?> clazz = data.getClass();
final QueryOptions options = new QueryOptions(option);
@ -1028,7 +1030,7 @@ public class DataAccess {
}
public static <ID_TYPE> QueryCondition getTableIdCondition(final Class<?> clazz, final ID_TYPE idKey)
throws Exception {
throws DataAccessException {
// Find the ID field type ....
final Field idField = AnnotationTools.getIdField(clazz);
if (idField == null) {
@ -1115,6 +1117,7 @@ public class DataAccess {
return updateWhere(data, options);
}
@SuppressFBWarnings("SQL_PREPARED_STATEMENT_GENERATED_FROM_NONCONSTANT_STRING")
public static <T> int updateWhere(final T data, QueryOptions options) throws Exception {
final Class<?> clazz = data.getClass();
if (options == null) {
@ -1196,8 +1199,8 @@ public class DataAccess {
if (!firstField) {
LOGGER.debug("generate update query: '{}'", query.toString());
// prepare the request:
final PreparedStatement ps = entry.connection.prepareStatement(query.toString(),
Statement.RETURN_GENERATED_KEYS);
try (final PreparedStatement ps = entry.connection.prepareStatement(query.toString(),
Statement.RETURN_GENERATED_KEYS)) {
final CountInOut iii = new CountInOut(1);
for (final Field field : clazz.getFields()) {
// static field is only for internal global declaration ==> remove it ..
@ -1229,9 +1232,9 @@ public class DataAccess {
}
condition.injectQuery(ps, iii);
final int out = ps.executeUpdate();
ps.close();
return out;
}
}
} catch (final SQLException ex) {
ex.printStackTrace();
}
@ -1294,17 +1297,19 @@ public class DataAccess {
throws SQLException, IOException {
final QueryOptions options = new QueryOptions(option);
final DBEntry entry = DBInterfaceOption.getAutoEntry(options);
final Statement stmt = entry.connection.createStatement();
try (final Statement stmt = entry.connection.createStatement()) {
return stmt.executeUpdate(query);
}
}
public static boolean executeQuery(final String query, final QueryOption... option)
throws SQLException, IOException {
final QueryOptions options = new QueryOptions(option);
final DBEntry entry = DBInterfaceOption.getAutoEntry(options);
final Statement stmt = entry.connection.createStatement();
try (final Statement stmt = entry.connection.createStatement()) {
return stmt.execute(query);
}
}
public static <T> T getWhere(final Class<T> clazz, final QueryOptions options) throws Exception {
options.add(new Limit(1));
@ -1395,14 +1400,13 @@ public class DataAccess {
}
@SuppressWarnings("unchecked")
public static <T> List<T> getsWhere(final Class<T> clazz, final QueryOptions options) throws Exception {
public static <T> List<T> getsWhere(final Class<T> clazz, final QueryOptions options)
throws DataAccessException, IOException {
final Condition condition = conditionFusionOrEmpty(options, false);
final List<LazyGetter> lazyCall = new ArrayList<>();
final String deletedFieldName = AnnotationTools.getDeletedFieldName(clazz);
final DBEntry entry = DBInterfaceOption.getAutoEntry(options);
final List<T> outs = new ArrayList<>();
// real add in the BDD:
try {
try (final DBEntry entry = DBInterfaceOption.getAutoEntry(options)) {
final CountInOut count = new CountInOut();
final StringBuilder querySelect = new StringBuilder();
StringBuilder query = new StringBuilder();
@ -1432,8 +1436,8 @@ public class DataAccess {
}
LOGGER.debug("generate the query: '{}'", query.toString());
// prepare the request:
final PreparedStatement ps = entry.connection.prepareStatement(query.toString(),
Statement.RETURN_GENERATED_KEYS);
try (final PreparedStatement ps = entry.connection.prepareStatement(query.toString(),
Statement.RETURN_GENERATED_KEYS)) {
final CountInOut iii = new CountInOut(1);
condition.injectQuery(ps, iii);
if (limits.size() == 1) {
@ -1452,13 +1456,13 @@ public class DataAccess {
for (final LazyGetter elem : lazyCall) {
elem.doRequest();
}
}
} catch (final SQLException ex) {
ex.printStackTrace();
throw ex;
throw new DataAccessException("Catch a SQL Exception: " + ex.getMessage());
} catch (final Exception ex) {
ex.printStackTrace();
} finally {
entry.close();
throw new DataAccessException("Catch an Exception: " + ex.getMessage());
}
return outs;
}
@ -1672,18 +1676,18 @@ public class DataAccess {
}
}
public static <ID_TYPE> int unsetDelete(final Class<?> clazz, final ID_TYPE id) throws Exception {
public static <ID_TYPE> int unsetDelete(final Class<?> clazz, final ID_TYPE id) throws DataAccessException {
return unsetDeleteWhere(clazz, new Condition(getTableIdCondition(clazz, id)));
}
public static <ID_TYPE> int unsetDelete(final Class<?> clazz, final ID_TYPE id, final QueryOption... option)
throws Exception {
throws DataAccessException {
final QueryOptions options = new QueryOptions(option);
options.add(new Condition(getTableIdCondition(clazz, id)));
return unsetDeleteWhere(clazz, options.getAllArray());
}
public static int unsetDeleteWhere(final Class<?> clazz, final QueryOption... option) throws Exception {
public static int unsetDeleteWhere(final Class<?> clazz, final QueryOption... option) throws DataAccessException {
final QueryOptions options = new QueryOptions(option);
final Condition condition = conditionFusionOrEmpty(options, true);
final String tableName = AnnotationTools.getTableName(clazz, options);
@ -1691,7 +1695,12 @@ public class DataAccess {
if (deletedFieldName == null) {
throw new DataAccessException("The class " + clazz.getCanonicalName() + " has no deleted field");
}
final DBEntry entry = DBInterfaceOption.getAutoEntry(options);
DBEntry entry;
try {
entry = DBInterfaceOption.getAutoEntry(options);
} catch (final IOException ex) {
throw new DataAccessException("Fail to connect the DB: " + ex.getMessage());
}
final StringBuilder query = new StringBuilder();
query.append("UPDATE `");
query.append(tableName);
@ -1701,13 +1710,14 @@ public class DataAccess {
// need to disable the deleted false because the model must be unselected to be updated.
options.add(QueryOptions.ACCESS_DELETED_ITEMS);
condition.whereAppendQuery(query, tableName, options, deletedFieldName);
try {
final PreparedStatement ps = entry.connection.prepareStatement(query.toString());
try (final PreparedStatement ps = entry.connection.prepareStatement(query.toString())) {
final CountInOut iii = new CountInOut(1);
condition.injectQuery(ps, iii);
return ps.executeUpdate();
} finally {
entry.close();
} catch (final SQLException ex) {
throw new DataAccessException("Catch SQL error:" + ex.getMessage());
} catch (final Exception ex) {
throw new DataAccessException("Fail to excute the SQL query:" + ex.getMessage());
}
}