|
@@ -0,0 +1,254 @@
|
|
|
+package cn.com.qmth.examcloud.bridge.commons;
|
|
|
+
|
|
|
+import cn.com.qmth.examcloud.commons.exception.ExamCloudRuntimeException;
|
|
|
+import cn.com.qmth.examcloud.commons.util.PropertiesUtil;
|
|
|
+import org.slf4j.Logger;
|
|
|
+import org.slf4j.LoggerFactory;
|
|
|
+
|
|
|
+import javax.sql.DataSource;
|
|
|
+import java.sql.Connection;
|
|
|
+import java.sql.ResultSet;
|
|
|
+import java.sql.SQLException;
|
|
|
+import java.sql.Statement;
|
|
|
+import java.util.Map;
|
|
|
+import java.util.concurrent.ConcurrentHashMap;
|
|
|
+
|
|
|
+/**
|
|
|
+ * 类注释
|
|
|
+ *
|
|
|
+ * @author WANGWEI
|
|
|
+ */
|
|
|
+public class DBUtil {
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 属性注释
|
|
|
+ */
|
|
|
+ private static final Logger LOG = LoggerFactory.getLogger(DBUtil.class);
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 属性注释
|
|
|
+ */
|
|
|
+ private static final Object LOCK = new Object();
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 属性注释
|
|
|
+ */
|
|
|
+ private static Map<String, DataSource> dataSources = new ConcurrentHashMap<String, DataSource>();
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 初始化数据源(C3P0)
|
|
|
+ *
|
|
|
+ * @author WANGWEI
|
|
|
+ * @param dataSourceName
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static DataSource initDataSource(final String dataSourceName) {
|
|
|
+ Class<?> c = null;
|
|
|
+ try {
|
|
|
+ c = Class.forName("com.alibaba.druid.pool.DruidDataSource");
|
|
|
+ } catch (ClassNotFoundException e) {
|
|
|
+ // ignore
|
|
|
+ }
|
|
|
+
|
|
|
+ if (null == c) {
|
|
|
+ try {
|
|
|
+ c = Class.forName("com.mchange.v2.c3p0.ComboPooledDataSource");
|
|
|
+ } catch (ClassNotFoundException e) {
|
|
|
+ // ignore
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ if (null == c) {
|
|
|
+ throw new ExamCloudRuntimeException("no class about dataSource implementation.");
|
|
|
+ }
|
|
|
+
|
|
|
+ @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", 1);
|
|
|
+ int minPoolSize = PropertiesUtil.getInt(dataSourceName + ".minPoolSize", 1);
|
|
|
+ int maxPoolSize = PropertiesUtil.getInt(dataSourceName + ".maxPoolSize", 10);
|
|
|
+
|
|
|
+ DataSource dataSource = null;
|
|
|
+ String simpleName = implementation.getSimpleName();
|
|
|
+ try {
|
|
|
+ // C3P0
|
|
|
+ if (simpleName.equals("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", int.class).invoke(c3po,
|
|
|
+ initialPoolSize);
|
|
|
+ implementation.getMethod("setMinPoolSize", int.class).invoke(c3po, minPoolSize);
|
|
|
+ implementation.getMethod("setMaxPoolSize", int.class).invoke(c3po, maxPoolSize);
|
|
|
+ implementation.getMethod("setPreferredTestQuery", String.class).invoke(c3po,
|
|
|
+ "select 1 from dual");
|
|
|
+ }
|
|
|
+ // druid
|
|
|
+ else if (simpleName.equals("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", int.class).invoke(druid,
|
|
|
+ initialPoolSize);
|
|
|
+ implementation.getMethod("setMaxActive", int.class).invoke(druid, maxPoolSize);
|
|
|
+
|
|
|
+ implementation.getMethod("setValidationQuery", String.class).invoke(druid,
|
|
|
+ "select 1 from dual");
|
|
|
+ }
|
|
|
+
|
|
|
+ } catch (Exception e) {
|
|
|
+ LOG.error("[JDBC] Fail to init dataSource. dataSourceName=" + dataSourceName, e);
|
|
|
+ throw new RuntimeException(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);
|
|
|
+ }
|
|
|
+
|
|
|
+ return dataSource;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 方法注释
|
|
|
+ *
|
|
|
+ * @author WANGWEI
|
|
|
+ * @param dataSourceName
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static DataSource getDataSource(String dataSourceName) {
|
|
|
+ DataSource dataSource = dataSources.get(dataSourceName);
|
|
|
+ if (null == dataSource) {
|
|
|
+ synchronized (LOCK) {
|
|
|
+ dataSource = dataSources.get(dataSourceName);
|
|
|
+ if (null == dataSource) {
|
|
|
+ dataSource = initDataSource(dataSourceName);
|
|
|
+ dataSources.put(dataSourceName, dataSource);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return dataSource;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 方法注释
|
|
|
+ *
|
|
|
+ * @author WANGWEI
|
|
|
+ * @param dataSourceName
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static Connection getConnection(String dataSourceName) {
|
|
|
+ Connection conn = null;
|
|
|
+ try {
|
|
|
+ conn = getDataSource(dataSourceName).getConnection();
|
|
|
+ } catch (SQLException e) {
|
|
|
+ LOG.error("[JDBC] Fail to get connection.", e);
|
|
|
+ }
|
|
|
+
|
|
|
+ return conn;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @param conn
|
|
|
+ * @param stmt
|
|
|
+ * @param res
|
|
|
+ */
|
|
|
+ public static void close(Connection conn, Statement statement, ResultSet rs) {
|
|
|
+ close(rs);
|
|
|
+ close(conn, statement);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @param conn
|
|
|
+ * @param statement
|
|
|
+ */
|
|
|
+ public static void close(Connection conn, Statement statement) {
|
|
|
+ close(statement);
|
|
|
+ close(conn);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @param stmt
|
|
|
+ * @param res
|
|
|
+ */
|
|
|
+ public static void close(Statement statement, ResultSet rs) {
|
|
|
+ close(statement);
|
|
|
+ close(rs);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @param conn
|
|
|
+ */
|
|
|
+ public static void close(Connection conn) {
|
|
|
+ if (conn != null) {
|
|
|
+ try {
|
|
|
+ conn.close();
|
|
|
+ } catch (SQLException e) {
|
|
|
+ LOG.error("[JDBC] Fail to close Connection.", e);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @param statement
|
|
|
+ */
|
|
|
+ public static void close(Statement statement) {
|
|
|
+ if (statement != null) {
|
|
|
+ try {
|
|
|
+ statement.close();
|
|
|
+ } catch (SQLException e) {
|
|
|
+ LOG.error("[JDBC] Fail to close Statement.", e);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @param rs
|
|
|
+ */
|
|
|
+ public static void close(ResultSet rs) {
|
|
|
+ if (rs != null) {
|
|
|
+ try {
|
|
|
+ rs.close();
|
|
|
+ } catch (SQLException e) {
|
|
|
+ LOG.error("[JDBC] Fail to close ResultSet.", e);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+}
|