浏览代码

优化动态数据源

zhouhao 7 年之前
父节点
当前提交
e848821831

+ 31 - 15
hsweb-datasource/hsweb-datasource-api/src/main/java/org/hswebframework/web/datasource/DynamicDataSourceAutoConfiguration.java

@@ -1,11 +1,14 @@
 package org.hswebframework.web.datasource;
 
 import org.hswebframework.ezorm.rdb.executor.SqlExecutor;
+import org.hswebframework.web.datasource.service.InMemoryDynamicDataSourceService;
 import org.hswebframework.web.datasource.starter.AopDataSourceSwitcherAutoConfiguration;
 import org.hswebframework.web.datasource.switcher.DataSourceSwitcher;
 import org.springframework.beans.BeansException;
+import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.beans.factory.config.BeanPostProcessor;
 import org.springframework.boot.autoconfigure.ImportAutoConfiguration;
+import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
 import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
 import org.springframework.context.annotation.Bean;
 import org.springframework.context.annotation.Configuration;
@@ -13,8 +16,6 @@ import org.springframework.context.annotation.Configuration;
 import javax.sql.DataSource;
 
 /**
- * TODO 完成注释
- *
  * @author zhouhao
  */
 @Configuration
@@ -29,20 +30,9 @@ public class DynamicDataSourceAutoConfiguration implements BeanPostProcessor {
 
     @Bean
     @ConditionalOnMissingBean(DynamicDataSourceService.class)
-    public DynamicDataSourceService justSupportDefaultDataSourceService(DataSource dataSource) {
+    public InMemoryDynamicDataSourceService inMemoryDynamicDataSourceService(DataSource dataSource) {
         DynamicDataSourceProxy dataSourceProxy = new DynamicDataSourceProxy(null, dataSource);
-        return new DynamicDataSourceService() {
-            @Override
-            public DynamicDataSource getDataSource(String dataSourceId) {
-                throw new UnsupportedOperationException("dynamic datasource not enable");
-            }
-
-            @Override
-            public DynamicDataSource getDefaultDataSource() {
-                return dataSourceProxy;
-            }
-        };
-
+        return new InMemoryDynamicDataSourceService(dataSourceProxy);
     }
 
     @Override
@@ -50,6 +40,32 @@ public class DynamicDataSourceAutoConfiguration implements BeanPostProcessor {
         return bean;
     }
 
+    @ConditionalOnBean(InMemoryDynamicDataSourceService.class)
+    @Configuration
+    public static class AutoRegisterDataSource implements BeanPostProcessor {
+
+        private InMemoryDynamicDataSourceService dataSourceService;
+
+        @Autowired
+        public void setDataSourceService(InMemoryDynamicDataSourceService dataSourceService) {
+            DataSourceHolder.dynamicDataSourceService = dataSourceService;
+            this.dataSourceService = dataSourceService;
+        }
+
+        @Override
+        public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
+            return bean;
+        }
+
+        @Override
+        public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
+            if (bean instanceof DataSource) {
+                dataSourceService.registerDataSource(beanName, ((DataSource) bean));
+            }
+            return bean;
+        }
+    }
+
     @Override
     public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
         if (bean instanceof DynamicDataSourceService) {

+ 44 - 0
hsweb-datasource/hsweb-datasource-api/src/main/java/org/hswebframework/web/datasource/service/InMemoryDynamicDataSourceService.java

@@ -0,0 +1,44 @@
+package org.hswebframework.web.datasource.service;
+
+import org.hswebframework.web.datasource.DynamicDataSource;
+import org.hswebframework.web.datasource.DynamicDataSourceProxy;
+import org.hswebframework.web.datasource.exception.DataSourceNotFoundException;
+
+import javax.sql.DataSource;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.concurrent.CountDownLatch;
+
+/**
+ * @author zhouhao
+ * @since 1.0
+ */
+public class InMemoryDynamicDataSourceService extends AbstractDynamicDataSourceService {
+    public InMemoryDynamicDataSourceService(DynamicDataSource defaultDataSource) {
+        super(defaultDataSource);
+    }
+
+    private Map<String, DataSourceCache> dataSourceMap = new HashMap<>();
+
+    public void registerDataSource(String id, DataSource dataSource) {
+        CountDownLatch countDownLatch = new CountDownLatch(1);
+        dataSourceMap.put(id, new DataSourceCache(0L
+                , new DynamicDataSourceProxy(id, dataSource), countDownLatch));
+        countDownLatch.countDown();
+    }
+
+    @Override
+    protected int getHash(String id) {
+        return 0;
+    }
+
+    @Override
+    protected DataSourceCache createCache(String id) {
+        DataSourceCache cache = dataSourceMap.get(id);
+        if (cache == null) {
+            throw new DataSourceNotFoundException(id);
+        }
+        return cache;
+    }
+
+}

+ 5 - 2
hsweb-system/hsweb-system-oauth2-server/hsweb-system-oauth2-server-simple/src/main/java/org/hswebframework/web/service/oauth2/server/simple/SimpleAccessTokenService.java

@@ -27,11 +27,11 @@ import org.hswebframework.web.dao.oauth2.OAuth2AccessDao;
 import org.hswebframework.web.id.IDGenerator;
 import org.hswebframework.web.service.DefaultDSLQueryService;
 import org.hswebframework.web.service.DefaultDSLUpdateService;
+import org.springframework.transaction.annotation.Propagation;
+import org.springframework.transaction.annotation.Transactional;
 import org.springframework.util.Assert;
 
 /**
- * TODO 完成注释
- *
  * @author zhouhao
  */
 public class SimpleAccessTokenService implements AccessTokenService {
@@ -64,6 +64,7 @@ public class SimpleAccessTokenService implements AccessTokenService {
     }
 
     @Override
+    @Transactional(propagation = Propagation.NOT_SUPPORTED)
     public OAuth2AccessToken getTokenByRefreshToken(String refreshToken) {
         Assert.notNull(refreshToken, "refreshToken can not be null!");
         return DefaultDSLQueryService.createQuery(oAuth2AccessDao)
@@ -71,6 +72,7 @@ public class SimpleAccessTokenService implements AccessTokenService {
     }
 
     @Override
+    @Transactional(propagation = Propagation.NOT_SUPPORTED)
     public OAuth2AccessToken getTokenByAccessToken(String accessToken) {
         Assert.notNull(accessToken, "accessToken can not be null!");
         return DefaultDSLQueryService.createQuery(oAuth2AccessDao)
@@ -78,6 +80,7 @@ public class SimpleAccessTokenService implements AccessTokenService {
     }
 
     @Override
+    @Transactional(propagation = Propagation.NOT_SUPPORTED)
     public OAuth2AccessToken saveOrUpdateToken(OAuth2AccessToken token) {
         Assert.notNull(token, "token can not be null!");
         int total = DefaultDSLQueryService

+ 3 - 0
hsweb-system/hsweb-system-oauth2-server/hsweb-system-oauth2-server-simple/src/main/java/org/hswebframework/web/service/oauth2/server/simple/SimpleAuthorizationCodeService.java

@@ -27,6 +27,8 @@ import org.hswebframework.web.dao.oauth2.AuthorizationCodeDao;
 import org.hswebframework.web.id.IDGenerator;
 import org.hswebframework.web.service.DefaultDSLDeleteService;
 import org.hswebframework.web.service.DefaultDSLQueryService;
+import org.springframework.transaction.annotation.Propagation;
+import org.springframework.transaction.annotation.Transactional;
 
 
 /**
@@ -65,6 +67,7 @@ public class SimpleAuthorizationCodeService implements AuthorizationCodeService
     }
 
     @Override
+    @Transactional(propagation = Propagation.NOT_SUPPORTED)
     public AuthorizationCode consumeAuthorizationCode(String code) {
         AuthorizationCodeEntity codeEntity = DefaultDSLQueryService
                 .createQuery(authorizationCodeDao)