package cn.ac.iie.dao; import com.alibaba.druid.pool.DruidDataSource; import com.alibaba.druid.pool.DruidPooledConnection; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.SQLException; import java.util.Properties; /** * Druid连接信息 * * @author antlee * @date 2018/8/20 */ public class DbConnect { private static DruidDataSource dataSource = null; private static DbConnect dbConnect = null; private static Properties props = new Properties(); static { getDbConnect(); } private static void getDbConnect() { try { if (dataSource == null) { dataSource = new DruidDataSource(); props.load(DbConnect.class.getClassLoader().getResourceAsStream("clickhouse.properties")); //设置连接参数 dataSource.setUrl("jdbc:clickhouse://" + props.getProperty("db.id")); dataSource.setDriverClassName(props.getProperty("drivers")); dataSource.setUsername(props.getProperty("mdb.user")); dataSource.setPassword(props.getProperty("mdb.password")); //配置初始化大小、最小、最大 dataSource.setInitialSize(Integer.parseInt(props.getProperty("initialsize"))); dataSource.setMinIdle(Integer.parseInt(props.getProperty("minidle"))); dataSource.setMaxActive(Integer.parseInt(props.getProperty("maxactive"))); //连接泄漏监测 dataSource.setRemoveAbandoned(true); dataSource.setRemoveAbandonedTimeout(30); dataSource.setDefaultAutoCommit(false); //配置获取连接等待超时的时间 dataSource.setMaxWait(30000); //配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 dataSource.setTimeBetweenEvictionRunsMillis(2000); //防止过期 dataSource.setValidationQuery("SELECT 1"); dataSource.setTestWhileIdle(true); dataSource.setTestOnBorrow(true); dataSource.setKeepAlive(true); } } catch (Exception e) { e.printStackTrace(); } } /** * 数据库连接池单例 * * @return dbConnect */ public static synchronized DbConnect getInstance() { if (null == dbConnect) { dbConnect = new DbConnect(); } return dbConnect; } /** * 返回druid数据库连接 * * @return 连接 * @throws SQLException sql异常 */ public DruidPooledConnection getConnection() throws SQLException { return dataSource.getConnection(); } /** * 清空PreparedStatement、Connection对象,未定义的置空。 * * @param pstmt PreparedStatement对象 * @param connection Connection对象 */ public void clear(PreparedStatement pstmt, Connection connection) { try { if (pstmt != null) { pstmt.close(); } if (connection != null) { connection.close(); } } catch (SQLException e) { e.printStackTrace(); } } }