Browse Source

切面捕获异常

yingp 8 years ago
parent
commit
4fda5f72f3

+ 7 - 0
src/main/java/com/uas/erp/database/datasource/DynamicDataSource.java

@@ -130,4 +130,11 @@ public class DynamicDataSource extends AbstractDataSource implements Initializin
             this.resolvedDataSources.put(lookupKey, dataSource);
             this.resolvedDataSources.put(lookupKey, dataSource);
         }
         }
     }
     }
+
+    public void removeDataSource(Object key) {
+        Object lookupKey = this.resolveSpecifiedLookupKey(key);
+        if (!containsDataSource(lookupKey)) {
+            this.resolvedDataSources.remove(key);
+        }
+    }
 }
 }

+ 42 - 10
src/main/java/com/uas/erp/database/datasource/DynamicDataSourceAspect.java

@@ -2,12 +2,16 @@ package com.uas.erp.database.datasource;
 
 
 import org.aspectj.lang.JoinPoint;
 import org.aspectj.lang.JoinPoint;
 import org.aspectj.lang.annotation.After;
 import org.aspectj.lang.annotation.After;
+import org.aspectj.lang.annotation.AfterThrowing;
 import org.aspectj.lang.annotation.Aspect;
 import org.aspectj.lang.annotation.Aspect;
 import org.aspectj.lang.annotation.Before;
 import org.aspectj.lang.annotation.Before;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.core.annotation.Order;
 import org.springframework.core.annotation.Order;
 import org.springframework.stereotype.Component;
 import org.springframework.stereotype.Component;
 
 
+import java.sql.SQLException;
+import java.sql.SQLRecoverableException;
+
 /**
 /**
  * Created by Pro1 on 2017/7/27.
  * Created by Pro1 on 2017/7/27.
  */
  */
@@ -21,23 +25,51 @@ public class DynamicDataSourceAspect {
 
 
     @Before("@annotation(dataSource)")
     @Before("@annotation(dataSource)")
     public void changeDataSource(JoinPoint point, TargetDataSource dataSource) throws Throwable {
     public void changeDataSource(JoinPoint point, TargetDataSource dataSource) throws Throwable {
+        Connectable connectable = getConnectableByPoint(point);
+        if (null != connectable) {
+            // 防止数据源未创建
+            dataSourceRegister.createDataSource(connectable);
+            DynamicDataSourceContextHolder.set(connectable);
+        }
+    }
+
+    @After("@annotation(dataSource)")
+    public void restoreDataSource(JoinPoint point, TargetDataSource dataSource) {
+        DynamicDataSourceContextHolder.clear();
+    }
+
+    @AfterThrowing(value = "@annotation(dataSource)", throwing = "e")
+    public void catchThrowing(JoinPoint point, TargetDataSource dataSource, Throwable e) {
+        if (e.getCause() instanceof  SQLException) {
+            Connectable connectable = getConnectableByPoint(point);
+            if (null != connectable) {
+                SQLException ex = (SQLException)e.getCause();
+                switch (ex.getErrorCode()){
+                    case 1017:
+                        // ORA-01017: invalid username/password
+                        dataSourceRegister.unregister(connectable);
+                        break;
+                    case 17002:
+                        // The Network Adapter could not establish the connection
+                        dataSourceRegister.unregister(connectable);
+                        break;
+                    default:
+                        break;
+                }
+            }
+        }
+    }
+
+    private Connectable getConnectableByPoint(JoinPoint point) {
         if (null != point.getArgs()) {
         if (null != point.getArgs()) {
             for (Object arg: point.getArgs()) {
             for (Object arg: point.getArgs()) {
                 // 按参数类型为Connectable的进行数据源切换
                 // 按参数类型为Connectable的进行数据源切换
                 if (null != arg && arg instanceof Connectable) {
                 if (null != arg && arg instanceof Connectable) {
-                    Connectable connectable = (Connectable) arg;
-                    // 防止数据源未创建
-                    dataSourceRegister.createDataSource(connectable);
-                    DynamicDataSourceContextHolder.set(connectable);
-                    break;
+                    return (Connectable) arg;
                 }
                 }
             }
             }
         }
         }
-    }
-
-    @After("@annotation(dataSource)")
-    public void restoreDataSource(JoinPoint point, TargetDataSource dataSource) {
-        DynamicDataSourceContextHolder.clear();
+        return null;
     }
     }
 
 
 }
 }

+ 4 - 0
src/main/java/com/uas/erp/database/datasource/DynamicDataSourceRegister.java

@@ -26,4 +26,8 @@ public class DynamicDataSourceRegister {
         }
         }
     }
     }
 
 
+    public void unregister(Connectable connectable) {
+        dynamicDataSource.removeDataSource(connectable.qualifier());
+    }
+
 }
 }

+ 1 - 1
src/main/resources/application.yml

@@ -39,7 +39,7 @@ spring:
   profiles: dev
   profiles: dev
 dba:
 dba:
   url: jdbc:oracle:thin:@192.168.253.12:1521:orcl
   url: jdbc:oracle:thin:@192.168.253.12:1521:orcl
-  username: UAS
+  username: UASS
   password: select!#%*(
   password: select!#%*(
 ---
 ---
 spring:
 spring: