go sql.stmt query 发生了一个NULL值,所以发现了error, 发现服务不停的初始化sql stmt, 导致连接数过多,服务就变得很慢。

首先,我在初始化的之前,要判断这个是否是NULL(见下面列表5)导致的,并非是sql请求出错。 我为啥要全局变量保存sql.Stmt, 导致要处理sql.Stmt出错。

go prepare 的问题(见下面列表2)导致数据库连接数太大,所以我使用全局变量保存用prepare 方法创建的sql.Stmt(见下面列表3,但是根据列表2,其实没有解决问题,  除非列表4),同时就要考虑出现错误的时候重新初始化。初始化的时候就涉及到互斥的问题, 那就要加锁(见下面列表1)。但是锁不能乱加,因为后台是要支持并发的,如果都加写锁。就会响应很慢了。所以要加读锁,重新初始化的时候,解读锁加写锁,但是加锁的作用是线程与线程之前,并非是for 语句,所以你如果有for语句,要考虑是否要break。加锁的范围不要很大,因为太大不好控制(出现嵌套加锁,其实无关的代码占用了释放锁的时间),而且你要释放锁


1. 读写锁

http://www.alexedwards.net/blog/understanding-mutexes

func (rw *RWMutex) Lock()

Lock locks rw for writing. If the lock is already locked for reading or writing, Lock blocks until the lock is available. To ensure that the lock eventually becomes available, a blocked Lock call excludes new readers from acquiring the lock.

不能读也不能写

Unlock unlocks rw for writing. It is a run-time error if rw is not locked for writing on entry to Unlock.

As with Mutexes, a locked RWMutex is not associated with a particular goroutine. One goroutine may RLock (Lock) an RWMutex and then arrange for another goroutine to RUnlock (Unlock) it.

We can achieve this using RWMutex, a reader/writer mutual exclusion lock which allows any number of readers to hold the lock or one writer.  ---  可以有多个读操作,或者一个写操作。

如果不加对应的锁,然后解锁的话会引发错误。会引发错误。

但是已经加了一个读锁,没有解锁又加一个写锁,会造成死锁。就是挂住,程序不会动了。

有的会打印: fatal error: all goroutines are asleep - deadlock!


2. 使用sql prepare不能很好复用,它可能自己重新生成prepare语句,会导致泄露。

http://go-database-sql.org/prepared.html

Here’s how it works:

  1. When you prepare a statement, it’s prepared on a connection in the pool.
  2. The Stmt object remembers which connection was used.
  3. When you execute the Stmt, it tries to use the connection. If it’s not available because it’s closed or busy doing something else, it gets another connection from the pool and re-prepares the statement with the database on another connection.

Because statements will be re-prepared as needed when their original connection is busy, it’s possible for high-concurrency usage of the database, which may keep a lot of connections busy, to create a large number of prepared statements. This can result in apparent leaks of statements, statements being prepared and re-prepared more often than you think, and even running into server-side limits on the number of statements.


3. sql.Stmt 是可并发的

type Stmt

type Stmt struct {
// contains filtered or unexported fields
}

Stmt is a prepared statement. A Stmt is safe for concurrent use by multiple goroutines.

http://go-database-sql.org/retrieving.html


4.  go 1.4 fixed bug.

prepare stmt

http://studygolang.com/articles/1795


5. 数据表里默认值为NULL

http://www.cnblogs.com/javathread/archive/2012/01/05/2634719.html

11. 尽可能的使用 NOT NULL

除非你有一个很特别的原因去使用 NULL 值,你应该总是让你的字段保持 NOT NULL。这看起来好像有点争议,请往下看。

首先,问问你自己“Empty”和“NULL”有多大的区别(如果是INT,那就是0和NULL)?如果你觉得它们之间没有什么区别,那么你就不要使用NULL。(你知道吗?在 Oracle 里,NULL 和 Empty 的字符串是一样的!)

不要以为 NULL 不需要空间,其需要额外的空间,并且,在你进行比较的时候,你的程序会更复杂。 当然,这里并不是说你就不能使用NULL了,现实情况是很复杂的,依然会有些情况下,你需要使用NULL值。

下面摘自MySQL自己的文档:    

“NULL columns require additional space in the row to record whether their values are NULL. For MyISAM tables, each NULL column takes one bit extra, rounded up to the nearest byte.”      

一个NULL引发的血案的更多相关文章

  1. Replication的犄角旮旯(六)-- 一个DDL引发的血案(上)(如何近似估算DDL操作进度)

    <Replication的犄角旮旯>系列导读 Replication的犄角旮旯(一)--变更订阅端表名的应用场景 Replication的犄角旮旯(二)--寻找订阅端丢失的记录 Repli ...

  2. 一个字母引发的血案 java.io.File中mkdir()和mkdirs()

    一个字母引发的血案 明天开始放年假了,临放假前有个爬虫的任务,其中需要把网络图片保存到本地,很简单,马上写完了代码: //省略部分代码... Long fileId= (Long) data.get( ...

  3. SQL实战——04. 查找所有已经分配部门的员工的last_name和first_name以及dept_no (一个逗号引发的血案)

    查找所有已经分配部门的员工的last_name和first_name以及dept_noCREATE TABLE `dept_emp` (`emp_no` int(11) NOT NULL,`dept_ ...

  4. Replication的犄角旮旯(七)-- 一个DDL引发的血案(下)(聊聊logreader的延迟)

    <Replication的犄角旮旯>系列导读 Replication的犄角旮旯(一)--变更订阅端表名的应用场景 Replication的犄角旮旯(二)--寻找订阅端丢失的记录 Repli ...

  5. 【原创】经验分享:一个Content-Length引发的血案(almost....)

    前言 上周在工作中遇到一个问题,挺有意思,这里记录一下.上周在工作中遇到一个问题,挺有意思,这里记录一下.标题起的很唬人,这个问题差点引发血案,花哥还是很严谨的一个人,后面备注了almost.... ...

  6. 一个链接引发的血案---------服务器 IO及网络流量暴涨解决历程

    在这里介绍一次因为更改网站地址而引发服务器IO读取速度,网络流入流出速度暴涨10倍的解决经历. 环境:Ubuntu + Nginx + php-cgi + Wordpress 事情是这样的,现在网站使 ...

  7. 一个null引发的错误

    写程序时,发生一个unrecognized selector的错误,很显然,这是一个经典错误,运行时找不到可调用的方法. 系统提示的reason是 -[NSNull length] 错误 经过查找,原 ...

  8. String属于“假引用类型”,代码为证(一个String引发的血案...)

    一直以为String是引用类型,今天写了个浅拷贝的测试,发现String有基本类型的特征. class A{ public int a = 555; } class User implements C ...

  9. 【转】Druid连接池一个设置引发的血案

    https://my.oschina.net/haogrgr/blog/224010 今天在一台配置很低的机器上运行批量更新的程序~~~ 大概跑了三十分钟~~~这配置~~~这程序~~~ 然后华丽丽的报 ...

随机推荐

  1. DS实验题 击鼓传花

    题目: 代码1(数组实现): // // main.cpp // DS-击鼓传花 // // Created by wasdns on 16/11/9. // Copyright © 2016年 wa ...

  2. PHP 设计模式 笔记与总结(9)数据对象映射模式

    [数据对象映射模式] 是将对象和数据存储映射起来,对一个对象的操作会映射为对数据存储的操作.例如在代码中 new 一个对象,使用数据对象映射模式就可以将对象的一些操作比如设置一些属性,就会自动保存到数 ...

  3. MySQL 数据库设计 笔记与总结(1)需求分析

    数据库设计的步骤 ① 需求分析 ② 逻辑设计 使用 ER 图对数据库进行逻辑建模 ③ 物理设计 ④ 维护优化 a. 新的需求进行建表 b. 索引优化 c. 大表拆分 [需求分析] ① 了解系统中所要存 ...

  4. FTS抓包看AVDTP

    1.概述   测试过程为打开Audio连接,没有听音乐,人后断开Audio连接,主要目的是为了测试AVDTP的工作流程.   2.Frame分析    首先贴出抓取的关于AVDTP的包: 在L2CAP ...

  5. Redis-秒杀场景应用

    Redis Util实现 package test.jedis; import java.util.List; import java.util.Set; import redis.clients.j ...

  6. Android源码剖析之Framework层进阶版(Wms窗口管理)

    本文来自http://blog.csdn.net/liuxian13183/ ,引用必须注明出处! 上一篇我们主要讲了Ams,篇幅有限,本篇再讲讲Wms,即WindowManagerService,管 ...

  7. MongoDB过过瘾

    MongoDB 中默认的数据库为 test,连接后尝试以下操作 连接 插入数据:用过json的同学看到这格式相信不会陌生吧! db.person.insert({}) db.person.insert ...

  8. [LeetCode]题解(python):034-Search for a Range

    题目来源 https://leetcode.com/problems/search-for-a-range/ Given a sorted array of integers, find the st ...

  9. Sort---hdu5884(优先队列+二分)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5884 题意:有n个有序序列,每个序列有ai个元素,现在有一个程序每次可以归并最多k个序列,最终把所有的 ...

  10. Subway---poj2502(最短路)

    题目链接:http://poj.org/problem?id=2502 人走路的速度是10km/h,地铁的速度是40km/h题目给出一个起点,一个终点,以及几条地铁线路运行的站点.题目给的点的做坐标单 ...