内容简介:在对上述上述三个方法测试验证有效测试代码如下(部分):
在 https://github.com/go-sql-driver/mysql 可以看到关于 go 的连接池和超时相关的信息如下: Connection pool and timeouts The connection pool is managed by Go's database/sql package. For details on how to configure the size of the pool and how long connections stay in the pool see * DB.SetMaxOpenConns ,* DB.SetMaxIdleConns , and * DB.SetConnMaxLifetime in the database/sql documentation. The read, write, and dial timeouts for each individual connection are configured with the DSN parameters readTimeout, writeTimeout, and timeout, respectively
对上述上述三个方法测试验证有效
测试代码如下(部分):
片段一:
var err error My, err = sql.Open("mysql", "root:@tcp(127.0.0.1:3306)/test?parseTime=true") if err != nil { log.Fatal(err.Error()) } err = My.Ping() if err != nil { log.Fatal(err.Error()) } // Connection pool and timeouts // 连接池 和 超时 My.SetMaxOpenConns(20) // 最大打开连接数 My.SetMaxIdleConns(10) // 最大空闲连接数 //My.SetConnMaxLifetime(time.Second * 10) // 连接过期时间; 如果不设置 连接会一直不释放
片段二:
func (p *Person) GetAll() (persons []Person, err error) { persons = make([]Person, 0) rows, err := db.My.Query("SELECT id, firstname, lastname FROM person") defer rows.Close() if err != nil { return } time.Sleep(time.Second * 5) //**测试连接池效果 保持db连接不释放** for rows.Next() { var person Person rows.Scan(&person.Id, &person.FirstName, &person.LastName) persons = append(persons, person) } if err = rows.Err(); err != nil { return } return }
开始测试:
for ((i=0;i<1000;i++)) do curl http://127.0.0.1:10086/person/all & done
netstat -anp | findstr 3306 > 1.txt // windows
测试结果:
可以看到确实保持住了20个连接,go的db连接池使用和 Java 中配置连接池一样方便
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网
猜你喜欢:- 携程一次 Dubbo 连接超时问题的排查
- 记一次Kubernetes集群异常:kubelet连接apiserver超时
- Kooder 1.0 Alpha2 发布,支持 Gitlab 连接超时设置
- kubernetes 中删除 pod 导致客户端连接不存在的 IP 超时问题
- wifidog源码分析Lighttpd1.4.20源码分析之fdevent系统(4) -----连接socket的处理与超时处理
- redigo设置超时时间
本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
The Filter Bubble
Eli Pariser / Penguin Press / 2011-5-12 / GBP 16.45
In December 2009, Google began customizing its search results for each user. Instead of giving you the most broadly popular result, Google now tries to predict what you are most likely to click on. Ac......一起来看看 《The Filter Bubble》 这本书的介绍吧!