1.需要导入的包
com.alibaba
druid
1.1.12
2.具体程序实现
package com.zlkj.data.portals.utils;
import java.security.NoSuchAlgorithmException;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;
import org.hibernate.service.spi.ServiceException;
import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.stat.DruidDataSourceStatManager;
import com.zlkj.data.portals.request.dataSource.TestDataSourceRequest;
import lombok.extern.slf4j.Slf4j;
/**
-
连接池相关系统监测
-
@author admin
*/
@Slf4j
public class DataSourceTools {/**
- 获取当前连接池信息
- @param dsName
- @return
*/
public static DruidDataSource getDataSource(String dsName) {
DruidDataSource ds = null;
for (DruidDataSource datasource : DruidDataSourceStatManager.getDruidDataSourceInstances()) {
if (dsName.equals(datasource.getName())) {
ds = datasource;
break;
}
}
return ds;
}
/**
- 获取当前连接池信息
- @param dsName
- @return
*/
public static Map<String, Object> getDataSourceStat(String dsName) {
DruidDataSource ds = getDataSource(dsName);
return ds != null ? ds.getStatData() : new HashMap<String, Object>();
}
/**
-
创建数据库连接池
-
@param testDataSourceRequest
-
@return
-
@throws SQLException
-
@throws NoSuchAlgorithmException
*/
public static DruidDataSource createDataSource(TestDataSourceRequest testDataSourceRequest) throws SQLException, NoSuchAlgorithmException {
DruidDataSource druidDataSource = new DruidDataSource();//定义数据池中数据源名称
String dsName = testDataSourceRequest.getDataSourceType() + testDataSourceRequest.getAddress() + testDataSourceRequest.getPort() + testDataSourceRequest.getDataBaseName()
+ testDataSourceRequest.getUserName() + testDataSourceRequest.getUserPassword();
Connection conn = null;
try {
if (testDataSourceRequest.getDataSourceType().equals(“mysql”)) {
druidDataSource.setDriverClassName(“com.mysql.jdbc.Driver”);
}
String url = “jdbc:” + testDataSourceRequest.getDataSourceType() + “?/” + testDataSourceRequest.getAddress() + “:” + testDataSourceRequest.getPort() + “/” + testDataSourceRequest.getDataBaseName() + “?useUnicode=true&characterEncoding=UTF-8&useSSL=false&autoReconnect=true&failOverReadOnly=false”;
String username = testDataSourceRequest.getUserName();
String password = testDataSourceRequest.getUserPassword();
druidDataSource.setName(dsName);
druidDataSource.setUrl(url);
druidDataSource.setUsername(username);
druidDataSource.setPassword(password);druidDataSource.setInitialSize(5); druidDataSource.setMinIdle(5); druidDataSource.setMaxActive(5); druidDataSource.setMaxWait(6000); druidDataSource.setMinEvictableIdleTimeMillis(300000); druidDataSource.setRemoveAbandoned(false);//超过时间限制是否回收 druidDataSource.setRemoveAbandonedTimeout(180);//超过时间限制多长 druidDataSource.setTimeBetweenEvictionRunsMillis(60000);//配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 druidDataSource.setTestWhileIdle(true); druidDataSource.setTestOnBorrow(false); druidDataSource.setTestOnReturn(false); druidDataSource.setPoolPreparedStatements(true); druidDataSource.setMaxPoolPreparedStatementPerConnectionSize(20); druidDataSource.setTimeBetweenLogStatsMillis(3600000); druidDataSource.addConnectionProperty("remarksReporting", "true"); conn = druidDataSource.getConnection(username, password);
} catch (SQLException e) {
closeDataSource(dsName);
e.printStackTrace();
throw new ServiceException(“连接数据源失败 : {}” + e.getMessage());
} finally {
try {
if (conn != null) {
conn.close();
}
} catch (SQLException e) {
closeDataSource(dsName);
e.printStackTrace();
throw new ServiceException(“连接数据源关闭失败 : {}” + e.getMessage());
}
}
return druidDataSource;
}
/**
- 关闭数据库连接池
- @param dsName
*/
public static void closeDataSource(String dsName) {
if (getDataSource(dsName) != null) {
getDataSource(dsName).close();
}
}
}