소스 검색

修改源码支持mysql查询

liwh 1 개월 전
부모
커밋
314d495a40
5개의 변경된 파일75개의 추가작업 그리고 29개의 파일을 삭제
  1. 1 1
      config/jdbc-reload.properties
  2. 0 3
      config/socketPolicy.policy
  3. 3 3
      pom.xml
  4. 1 1
      src/main/assemblies/plugin.xml
  5. 70 21
      src/main/java/org/wltea/analyzer/dic/DatabaseMonitor.java

+ 1 - 1
config/jdbc-reload.properties

@@ -1,4 +1,4 @@
-jdbc.url=jdbc:mysql://10.168.1.47:18080/smart-search?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&useSSL=false&zeroDateTimeBehavior=convertToNull&serverTimezone=Asia/Shanghai
+jdbc.url=jdbc:mysql://8.130.72.63:18080/smart-search?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&useSSL=false&zeroDateTimeBehavior=convertToNull&serverTimezone=Asia/Shanghai
 jdbc.user=root
 jdbc.password=Taiji@2023#data
 jdbc.driver=com.mysql.cj.jdbc.Driver

+ 0 - 3
config/socketPolicy.policy

@@ -1,3 +0,0 @@
-grant {
-   permission java.net.SocketPermission "10.168.1.47:18080","connect,resolve";
-};

+ 3 - 3
pom.xml

@@ -84,9 +84,9 @@
         </dependency>
 
         <dependency>
-            <groupId>mysql</groupId>
-            <artifactId>mysql-connector-java</artifactId>
-            <version>8.0.21</version>
+            <groupId>com.mysql</groupId>
+            <artifactId>mysql-connector-j</artifactId>
+            <version>8.3.0</version>
         </dependency>
 
         <dependency>

+ 1 - 1
src/main/assemblies/plugin.xml

@@ -46,7 +46,7 @@
             <useProjectArtifact>true</useProjectArtifact>
             <useTransitiveFiltering>true</useTransitiveFiltering>
             <includes>
-                <include>mysql:mysql-connector-java</include>
+                <include>com.mysql:mysql-connector-j</include>
             </includes>
         </dependencySet>
     </dependencySets>

+ 70 - 21
src/main/java/org/wltea/analyzer/dic/DatabaseMonitor.java

@@ -18,7 +18,7 @@ public class DatabaseMonitor implements Runnable {
     private static final Logger logger = ESPluginLoggerFactory.getLogger(DatabaseMonitor.class.getName());
 
     private static final String JDBC_URL = "jdbc.url";
-    private static final String JDBC_USERNAME = "jdbc.username";
+    private static final String JDBC_USERNAME = "jdbc.user";
     private static final String JDBC_PASSWORD = "jdbc.password";
     private static final String JDBC_DRIVER = "jdbc.driver";
 
@@ -78,20 +78,17 @@ public class DatabaseMonitor implements Runnable {
 
         try {
 
-            if (connection == null || statement == null) {
+            connection = DriverManager.getConnection(
+                    dbProps.getProperty(JDBC_URL),
+                    dbProps.getProperty(JDBC_USERNAME),
+                    dbProps.getProperty(JDBC_PASSWORD)
+            );
 
-                connection = DriverManager.getConnection(
-                        dbProps.getProperty(JDBC_URL),
-                        dbProps.getProperty(JDBC_USERNAME),
-                        dbProps.getProperty(JDBC_PASSWORD)
-                );
+            statement = connection.createStatement();
 
-                statement = connection.createStatement();
-            }
-
-            logger.info("获取新词库上一次修改的时间"+lastModified);
+            logger.info("从mysql加载热更新词典上一次修改的时间:"+lastModified);
 
-            String sql = "select *,UNIX_TIMESTAMP(update_time) as version from sys_hot_word where UNIX_TIMESTAMP(update_time) >= "+lastModified;
+            String sql = "select *,UNIX_TIMESTAMP(update_time) as version from sys_hot_word where UNIX_TIMESTAMP(update_time) >"+lastModified;
 
             rs = statement.executeQuery(sql);
 
@@ -137,27 +134,50 @@ public class DatabaseMonitor implements Runnable {
                 }
             }
 
-            logger.info("获取新词库下一次修改的时间"+lastModified);
-
             if(stopWordList.size() > 0){
 
+                for(String word : stopWordList){
+
+                    logger.info("从数据库加载停用词条到主内存词典中->" + word);
+
+                }
+
                 Dictionary.getSingleton().addStopWords(stopWordList);
 
             }
 
             if(mainWordList.size() > 0){
+
+                for(String word : mainWordList){
+
+                    logger.info("从数据库加载新词条到主内存词典中->" + word);
+
+                }
+
                 Dictionary.getSingleton().addMainWords(mainWordList);
             }
 
             if(disStopWordList.size() > 0){
+
+                for(String word : disStopWordList){
+
+                    logger.info("从主内存词典中屏蔽停用词条->" + word);
+
+                }
                 Dictionary.getSingleton().disableStopWords(disStopWordList);
             }
 
             if(disMainWordList.size() > 0){
+
+                for(String word : disMainWordList){
+
+                    logger.info("从主内存词典中屏蔽词条->" + word);
+
+                }
                 Dictionary.getSingleton().disableMainWords(disMainWordList);
             }
 
-            logger.info("从mysql加载热更新词典成功!");
+            logger.info("从mysql加载热更新词典下一次修改的时间:"+lastModified);
 
         } catch (Exception e) {
 
@@ -165,13 +185,42 @@ public class DatabaseMonitor implements Runnable {
 
         } finally {
 
-            if (rs != null) {
-                try {
-                    rs.close();
-                } catch (SQLException e) {
-                    logger.error("error", e);
-                }
+            close(connection, statement,rs);
+        }
+    }
+
+    /**
+     *
+     * @param connection
+     * @param statement
+     * @param resultSet
+     * select 查询关闭发方法:关闭结果集,SQL执行对象,连接对象三个的资源
+     */
+    public void close(Connection connection, Statement statement, ResultSet resultSet){
+
+        if (resultSet != null){
+            try{
+                resultSet.close();
+            }catch (Exception ex){
+                ex.printStackTrace();
+            }
+        }
+
+        if (statement != null){
+            try{
+                statement.close();
+            }catch (Exception ex){
+                ex.printStackTrace();
             }
         }
+
+        if (connection != null){
+            try{
+                connection.close();
+            }catch (Exception ex){
+                ex.printStackTrace();
+            }
+        }
+
     }
 }