text:(连接池链接 初始化)
parent
cb94836638
commit
bd6233a8c4
|
@ -0,0 +1,113 @@
|
||||||
|
package com.zx.domain;
|
||||||
|
|
||||||
|
import com.alibaba.druid.pool.DruidDataSource;
|
||||||
|
import lombok.extern.log4j.Log4j2;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
import java.sql.Connection;
|
||||||
|
import java.sql.SQLException;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @ClassDescription:
|
||||||
|
* @JdkVersion: 17
|
||||||
|
* @Author: zhangxu
|
||||||
|
* @Created: 2024/5/14 10:38
|
||||||
|
*/
|
||||||
|
@Component
|
||||||
|
@Log4j2
|
||||||
|
public class MyDataSource {
|
||||||
|
private static HashMap<String, DruidDataSource> pools=new HashMap<>();
|
||||||
|
/**
|
||||||
|
* 配置数据源信息,并初始化Druid连接池。
|
||||||
|
*
|
||||||
|
* @param dataSourceList 数据源列表,包含各个数据源的详细配置信息。
|
||||||
|
*/
|
||||||
|
public static void info(List<DataSource> dataSourceList){
|
||||||
|
dataSourceList.stream().forEach(dataSource -> {
|
||||||
|
// 构造jdbc连接字符串
|
||||||
|
String jdbcUrl="jdbc:"+dataSource.getType().toLowerCase()+"://"+dataSource.getLinkAddress()+":"+dataSource.getPort()+"/"+dataSource.getDatabaseName();
|
||||||
|
|
||||||
|
// 创建并配置Druid数据源实例
|
||||||
|
DruidDataSource druidSource = new DruidDataSource();
|
||||||
|
druidSource.setUrl(jdbcUrl);
|
||||||
|
druidSource.setUsername(dataSource.getUsername());
|
||||||
|
druidSource.setPassword(dataSource.getPassword());
|
||||||
|
|
||||||
|
// 配置连接池参数
|
||||||
|
druidSource.setInitialSize(Integer.valueOf(dataSource.getInitNum().toString()));
|
||||||
|
druidSource.setMinIdle(5);
|
||||||
|
druidSource.setMaxActive(Integer.valueOf(dataSource.getMaxNum().toString()));
|
||||||
|
|
||||||
|
// 初始化Druid数据源,异常情况下抛出运行时异常
|
||||||
|
try {
|
||||||
|
druidSource.init();
|
||||||
|
} catch (SQLException e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 将数据源实例存储到pools中,以备后续使用
|
||||||
|
pools.put(dataSource.getDataSourceName()+"_"+dataSource.getId(), druidSource);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 根据提供的key从连接池获取一个数据库连接。
|
||||||
|
*
|
||||||
|
* @param key 连接池的标识,用于从连接池映射中找到对应的DruidDataSource。
|
||||||
|
* @return Connection 返回从指定连接池获取的数据库连接。
|
||||||
|
* @throws RuntimeException 如果获取连接时发生SQLException,则将其封装并抛出为RuntimeException。
|
||||||
|
*/
|
||||||
|
public static Connection getConnection(String key){
|
||||||
|
// 根据key获取对应的DruidDataSource连接池
|
||||||
|
DruidDataSource druidDataSource = pools.get(key);
|
||||||
|
// 尝试从连接池中获取一个数据库连接
|
||||||
|
try {
|
||||||
|
return druidDataSource.getConnection();
|
||||||
|
} catch (SQLException e) {
|
||||||
|
// 如果获取连接失败,抛出运行时异常
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* 将给定的连接对象归还到某个资源池中。此方法为非静态方法,需要通过类实例来调用。
|
||||||
|
* 该方法尝试关闭传入的连接,如果关闭失败,则抛出运行时异常。
|
||||||
|
*
|
||||||
|
* @param connection 需要被归还的数据库连接对象。
|
||||||
|
* @throws RuntimeException 如果尝试关闭连接时发生SQLException,则抛出此异常。
|
||||||
|
*/
|
||||||
|
public static void addBack(Connection connection){
|
||||||
|
try {
|
||||||
|
connection.close(); // 尝试关闭数据库连接
|
||||||
|
} catch (SQLException e) {
|
||||||
|
throw new RuntimeException(e); // 如果关闭连接失败,转换异常并抛出
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 根据提供的键获取连接池,并统计当前连接池中正在使用的连接数和线程池中的总连接数。
|
||||||
|
*
|
||||||
|
* @param key 用于从连接池映射中获取特定DruidDataSource的键。
|
||||||
|
*/
|
||||||
|
public static void size(String key){
|
||||||
|
// 根据键从连接池映射中获取DruidDataSource实例
|
||||||
|
DruidDataSource druidDataSource = pools.get(key);
|
||||||
|
// 获取当前正在使用的连接数量
|
||||||
|
int activeCount = druidDataSource.getActiveCount();
|
||||||
|
// 获取线程池中当前的连接数量
|
||||||
|
int poolingCount = druidDataSource.getPoolingCount();
|
||||||
|
// 记录连接池的使用情况
|
||||||
|
log.info(key+" 连接池中正在使用连接: "+activeCount+"个 ,线程池中线程数量: "+poolingCount+"个");
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public static void setPools(HashMap<String, DruidDataSource> pools) {
|
||||||
|
MyDataSource.pools = pools;
|
||||||
|
log.info("数据源池初始化成功");
|
||||||
|
for (String key : pools.keySet()) {
|
||||||
|
log.info("数据源池初始化成功,key: "+key);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue