Ver Fonte

1、解决时序数据库插入不存在表报错的问题;
2、解决远程调用查询时序数据库sql报错抛异常的调用异常的问题;

james há 20 horas atrás
pai
commit
02de973bf7

+ 22 - 6
service-tsdb/service-tsdb-biz/src/main/java/com/usky/demo/service/impl/QueryTdengineDataServiceImpl.java

@@ -63,10 +63,10 @@ public class QueryTdengineDataServiceImpl extends AbstractCrudService<QueryTdeng
 
         // 遍历设备表集合,自动忽略表中不存在的字段,用有效字段构建查询,避免错误
         for (int i = 0; i < deviceUUIds.size(); i++) {
-            try (Connection connection = dataSource.getConnection()) {
 
-                String tablename = "_"+deviceUUIds.get(i);
+            String tablename = "_"+deviceUUIds.get(i);
 
+            try (Connection connection = dataSource.getConnection()) {
                 String sql = "describe " + tablename;
 
                 try (PreparedStatement itemStmt = connection.prepareStatement(sql)) {
@@ -123,6 +123,15 @@ public class QueryTdengineDataServiceImpl extends AbstractCrudService<QueryTdeng
                     }
                 }
 
+            } catch (SQLException e) {
+                // 判断TDengine专属错误码 0x2603 = 表不存在
+                if (e.getMessage().contains("0x2603") || e.getMessage().contains("Table does not exist")) {
+                    System.out.println("表" + tablename + "不存在,跳过");
+                    log.error("表" + tablename + "不存在,跳过");
+                } else {
+                    // 其他异常正常打印堆栈
+                    e.printStackTrace();
+                }
             } catch (Exception e) {
                 e.printStackTrace();
             }
@@ -141,10 +150,10 @@ public class QueryTdengineDataServiceImpl extends AbstractCrudService<QueryTdeng
 
         // 遍历设备表集合,自动忽略表中不存在的字段,用有效字段构建查询,避免错误
         for (int i = 0; i < deviceUUIds.size(); i++) {
-            try (Connection connection = dataSource.getConnection()) {
 
-                String tablename = "_"+deviceUUIds.get(i);
+            String tablename = "_"+deviceUUIds.get(i);
 
+            try (Connection connection = dataSource.getConnection()) {
                 String sql = "describe " + tablename;
 
                 try (PreparedStatement itemStmt = connection.prepareStatement(sql)) {
@@ -228,6 +237,15 @@ public class QueryTdengineDataServiceImpl extends AbstractCrudService<QueryTdeng
                     }
                 }
 
+            } catch (SQLException e) {
+                // 判断TDengine专属错误码 0x2603 = 表不存在
+                if (e.getMessage().contains("0x2603") || e.getMessage().contains("Table does not exist")) {
+                    System.out.println("表" + tablename + "不存在,跳过");
+                    log.error("表" + tablename + "不存在,跳过");
+                } else {
+                    // 其他异常正常打印堆栈
+                    e.printStackTrace();
+                }
             } catch (Exception e) {
                 e.printStackTrace();
             }
@@ -352,7 +370,6 @@ public class QueryTdengineDataServiceImpl extends AbstractCrudService<QueryTdeng
             }
         } catch (SQLException e) {
             log.error("能耗分项汇总查询失败, sql=" + sql + ", " + e.getMessage());
-            throw new BusinessException("能耗分项汇总查询失败: " + e.getMessage());
         }
         return result;
     }
@@ -405,7 +422,6 @@ public class QueryTdengineDataServiceImpl extends AbstractCrudService<QueryTdeng
             }
         } catch (SQLException e) {
             log.error("能耗分项趋势查询失败, sql=" + sql + ", " + e.getMessage());
-            throw new BusinessException("能耗分项趋势查询失败: " + e.getMessage());
         }
 
         result.setPoints(points);