Browse Source

。。。。。

wangwei 7 years ago
parent
commit
68a8f25ec2

+ 0 - 61
examcloud-task-starter/src/main/java/cn/com/qmth/examcloud/task/starter/config/DataSourcesHolder.java

@@ -1,61 +0,0 @@
-package cn.com.qmth.examcloud.task.starter.config;
-
-import javax.sql.DataSource;
-
-import cn.com.qmth.examcloud.commons.base.util.DBUtil;
-
-/**
- * 数据源
- *
- * @author WANGWEI
- * @date 2018年8月3日
- * @Copyright (c) 2018-? http://qmth.com.cn All Rights Reserved.
- */
-public class DataSourcesHolder {
-
-	public enum DataSourceName {
-		/**
-		 * 基础信息
-		 */
-		CORE_BASIC("$ds.core.basic"),
-		/**
-		 * 考务
-		 */
-		CORE_EXAMWORK("$ds.core.examwork"),
-		/**
-		 * 题库
-		 */
-		CORE_QUESTION("$ds.core.question"),
-		/**
-		 * 阅卷
-		 */
-		CORE_MARKING("$ds.core.marking"),
-		/**
-		 * 网考
-		 */
-		CORE_OE("$ds.core.oe");
-
-		private String configName;
-
-		private DataSourceName(String configName) {
-			this.configName = configName;
-		}
-
-		public String getConfigName() {
-			return configName;
-		}
-
-	}
-
-	/**
-	 * 获取数据源
-	 *
-	 * @author WANGWEI
-	 * @param dataSourceName
-	 * @return
-	 */
-	public static DataSource getDataSource(DataSourceName dataSourceName) {
-		return DBUtil.getDataSource(dataSourceName.getConfigName());
-	}
-
-}

+ 95 - 0
examcloud-task-starter/src/main/java/cn/com/qmth/examcloud/task/starter/config/JdbcTemplateHolder.java

@@ -0,0 +1,95 @@
+package cn.com.qmth.examcloud.task.starter.config;
+
+import java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
+
+import javax.sql.DataSource;
+
+import org.springframework.jdbc.core.JdbcTemplate;
+
+import cn.com.qmth.examcloud.commons.base.util.DBUtil;
+
+/**
+ * JDBC模板
+ *
+ * @author WANGWEI
+ * @date 2018年8月3日
+ * @Copyright (c) 2018-? http://qmth.com.cn All Rights Reserved.
+ */
+public class JdbcTemplateHolder {
+
+	private static Map<String, JdbcTemplate> jdbcTemplates = new ConcurrentHashMap<String, JdbcTemplate>();
+
+	private static final Object LOCK = new Object();
+
+	public enum DataSourceName {
+		/**
+		 * 基础信息
+		 */
+		CORE_BASIC("$ds.core.basic"),
+		/**
+		 * 考务
+		 */
+		CORE_EXAMWORK("$ds.core.examwork"),
+		/**
+		 * 题库
+		 */
+		CORE_QUESTION("$ds.core.question"),
+		/**
+		 * 阅卷
+		 */
+		CORE_MARKING("$ds.core.marking"),
+		/**
+		 * 网考
+		 */
+		CORE_OE("$ds.core.oe");
+
+		private String configName;
+
+		private DataSourceName(String configName) {
+			this.configName = configName;
+		}
+
+		public String getConfigName() {
+			return configName;
+		}
+
+	}
+
+	/**
+	 * 启动初始化
+	 *
+	 * @author WANGWEI
+	 */
+	public static void init() {
+		// getJdbcTemplate(DataSourceName.CORE_BASIC);
+		// getJdbcTemplate(DataSourceName.CORE_EXAMWORK);
+		// getJdbcTemplate(DataSourceName.CORE_MARKING);
+		// getJdbcTemplate(DataSourceName.CORE_OE);
+		// getJdbcTemplate(DataSourceName.CORE_QUESTION);
+	}
+
+	/**
+	 * 获取 JdbcTemplate
+	 *
+	 * @author WANGWEI
+	 * @param dataSourceName
+	 * @return
+	 */
+	public static JdbcTemplate getJdbcTemplate(DataSourceName dataSourceName) {
+		JdbcTemplate jdbcTemplate = jdbcTemplates.get(dataSourceName.name());
+		if (null == jdbcTemplate) {
+			synchronized (LOCK) {
+				jdbcTemplate = jdbcTemplates.get(dataSourceName.name());
+				if (null == jdbcTemplate) {
+					DataSource dataSource = DBUtil.getDataSource(dataSourceName.getConfigName());
+					jdbcTemplate = new JdbcTemplate(dataSource);
+					jdbcTemplates.put(dataSourceName.name(), jdbcTemplate);
+				}
+			}
+		}
+
+		return jdbcTemplate;
+	}
+
+}