wangwei 5 years ago
parent
commit
5c61a7bd4b
1 changed files with 88 additions and 37 deletions
  1. 88 37
      src/main/java/cn/com/qmth/examcloud/commons/util/DBUtil.java

+ 88 - 37
src/main/java/cn/com/qmth/examcloud/commons/util/DBUtil.java

@@ -11,7 +11,6 @@ import javax.sql.DataSource;
 
 import org.apache.commons.lang3.StringUtils;
 
-import com.mchange.v2.c3p0.ComboPooledDataSource;
 import com.mchange.v2.c3p0.DataSources;
 
 import cn.com.qmth.examcloud.commons.logging.ExamCloudLog;
@@ -40,54 +39,106 @@ public class DBUtil {
 	private static Map<String, DataSource> dataSources = new ConcurrentHashMap<String, DataSource>();
 
 	/**
-	 * 方法注释
+	 * 初始化数据源(C3P0)
 	 *
 	 * @author WANGWEI
 	 * @param dataSourceName
 	 * @return
 	 */
 	public static DataSource initDataSource(final String dataSourceName) {
-		final ComboPooledDataSource dataSource = new ComboPooledDataSource();
-		Connection conn = null;
+		Class<?> c;
 		try {
-			String dbDriver = PropertiesUtil.getString(dataSourceName + ".driver");
-			String url = PropertiesUtil.getString(dataSourceName + ".url");
-			String userName = PropertiesUtil.getString(dataSourceName + ".username");
-			String password = PropertiesUtil.getString(dataSourceName + ".password");
-			int minPoolSize = PropertiesUtil.getInt(dataSourceName + ".minPoolSize", 0);
-			int maxPoolSize = PropertiesUtil.getInt(dataSourceName + ".maxPoolSize", 10);
-
-			dataSource.setDriverClass(dbDriver);
-			dataSource.setJdbcUrl(url);
-			dataSource.setUser(userName);
-			dataSource.setPassword(password);
-			dataSource.setMinPoolSize(minPoolSize);
-			dataSource.setMaxPoolSize(maxPoolSize);
-			dataSource.setInitialPoolSize(minPoolSize);
-			dataSource.setPreferredTestQuery("select 1 from dual");
-			dataSource.setTestConnectionOnCheckin(true);
-			dataSource.setAcquireRetryAttempts(1);
-			dataSource.setAcquireRetryDelay(1000);
-			dataSource.setMaxIdleTime(60);
-			dataSource.setCheckoutTimeout(2000);
-			dataSource.setAcquireIncrement(1);
-			dataSource.setBreakAfterAcquireFailure(true);
+			c = Class.forName("com.mchange.v2.c3p0.ComboPooledDataSource");
+		} catch (ClassNotFoundException e) {
+			throw new RuntimeException(e);
+		}
 
-			conn = dataSource.getConnection();
+		@SuppressWarnings("unchecked")
+		Class<? extends DataSource> z = (Class<? extends DataSource>) c;
+
+		return initDataSource(z, dataSourceName);
+	}
+
+	/**
+	 * 初始化数据源
+	 *
+	 * @author WANGWEI
+	 * 
+	 * @param implementation
+	 *            数据源实现类
+	 * @param dataSourceName
+	 *            数据源名称
+	 * @return
+	 */
+	public static DataSource initDataSource(Class<? extends DataSource> implementation,
+			final String dataSourceName) {
+
+		String dbDriver = PropertiesUtil.getString(dataSourceName + ".driver");
+		String url = PropertiesUtil.getString(dataSourceName + ".url");
+		String userName = PropertiesUtil.getString(dataSourceName + ".username");
+		String password = PropertiesUtil.getString(dataSourceName + ".password");
+		int initialPoolSize = PropertiesUtil.getInt(dataSourceName + ".initialPoolSize", 2);
+		int minPoolSize = PropertiesUtil.getInt(dataSourceName + ".minPoolSize", 2);
+		int maxPoolSize = PropertiesUtil.getInt(dataSourceName + ".maxPoolSize", 10);
+
+		DataSource dataSource = null;
+		String simpleName = implementation.getSimpleName();
+		try {
+			// C3P0
+			if (simpleName.endsWith("ComboPooledDataSource")) {
+				DataSource c3po = implementation.newInstance();
+				dataSource = c3po;
+				implementation.getMethod("setDriverClass", String.class).invoke(c3po, dbDriver);
+				implementation.getMethod("setJdbcUrl", String.class).invoke(c3po, url);
+				implementation.getMethod("setUser", String.class).invoke(c3po, userName);
+				implementation.getMethod("setPassword", String.class).invoke(c3po, password);
+				implementation.getMethod("setInitialPoolSize", Integer.class).invoke(c3po,
+						initialPoolSize);
+				implementation.getMethod("setMinPoolSize", Integer.class).invoke(c3po, minPoolSize);
+				implementation.getMethod("setMaxPoolSize", Integer.class).invoke(c3po, maxPoolSize);
+				implementation.getMethod("setPreferredTestQuery", String.class).invoke(c3po,
+						"select 1 from dual");
+			}
+			// druid
+			else if (simpleName.endsWith("DruidDataSource")) {
+				DataSource druid = implementation.newInstance();
+				dataSource = druid;
+				implementation.getMethod("setDriverClassName", String.class).invoke(druid,
+						dbDriver);
+				implementation.getMethod("setUrl", String.class).invoke(druid, url);
+				implementation.getMethod("setUsername", String.class).invoke(druid, userName);
+				implementation.getMethod("setPassword", String.class).invoke(druid, password);
+				implementation.getMethod("setInitialSize", Integer.class).invoke(druid,
+						initialPoolSize);
+				implementation.getMethod("setMaxActive", Integer.class).invoke(druid, maxPoolSize);
+
+				implementation.getMethod("setValidationQuery", String.class).invoke(druid,
+						"select 1 from dual");
+			}
 
-			Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() {
-				public void run() {
-					try {
-						DataSources.destroy(dataSource);
-					} catch (SQLException e) {
-						LOG.error("[JDBC] Fail to destroy dataSource. dataSourceName="
-								+ dataSourceName, e);
-					}
-				}
-			}));
 		} catch (Exception e) {
 			LOG.error("[JDBC] Fail to init dataSource. dataSourceName=" + dataSourceName, e);
 			throw new RuntimeException(e);
+		}
+
+		final DataSource finalDataSource = dataSource;
+		Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() {
+			public void run() {
+				try {
+					DataSources.destroy(finalDataSource);
+				} catch (SQLException e) {
+					LOG.error("[JDBC] Fail to destroy dataSource. dataSourceName=" + dataSourceName,
+							e);
+				}
+			}
+		}));
+
+		Connection conn = null;
+		try {
+			conn = dataSource.getConnection();
+		} catch (Exception e) {
+			LOG.error("[JDBC] Fail to get connection. dataSourceName=" + dataSourceName, e);
+			throw new RuntimeException(e);
 		} finally {
 			close(conn);
 		}