连接池配置
parent
3e5a7ca928
commit
d8fab4fe7b
@ -0,0 +1,39 @@
|
||||
package com.anjiplus.template.gaea.business.modules.dataSource.service;
|
||||
|
||||
import com.anjiplus.template.gaea.business.modules.dataSource.controller.dto.DataSourceDto;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.SQLException;
|
||||
|
||||
/**
|
||||
* Created by raodeming on 2021/8/6.
|
||||
*/
|
||||
public interface JdbcService {
|
||||
|
||||
/**
|
||||
* 删除数据库连接池
|
||||
*
|
||||
* @param id
|
||||
*/
|
||||
void removeJdbcConnectionPool(Long id);
|
||||
|
||||
|
||||
/**
|
||||
* 获取连接
|
||||
*
|
||||
* @param dataSource
|
||||
* @return
|
||||
* @throws SQLException
|
||||
*/
|
||||
Connection getPooledConnection(DataSourceDto dataSource) throws SQLException;
|
||||
|
||||
/**
|
||||
* 测试数据库连接 获取一个连接
|
||||
*
|
||||
* @param dataSource
|
||||
* @return
|
||||
* @throws ClassNotFoundException driverName不正确
|
||||
* @throws SQLException
|
||||
*/
|
||||
Connection getUnPooledConnection(DataSourceDto dataSource) throws SQLException;
|
||||
}
|
@ -0,0 +1,96 @@
|
||||
package com.anjiplus.template.gaea.business.modules.dataSource.service.impl;
|
||||
|
||||
import com.alibaba.druid.pool.DruidDataSource;
|
||||
import com.anjiplus.template.gaea.business.config.DruidProperties;
|
||||
import com.anjiplus.template.gaea.business.modules.dataSource.controller.dto.DataSourceDto;
|
||||
import com.anjiplus.template.gaea.business.modules.dataSource.service.JdbcService;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.SQLException;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
/**
|
||||
* Created by raodeming on 2021/8/6.
|
||||
*/
|
||||
@Service
|
||||
@Slf4j
|
||||
public class JdbcServiceImpl implements JdbcService {
|
||||
|
||||
@Autowired
|
||||
private DruidProperties druidProperties;
|
||||
|
||||
/**
|
||||
* 所有数据源的连接池存在map里
|
||||
*/
|
||||
static Map<Long, DruidDataSource> map = new ConcurrentHashMap<>();
|
||||
|
||||
public DruidDataSource getJdbcConnectionPool(DataSourceDto dataSource) {
|
||||
if (map.containsKey(dataSource.getId())) {
|
||||
return map.get(dataSource.getId());
|
||||
} else {
|
||||
try {
|
||||
if (!map.containsKey(dataSource.getId())) {
|
||||
DruidDataSource pool = druidProperties.dataSource(dataSource.getJdbcUrl(),
|
||||
dataSource.getUsername(), dataSource.getPassword(), dataSource.getDriverName());
|
||||
map.put(dataSource.getId(), pool);
|
||||
log.info("创建连接池成功:{}", dataSource.getJdbcUrl());
|
||||
}
|
||||
return map.get(dataSource.getId());
|
||||
} finally {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 删除数据库连接池
|
||||
*
|
||||
* @param id
|
||||
*/
|
||||
@Override
|
||||
public void removeJdbcConnectionPool(Long id) {
|
||||
try {
|
||||
DruidDataSource pool = map.get(id);
|
||||
if (pool != null) {
|
||||
log.info("remove pool success, datasourceId:{}", id);
|
||||
map.remove(id);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
log.error("error", e);
|
||||
} finally {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取连接
|
||||
*
|
||||
* @param dataSource
|
||||
* @return
|
||||
* @throws SQLException
|
||||
*/
|
||||
@Override
|
||||
public Connection getPooledConnection(DataSourceDto dataSource) throws SQLException{
|
||||
DruidDataSource pool = getJdbcConnectionPool(dataSource);
|
||||
return pool.getConnection();
|
||||
}
|
||||
|
||||
/**
|
||||
* 测试数据库连接 获取一个连接
|
||||
*
|
||||
* @param dataSource
|
||||
* @return
|
||||
* @throws ClassNotFoundException driverName不正确
|
||||
* @throws SQLException
|
||||
*/
|
||||
@Override
|
||||
public Connection getUnPooledConnection(DataSourceDto dataSource) throws SQLException {
|
||||
DruidDataSource druidDataSource = druidProperties.dataSource(dataSource.getJdbcUrl(),
|
||||
dataSource.getUsername(), dataSource.getPassword(), dataSource.getDriverName());
|
||||
return druidDataSource.getConnection();
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue