]> git.basschouten.com Git - openhab-addons.git/commitdiff
Do not modify index or create tables when uninitialized (#13728)
authorJacob Laursen <jacob-github@vindvejr.dk>
Thu, 17 Nov 2022 19:00:31 +0000 (20:00 +0100)
committerGitHub <noreply@github.com>
Thu, 17 Nov 2022 19:00:31 +0000 (20:00 +0100)
Fixes #13727

Signed-off-by: Jacob Laursen <jacob-github@vindvejr.dk>
bundles/org.openhab.persistence.jdbc/src/main/java/org/openhab/persistence/jdbc/internal/JdbcMapper.java
bundles/org.openhab.persistence.jdbc/src/main/java/org/openhab/persistence/jdbc/internal/JdbcPersistenceService.java

index 2a6ba22aa9e36316bc65d155f6945dc3ed4096cb..e22e69d647ee61eef81ddd728d09bf0e34767922 100644 (file)
@@ -34,6 +34,7 @@ import org.openhab.core.types.State;
 import org.openhab.persistence.jdbc.internal.dto.ItemVO;
 import org.openhab.persistence.jdbc.internal.dto.ItemsVO;
 import org.openhab.persistence.jdbc.internal.dto.JdbcPersistenceItemInfo;
+import org.openhab.persistence.jdbc.internal.exceptions.JdbcException;
 import org.openhab.persistence.jdbc.internal.exceptions.JdbcSQLException;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -183,7 +184,7 @@ public class JdbcMapper {
         return vo;
     }
 
-    protected void storeItemValue(Item item, State itemState, @Nullable ZonedDateTime date) throws JdbcSQLException {
+    protected void storeItemValue(Item item, State itemState, @Nullable ZonedDateTime date) throws JdbcException {
         logger.debug("JDBC::storeItemValue: item={} state={} date={}", item, itemState, date);
         String tableName = getTable(item);
         long timerStart = System.currentTimeMillis();
@@ -316,20 +317,23 @@ public class JdbcMapper {
         }
     }
 
-    protected String getTable(Item item) throws JdbcSQLException {
-        int itemId = 0;
-        ItemsVO isvo;
-        ItemVO ivo;
-
+    protected String getTable(Item item) throws JdbcException {
         String itemName = item.getName();
-        String tableName = itemNameToTableNameMap.get(itemName);
+        if (!initialized) {
+            throw new JdbcException("Not initialized, unable to find table for item " + itemName);
+        }
 
         // Table already exists - return the name
+        String tableName = itemNameToTableNameMap.get(itemName);
         if (!Objects.isNull(tableName)) {
             return tableName;
         }
 
-        logger.debug("JDBC::getTable: no table found for item '{}' in sqlTables", itemName);
+        logger.debug("JDBC::getTable: no table found for item '{}' in itemNameToTableNameMap", itemName);
+
+        int itemId = 0;
+        ItemsVO isvo;
+        ItemVO ivo;
 
         if (!conf.getTableUseRealCaseSensitiveItemNames()) {
             // Create a new entry in items table
index 3e4783f32b9f8845c1b715ce371ae66b21f60541..a75f6d2c8af22510eaf70ef89d156dc14f621b28 100644 (file)
@@ -41,6 +41,7 @@ import org.openhab.core.persistence.strategy.PersistenceStrategy;
 import org.openhab.core.types.State;
 import org.openhab.core.types.UnDefType;
 import org.openhab.persistence.jdbc.internal.dto.ItemsVO;
+import org.openhab.persistence.jdbc.internal.exceptions.JdbcException;
 import org.openhab.persistence.jdbc.internal.exceptions.JdbcSQLException;
 import org.osgi.framework.BundleContext;
 import org.osgi.framework.Constants;
@@ -161,7 +162,7 @@ public class JdbcPersistenceService extends JdbcMapper implements ModifiablePers
                 logger.debug("JDBC: Stored item '{}' as '{}' in SQL database at {} in {} ms.", item.getName(), state,
                         new Date(), System.currentTimeMillis() - timerStart);
             }
-        } catch (JdbcSQLException e) {
+        } catch (JdbcException e) {
             logger.warn("JDBC::store: Unable to store item", e);
         }
     }