遇到了单独使用SqlServer连接池的情况,记录一下。
- pom.xml依赖
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.6.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<dependencies>
<!-- 本身不含连接池 -->
<dependency>
<groupId>com.microsoft.sqlserver</groupId>
<artifactId>sqljdbc4</artifactId>
<version>4.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.zaxxer/HikariCP -->
<dependency>
<groupId>com.zaxxer</groupId>
<artifactId>HikariCP</artifactId>
<version>3.4.2</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
- 配置文件
app.sqlConn=jdbc:sqlserver://127.0.0.1:1433;databaseName=test
app.sqlUser=sa
app.sqlPwd=123456
- 数据库连接工厂类
//读取配置文件的配置
@Data
@Configuration
@ConfigurationProperties(prefix = "app")
public class AppConfig {
/**
* SQL Server连接
*/
private String sqlConn;
/**
* SQL Server用户名
*/
private String sqlUser;
/**
* SQL Server密码
*/
private String sqlPwd;
}
@Slf4j
@Component
public final class DBFactory {
private static HikariDataSource INSTANCE = null;
//数据库连接信息
public static String URL;
private static String USER_NAME ;
private static String PASSWORD ;
@Autowired
private AppConfig appConfig;
//私有化,防止直接实例化
private DBFactory() {
}
@PostConstruct
public void init(){
URL = appConfig.getSqlConn();
USER_NAME = appConfig.getSqlUser();
PASSWORD = appConfig.getSqlPwd();
}
/**
* 获取数据源
* 1. HikariDataSource提供的方法比DataSource丰富
* 2. 保证多线程下单例
*
* @return
*/
private static HikariDataSource getDataSource() {
if (INSTANCE == null) {
synchronized (DBFactory.class) {
if (INSTANCE == null) {
HikariConfig config = new HikariConfig();
//最大连接数限制
config.setMaximumPoolSize(2000);
//写死driverClass,避免重复确认
config.setDataSourceClassName("com.microsoft.sqlserver.jdbc.SQLServerDataSource");
config.addDataSourceProperty("url", URL);
config.addDataSourceProperty("user", USER_NAME);
config.addDataSourceProperty("password", PASSWORD);
//TODO SqlServer没有找到cachePrepStmtpreStmtCacheSize相关的参数
INSTANCE = new HikariDataSource(config);
}
}
}
return INSTANCE;
}
/**
* 获取数据库连接
* @return
*/
public static Connection getConnection() throws SQLException {
if(getDataSource() == null){
throw new RuntimeException("get sqlserver datasource failed");
}
return getDataSource().getConnection();
}
/**
* 【慎用】
*/
public static final void close() {
if (INSTANCE != null) {
// 检查多线程下其他客户端方法关闭
synchronized (DBFactory.class) {
if (INSTANCE != null) {
INSTANCE.close();
log.info("#########connection pool closed##########");
}
}
}
}
}
- 使用
public static void insertUser(User user) throws SQLException {
try (
Connection conn = DBFactory.getConnection();
Statement st = conn.createStatement();
) {
String insertSQL = "Insert into user(id, name) VALUES(";
insertSQL += user.getUser() + ", '";
insertSQL += user.getName + "'";
insertSQL += ")";
log.error(insertSQL);
st.executeUpdate(insertSQL);
} catch (Exception ex) {
log.error("insert OA_RCT_ResumeEduEntityTags error: " + ex.getMessage());
ex.printStackTrace();
}
}