[FIX] close some statements
This commit is contained in:
parent
1c02d333a3
commit
5bbcf63c42
@ -47,6 +47,7 @@ import org.slf4j.LoggerFactory;
|
|||||||
import com.fasterxml.jackson.databind.JsonNode;
|
import com.fasterxml.jackson.databind.JsonNode;
|
||||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
|
|
||||||
|
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
|
||||||
import jakarta.ws.rs.DefaultValue;
|
import jakarta.ws.rs.DefaultValue;
|
||||||
import jakarta.ws.rs.InternalServerErrorException;
|
import jakarta.ws.rs.InternalServerErrorException;
|
||||||
|
|
||||||
@ -797,6 +798,7 @@ public class DataAccess {
|
|||||||
return out;
|
return out;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@SuppressFBWarnings("SQL_PREPARED_STATEMENT_GENERATED_FROM_NONCONSTANT_STRING")
|
||||||
public static <T> T insert(final T data, final QueryOption... option) throws Exception {
|
public static <T> T insert(final T data, final QueryOption... option) throws Exception {
|
||||||
final Class<?> clazz = data.getClass();
|
final Class<?> clazz = data.getClass();
|
||||||
final QueryOptions options = new QueryOptions(option);
|
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)
|
public static <ID_TYPE> QueryCondition getTableIdCondition(final Class<?> clazz, final ID_TYPE idKey)
|
||||||
throws Exception {
|
throws DataAccessException {
|
||||||
// Find the ID field type ....
|
// Find the ID field type ....
|
||||||
final Field idField = AnnotationTools.getIdField(clazz);
|
final Field idField = AnnotationTools.getIdField(clazz);
|
||||||
if (idField == null) {
|
if (idField == null) {
|
||||||
@ -1115,6 +1117,7 @@ public class DataAccess {
|
|||||||
return updateWhere(data, options);
|
return updateWhere(data, options);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@SuppressFBWarnings("SQL_PREPARED_STATEMENT_GENERATED_FROM_NONCONSTANT_STRING")
|
||||||
public static <T> int updateWhere(final T data, QueryOptions options) throws Exception {
|
public static <T> int updateWhere(final T data, QueryOptions options) throws Exception {
|
||||||
final Class<?> clazz = data.getClass();
|
final Class<?> clazz = data.getClass();
|
||||||
if (options == null) {
|
if (options == null) {
|
||||||
@ -1196,41 +1199,41 @@ public class DataAccess {
|
|||||||
if (!firstField) {
|
if (!firstField) {
|
||||||
LOGGER.debug("generate update query: '{}'", query.toString());
|
LOGGER.debug("generate update query: '{}'", query.toString());
|
||||||
// prepare the request:
|
// prepare the request:
|
||||||
final PreparedStatement ps = entry.connection.prepareStatement(query.toString(),
|
try (final PreparedStatement ps = entry.connection.prepareStatement(query.toString(),
|
||||||
Statement.RETURN_GENERATED_KEYS);
|
Statement.RETURN_GENERATED_KEYS)) {
|
||||||
final CountInOut iii = new CountInOut(1);
|
final CountInOut iii = new CountInOut(1);
|
||||||
for (final Field field : clazz.getFields()) {
|
for (final Field field : clazz.getFields()) {
|
||||||
// static field is only for internal global declaration ==> remove it ..
|
// static field is only for internal global declaration ==> remove it ..
|
||||||
if (java.lang.reflect.Modifier.isStatic(field.getModifiers())) {
|
if (java.lang.reflect.Modifier.isStatic(field.getModifiers())) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
final String name = AnnotationTools.getFieldName(field);
|
final String name = AnnotationTools.getFieldName(field);
|
||||||
if (!filter.getValues().contains(name)) {
|
if (!filter.getValues().contains(name)) {
|
||||||
continue;
|
continue;
|
||||||
} else if (AnnotationTools.isGenericField(field)) {
|
} else if (AnnotationTools.isGenericField(field)) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
final DataAccessAddOn addOn = findAddOnforField(field);
|
final DataAccessAddOn addOn = findAddOnforField(field);
|
||||||
if (addOn != null && !addOn.canInsert(field)) {
|
if (addOn != null && !addOn.canInsert(field)) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
if (addOn == null) {
|
if (addOn == null) {
|
||||||
final Class<?> type = field.getType();
|
final Class<?> type = field.getType();
|
||||||
if (!type.isPrimitive()) {
|
if (!type.isPrimitive()) {
|
||||||
final Object tmp = field.get(data);
|
final Object tmp = field.get(data);
|
||||||
if (tmp == null && field.getDeclaredAnnotationsByType(DefaultValue.class).length != 0) {
|
if (tmp == null && field.getDeclaredAnnotationsByType(DefaultValue.class).length != 0) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
setValuedb(type, data, iii, field, ps);
|
||||||
|
} else {
|
||||||
|
addOn.insertData(ps, field, data, iii);
|
||||||
}
|
}
|
||||||
setValuedb(type, data, iii, field, ps);
|
|
||||||
} else {
|
|
||||||
addOn.insertData(ps, field, data, iii);
|
|
||||||
}
|
}
|
||||||
|
condition.injectQuery(ps, iii);
|
||||||
|
final int out = ps.executeUpdate();
|
||||||
|
return out;
|
||||||
}
|
}
|
||||||
condition.injectQuery(ps, iii);
|
|
||||||
final int out = ps.executeUpdate();
|
|
||||||
ps.close();
|
|
||||||
return out;
|
|
||||||
}
|
}
|
||||||
} catch (final SQLException ex) {
|
} catch (final SQLException ex) {
|
||||||
ex.printStackTrace();
|
ex.printStackTrace();
|
||||||
@ -1294,16 +1297,18 @@ public class DataAccess {
|
|||||||
throws SQLException, IOException {
|
throws SQLException, IOException {
|
||||||
final QueryOptions options = new QueryOptions(option);
|
final QueryOptions options = new QueryOptions(option);
|
||||||
final DBEntry entry = DBInterfaceOption.getAutoEntry(options);
|
final DBEntry entry = DBInterfaceOption.getAutoEntry(options);
|
||||||
final Statement stmt = entry.connection.createStatement();
|
try (final Statement stmt = entry.connection.createStatement()) {
|
||||||
return stmt.executeUpdate(query);
|
return stmt.executeUpdate(query);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static boolean executeQuery(final String query, final QueryOption... option)
|
public static boolean executeQuery(final String query, final QueryOption... option)
|
||||||
throws SQLException, IOException {
|
throws SQLException, IOException {
|
||||||
final QueryOptions options = new QueryOptions(option);
|
final QueryOptions options = new QueryOptions(option);
|
||||||
final DBEntry entry = DBInterfaceOption.getAutoEntry(options);
|
final DBEntry entry = DBInterfaceOption.getAutoEntry(options);
|
||||||
final Statement stmt = entry.connection.createStatement();
|
try (final Statement stmt = entry.connection.createStatement()) {
|
||||||
return stmt.execute(query);
|
return stmt.execute(query);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static <T> T getWhere(final Class<T> clazz, final QueryOptions options) throws Exception {
|
public static <T> T getWhere(final Class<T> clazz, final QueryOptions options) throws Exception {
|
||||||
@ -1395,14 +1400,13 @@ public class DataAccess {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@SuppressWarnings("unchecked")
|
@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 Condition condition = conditionFusionOrEmpty(options, false);
|
||||||
final List<LazyGetter> lazyCall = new ArrayList<>();
|
final List<LazyGetter> lazyCall = new ArrayList<>();
|
||||||
final String deletedFieldName = AnnotationTools.getDeletedFieldName(clazz);
|
final String deletedFieldName = AnnotationTools.getDeletedFieldName(clazz);
|
||||||
final DBEntry entry = DBInterfaceOption.getAutoEntry(options);
|
|
||||||
final List<T> outs = new ArrayList<>();
|
final List<T> outs = new ArrayList<>();
|
||||||
// real add in the BDD:
|
try (final DBEntry entry = DBInterfaceOption.getAutoEntry(options)) {
|
||||||
try {
|
|
||||||
final CountInOut count = new CountInOut();
|
final CountInOut count = new CountInOut();
|
||||||
final StringBuilder querySelect = new StringBuilder();
|
final StringBuilder querySelect = new StringBuilder();
|
||||||
StringBuilder query = new StringBuilder();
|
StringBuilder query = new StringBuilder();
|
||||||
@ -1432,33 +1436,33 @@ public class DataAccess {
|
|||||||
}
|
}
|
||||||
LOGGER.debug("generate the query: '{}'", query.toString());
|
LOGGER.debug("generate the query: '{}'", query.toString());
|
||||||
// prepare the request:
|
// prepare the request:
|
||||||
final PreparedStatement ps = entry.connection.prepareStatement(query.toString(),
|
try (final PreparedStatement ps = entry.connection.prepareStatement(query.toString(),
|
||||||
Statement.RETURN_GENERATED_KEYS);
|
Statement.RETURN_GENERATED_KEYS)) {
|
||||||
final CountInOut iii = new CountInOut(1);
|
final CountInOut iii = new CountInOut(1);
|
||||||
condition.injectQuery(ps, iii);
|
condition.injectQuery(ps, iii);
|
||||||
if (limits.size() == 1) {
|
if (limits.size() == 1) {
|
||||||
limits.get(0).injectQuery(ps, iii);
|
limits.get(0).injectQuery(ps, iii);
|
||||||
}
|
}
|
||||||
// execute the request
|
// execute the request
|
||||||
final ResultSet rs = ps.executeQuery();
|
final ResultSet rs = ps.executeQuery();
|
||||||
while (rs.next()) {
|
while (rs.next()) {
|
||||||
count.value = 1;
|
count.value = 1;
|
||||||
final CountInOut countNotNull = new CountInOut(0);
|
final CountInOut countNotNull = new CountInOut(0);
|
||||||
final Object data = createObjectFromSQLRequest(rs, clazz, count, countNotNull, options, lazyCall);
|
final Object data = createObjectFromSQLRequest(rs, clazz, count, countNotNull, options, lazyCall);
|
||||||
final T out = (T) data;
|
final T out = (T) data;
|
||||||
outs.add(out);
|
outs.add(out);
|
||||||
}
|
}
|
||||||
LOGGER.info("Async calls: {}", lazyCall.size());
|
LOGGER.info("Async calls: {}", lazyCall.size());
|
||||||
for (final LazyGetter elem : lazyCall) {
|
for (final LazyGetter elem : lazyCall) {
|
||||||
elem.doRequest();
|
elem.doRequest();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
} catch (final SQLException ex) {
|
} catch (final SQLException ex) {
|
||||||
ex.printStackTrace();
|
ex.printStackTrace();
|
||||||
throw ex;
|
throw new DataAccessException("Catch a SQL Exception: " + ex.getMessage());
|
||||||
} catch (final Exception ex) {
|
} catch (final Exception ex) {
|
||||||
ex.printStackTrace();
|
ex.printStackTrace();
|
||||||
} finally {
|
throw new DataAccessException("Catch an Exception: " + ex.getMessage());
|
||||||
entry.close();
|
|
||||||
}
|
}
|
||||||
return outs;
|
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)));
|
return unsetDeleteWhere(clazz, new Condition(getTableIdCondition(clazz, id)));
|
||||||
}
|
}
|
||||||
|
|
||||||
public static <ID_TYPE> int unsetDelete(final Class<?> clazz, final ID_TYPE id, final QueryOption... option)
|
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);
|
final QueryOptions options = new QueryOptions(option);
|
||||||
options.add(new Condition(getTableIdCondition(clazz, id)));
|
options.add(new Condition(getTableIdCondition(clazz, id)));
|
||||||
return unsetDeleteWhere(clazz, options.getAllArray());
|
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 QueryOptions options = new QueryOptions(option);
|
||||||
final Condition condition = conditionFusionOrEmpty(options, true);
|
final Condition condition = conditionFusionOrEmpty(options, true);
|
||||||
final String tableName = AnnotationTools.getTableName(clazz, options);
|
final String tableName = AnnotationTools.getTableName(clazz, options);
|
||||||
@ -1691,7 +1695,12 @@ public class DataAccess {
|
|||||||
if (deletedFieldName == null) {
|
if (deletedFieldName == null) {
|
||||||
throw new DataAccessException("The class " + clazz.getCanonicalName() + " has no deleted field");
|
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();
|
final StringBuilder query = new StringBuilder();
|
||||||
query.append("UPDATE `");
|
query.append("UPDATE `");
|
||||||
query.append(tableName);
|
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.
|
// need to disable the deleted false because the model must be unselected to be updated.
|
||||||
options.add(QueryOptions.ACCESS_DELETED_ITEMS);
|
options.add(QueryOptions.ACCESS_DELETED_ITEMS);
|
||||||
condition.whereAppendQuery(query, tableName, options, deletedFieldName);
|
condition.whereAppendQuery(query, tableName, options, deletedFieldName);
|
||||||
try {
|
try (final PreparedStatement ps = entry.connection.prepareStatement(query.toString())) {
|
||||||
final PreparedStatement ps = entry.connection.prepareStatement(query.toString());
|
|
||||||
final CountInOut iii = new CountInOut(1);
|
final CountInOut iii = new CountInOut(1);
|
||||||
condition.injectQuery(ps, iii);
|
condition.injectQuery(ps, iii);
|
||||||
return ps.executeUpdate();
|
return ps.executeUpdate();
|
||||||
} finally {
|
} catch (final SQLException ex) {
|
||||||
entry.close();
|
throw new DataAccessException("Catch SQL error:" + ex.getMessage());
|
||||||
|
} catch (final Exception ex) {
|
||||||
|
throw new DataAccessException("Fail to excute the SQL query:" + ex.getMessage());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user