mysql的链接单例模式和线程池的优劣性

单例模式代码

from functools import wraps
import pymysql
def single(cls):
    """实现类的单例模式"""
    instance_dict = dict()
    @wraps(cls)
    def single_instance(*args,**kwargs):
        if cls not in instance_dict:
            instance_dict[cls] = cls(*args,**kwargs)
        return instance_dict[cls]
    return single_instance

@single
class PyMySQLTest(object):
    def __init__(self,db_name):
        self.coon = None
        self.cursor = None
        self.db_name = db_name
        try:
            self.coon = pymysql.connect(host="xx",port=xx,user="xx",password="xx",database=self.db_name,charset="utf8")
            self.cursor = self.coon.cursor()
        except Exception as e:
            raise e

    def get_all(self):
        sql = "select cow_number from cow_info where isdelete=0"
        self.cursor.execute(sql)
        result = self.cursor.fetchall()
        print(result)

100个线程测试代码

from single_pool.test_mysql import mysql_test
from threading import Thread
import time
def test():
    single_instance = mysql_test.PyMySQLTest("cattle")
    single_instance.get_all()

if __name__ == '__main__':
    start_time = time.time()
# 创建100个线程链接实例,测试获取数据
    for i in range(100):
        t = Thread(target=test)
        t.start()
    end_time = time.time()
    print(end_time-start_time)

结果分析

测试10次 时间分别为 0.032 0.031 0.037 0.031 0.032 0.030 0.032 0.030 0.035 0.032

成功的结果

0.031002044677734375
(('1002',), ('1005',), ('2007',), ('3001',), ('3004',), ('868201803000700',), ('868201803000704',), ('868201803000702',), ('868201803000696',), ('868201803000701',), ('868201803000697',), ('868201803000699',), ('868201803000689',), ('868201803000682',), ('868201803000695',), ('868201803000688',), ('868201803000681',), ('868201803000703',), ('868201803000674',), ('868201803000651',), ('180078',), ('180025',), ('130558',), ('160080',), ('160090',), ('170071',), ('170087',), ('170099',), ('170135',), ('150153',), ('160073',), ('170083',), ('150037',), ('150249',), ('160168',), ('160002',), ('170080',), ('140157',), ('160225',), ('140161',), ('140588',), ('160804',), ('140170',), ('160068',), ('16411',), ('160527',), ('180070',), ('180026',), ('180034',), ('180004',), ('1003',), ('130653',), ('160170',), ('180753',), ('180795',), ('180698',), ('180675',), ('20191001',), ('20191002',), ('140680',), ('170058',), ('170159',), ('160059',), ('170095',), ('150092',), ('20200221',), ('20200224',), ('20200226',), ('130739',), ('160169',), ('150641',), ('160829',), ('20200301',), ('2020030111',), ('202003011112',), ('170151',), ('150350',), ('180007',), ('110615',), ('160250',), ('170107',), ('150039',), ('170153',), ('110926',), ('170136',), ('160097',), ('160423',), ('160329',), ('170069',), ('170152',), ('140738',), ('110301',), ('170092',), ('160098',), ('150090',), ('180748',))
(('1002',), ('1005',), ('2007',), ('3001',), ('3004',), ('868201803000700',), ('868201803000704',), ('868201803000702',), ('868201803000696',), ('868201803000701',), ('868201803000697',), ('868201803000699',), ('868201803000689',), ('868201803000682',), ('868201803000695',), ('868201803000688',), ('868201803000681',), ('868201803000703',), ('868201803000674',), ('868201803000651',), ('180078',), ('180025',), ('130558',), ('160080',), ('160090',), ('170071',), ('170087',), ('170099',), ('170135',), ('150153',), ('160073',), ('170083',), ('150037',), ('150249',), ('160168',), ('160002',), ('170080',), ('140157',), ('160225',), ('140161',), ('140588',), ('160804',), ('140170',), ('160068',), ('16411',), ('160527',), ('180070',), ('180026',), ('180034',), ('180004',), ('1003',), ('130653',), ('160170',), ('180753',), ('180795',), ('180698',), ('180675',), ('20191001',), ('20191002',), ('140680',), ('170058',), ('170159',), ('160059',), ('170095',), ('150092',), ('20200221',), ('20200224',), ('20200226',), ('130739',), ('160169',), ('150641',), ('160829',), ('20200301',), ('2020030111',), ('202003011112',), ('170151',), ('150350',), ('180007',), ('110615',), ('160250',), ('170107',), ('150039',), ('170153',), ('110926',), ('170136',), ('160097',), ('160423',), ('160329',), ('170069',), ('170152',), ('140738',), ('110301',), ('170092',), ('160098',), ('150090',), ('180748',))(('1002',), ('1005',), ('2007',), ('3001',), ('3004',), ('868201803000700',), ('868201803000704',), ('868201803000702',), ('868201803000696',), ('868201803000701',), ('868201803000697',), ('868201803000699',), ('868201803000689',), ('868201803000682',), ('868201803000695',), ('868201803000688',), ('868201803000681',), ('868201803000703',), ('868201803000674',), ('868201803000651',), ('180078',), ('180025',), ('130558',), ('160080',), ('160090',), ('170071',), ('170087',), ('170099',), ('170135',), ('150153',), ('160073',), ('170083',), ('150037',), ('150249',), ('160168',), ('160002',), ('170080',), ('140157',), ('160225',), ('140161',), ('140588',), ('160804',), ('140170',), ('160068',), ('16411',), ('160527',), ('180070',), ('180026',), ('180034',), ('180004',), ('1003',), ('130653',), ('160170',), ('180753',), ('180795',), ('180698',), ('180675',), ('20191001',), ('20191002',), ('140680',), ('170058',), ('170159',), ('160059',), ('170095',), ('150092',), ('20200221',), ('20200224',), ('20200226',), ('130739',), ('160169',), ('150641',), ('160829',), ('20200301',), ('2020030111',), ('202003011112',), ('170151',), ('150350',), ('180007',), ('110615',), ('160250',), ('170107',), ('150039',), ('170153',), ('110926',), ('170136',), ('160097',), ('160423',), ('160329',), ('170069',), ('170152',), ('140738',), ('110301',), ('170092',), ('160098',), ('150090',), ('180748',))

 

失败的结果

pymysql.err.OperationalError: (2003, "Can't connect to MySQL server on 'xx.xx.xx.xx' (timed out)")

socket.timeout: timed out

连接池代码

 

 

 

 

上一篇:JavaSE面试题:单例设计模式


下一篇:java-面试题必备