Explorar o código

reset before preWait and after waitResponse

sunyj %!s(int64=7) %!d(string=hai) anos
pai
achega
34c1d2e505
Modificáronse 1 ficheiros con 39 adicións e 23 borrados
  1. 39 23
      src/main/java/com/uas/ps/product/sync/WaitSyncHelper.java

+ 39 - 23
src/main/java/com/uas/ps/product/sync/WaitSyncHelper.java

@@ -45,6 +45,7 @@ public class WaitSyncHelper {
      */
     public void preWait(String sourceApp) {
         Assert.notEmpty(sourceApp, "sourceApp is empty");
+        reset();
         String batchCode = initBatchCode();
         syncRepository.setSessionVariable(sourceApp, batchCode);
     }
@@ -59,6 +60,7 @@ public class WaitSyncHelper {
     public void preWait(String sourceApp, JdbcTemplate jdbcTemplate) {
         Assert.notEmpty(sourceApp, "sourceApp is empty");
         Assert.notNull(jdbcTemplate, "jdbcTemplate is null");
+        reset();
         String batchCode = initBatchCode();
         jdbcTemplates.set(jdbcTemplate);
         jdbcTemplate.update("call sync$set_session_variable('" + sourceApp + "','" + batchCode + "');");
@@ -76,31 +78,31 @@ public class WaitSyncHelper {
     public void waitResponse() {
         String batchCode = batchCodes.get();
         Assert.notEmpty(batchCode, "batchCode is empty, please call preWait() first");
-        batchCodes.remove();
 
-        // 根据修改方式是 jdbc 还是 jpa,采用不同的方式获取 batchSize
-        JdbcTemplate jdbcTemplate = jdbcTemplates.get();
         Integer batchSize;
-        if (jdbcTemplate == null) {
-            batchSize = syncRepository.getBatchSize();
-            syncRepository.unsetSessionVariable();
-        } else {
-            jdbcTemplates.remove();
-            batchSize = jdbcTemplate.execute(new CallableStatementCreator() {
-                @Override
-                public CallableStatement createCallableStatement(Connection connection) throws SQLException {
-                    CallableStatement callableStatement = connection.prepareCall("{call sync$get_batch_size(?)}");
-                    callableStatement.registerOutParameter(1, Types.INTEGER);
-                    return callableStatement;
-                }
-            }, new CallableStatementCallback<Integer>() {
-                @Override
-                public Integer doInCallableStatement(CallableStatement callableStatement) throws SQLException, DataAccessException {
-                    callableStatement.execute();
-                    return callableStatement.getInt(1);
-                }
-            });
-            jdbcTemplate.execute("call sync$unset_session_variable();");
+        try {
+            // 根据修改方式是 jdbc 还是 jpa,采用不同的方式获取 batchSize
+            JdbcTemplate jdbcTemplate = jdbcTemplates.get();
+            if (jdbcTemplate == null) {
+                batchSize = syncRepository.getBatchSize();
+            } else {
+                batchSize = jdbcTemplate.execute(new CallableStatementCreator() {
+                    @Override
+                    public CallableStatement createCallableStatement(Connection connection) throws SQLException {
+                        CallableStatement callableStatement = connection.prepareCall("{call sync$get_batch_size(?)}");
+                        callableStatement.registerOutParameter(1, Types.INTEGER);
+                        return callableStatement;
+                    }
+                }, new CallableStatementCallback<Integer>() {
+                    @Override
+                    public Integer doInCallableStatement(CallableStatement callableStatement) throws SQLException, DataAccessException {
+                        callableStatement.execute();
+                        return callableStatement.getInt(1);
+                    }
+                });
+            }
+        } finally {
+            reset();
         }
 
         logger.info("batchCode: " + batchCode + ", batchSize: " + batchSize);
@@ -110,4 +112,18 @@ public class WaitSyncHelper {
             messageConsumer.waitResponse(batchCode);
         }
     }
+
+    /**
+     * 重置,清除旧的 session 变量等
+     */
+    private void reset() {
+        batchCodes.remove();
+        JdbcTemplate jdbcTemplate = jdbcTemplates.get();
+        if (jdbcTemplate == null) {
+            syncRepository.unsetSessionVariable();
+        } else {
+            jdbcTemplates.remove();
+            jdbcTemplate.execute("call sync$unset_session_variable();");
+        }
+    }
 }