Skip to content

Commit

Permalink
修复连接池问题
Browse files Browse the repository at this point in the history
  • Loading branch information
mewhz committed Dec 21, 2021
1 parent 0595134 commit 95855a2
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 6 deletions.
10 changes: 9 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.7.15</version>
<version>5.7.17</version>
</dependency>

<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
Expand All @@ -41,6 +41,14 @@
<version>5.1.47</version>
</dependency>

<!-- https://mvnrepository.com/artifact/com.alibaba/druid -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.2.8</version>
</dependency>



</dependencies>

Expand Down
11 changes: 7 additions & 4 deletions src/main/java/com/mewhz/paste/utils/CodeSQL.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,16 @@

import cn.hutool.db.Db;
import cn.hutool.db.Entity;
import cn.hutool.db.ds.DSFactory;
import com.mewhz.paste.model.Code;
import com.mewhz.paste.model.IdentifyingCode;

import javax.sql.DataSource;
import java.sql.SQLException;
import java.util.List;

public class CodeSQL {
private DataSource ds = DSFactory.get();
public CodeSQL (){

}
Expand All @@ -18,7 +21,7 @@ public CodeSQL (){
*/
public void insertCode(Code code){
try {
Db.use().insert(
Db.use(ds).insert(
Entity.create("code")
.set("text", code.getText())
.set("time_id", code.getDate().getTime()+"")
Expand All @@ -38,7 +41,7 @@ public void insertCode(Code code){
*/
public void insertIdentifyingCode(IdentifyingCode identifyingCode){
try{
Db.use().insert(
Db.use(ds).insert(
Entity.create("identifying_code")
.set("identifying", identifyingCode.getIdentifying())
.set("time_id", identifyingCode.getTimeId())
Expand All @@ -55,7 +58,7 @@ public void insertIdentifyingCode(IdentifyingCode identifyingCode){
public List<Entity> findCode(String timeId){
List<Entity> codes = null;
try {
codes = Db.use().findAll(
codes = Db.use(ds).findAll(
Entity.create("code").set("time_id", "= " + timeId)
);
} catch (SQLException e) {
Expand All @@ -67,7 +70,7 @@ public List<Entity> findCode(String timeId){
public List<Entity> findIdentifyingCode(String identifying){
List<Entity> identifyingCodes = null;
try{
identifyingCodes = Db.use().query("select text, type, date, code.time_id, remark\n" +
identifyingCodes = Db.use(ds).query("select text, type, date, code.time_id, remark\n" +
"from code inner join identifying_code\n" +
" on code.time_id = identifying_code.time_id\n" +
" where identifying = ?", identifying);
Expand Down
22 changes: 21 additions & 1 deletion src/main/resources/db.setting
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,24 @@ initialSize = 1
# 最大连接池数量
maxActive = 8
# 最小连接池数量
minIdle = 1
minIdle = 1
# 获取连接时最大等待时间,单位毫秒。配置了maxWait之后, 缺省启用公平锁,并发效率会有所下降, 如果需要可以通过配置useUnfairLock属性为true使用非公平锁。
maxWait = 600
# 是否缓存preparedStatement,也就是PSCache。 PSCache对支持游标的数据库性能提升巨大,比如说oracle。 在mysql5.5以下的版本中没有PSCache功能,建议关闭掉。作者在5.5版本中使用PSCache,通过监控界面发现PSCache有缓存命中率记录, 该应该是支持PSCache。
poolPreparedStatements = false
# 要启用PSCache,必须配置大于0,当大于0时, poolPreparedStatements自动触发修改为true。 在Druid中,不会存在Oracle下PSCache占用内存过多的问题, 可以把这个数值配置大一些,比如说100
maxOpenPreparedStatements = -1
# 用来检测连接是否有效的sql,要求是一个查询语句。 如果validationQuery为null,testOnBorrow、testOnReturn、 testWhileIdle都不会其作用。
validationQuery = SELECT 1
# 申请连接时执行validationQuery检测连接是否有效,做了这个配置会降低性能。
testOnBorrow = true
# 归还连接时执行validationQuery检测连接是否有效,做了这个配置会降低性能
testOnReturn = true
# 建议配置为true,不影响性能,并且保证安全性。 申请连接的时候检测,如果空闲时间大于 timeBetweenEvictionRunsMillis,执行validationQuery检测连接是否有效。
testWhileIdle = true
# 有两个含义: 1) Destroy线程会检测连接的间隔时间 2) testWhileIdle的判断依据,详细看testWhileIdle属性的说明
timeBetweenEvictionRunsMillis = 600
# 物理连接初始化的时候执行的sql
connectionInitSqls = SELECT 1
# 属性类型是字符串,通过别名的方式配置扩展插件, 常用的插件有: 监控统计用的filter:stat 日志用的filter:log4j 防御sql注入的filter:wall
filters = stat
36 changes: 36 additions & 0 deletions src/test/java/com/mewhz/paste/SQLTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package com.mewhz.paste;

import cn.hutool.db.Db;
import cn.hutool.db.Entity;
import cn.hutool.db.ds.DSFactory;

import javax.sql.DataSource;
import java.sql.SQLException;
import java.util.List;
import java.util.Scanner;

public class SQLTest {

private static DataSource ds = DSFactory.get();

public static void main(String[] args) {
Scanner input = new Scanner(System.in);
while (true){
String timeId = input.next();
fun(timeId);
}
}

public static void fun(String timeId){
List<Entity> result = null;
{
try {
result = Db.use(ds).findAll(
Entity.create("student").set("id", "= " + timeId));
} catch (SQLException e) {
e.printStackTrace();
}
}
System.out.println(result);
}
}

0 comments on commit 95855a2

Please sign in to comment.