SafeObjectPool icon indicating copy to clipboard operation
SafeObjectPool copied to clipboard

mysql的连接池获取

Open junlei-lv opened this issue 5 years ago • 15 comments

为什么每次获取出来的conn打开事务的时候回出现 conn是null的情况。

junlei-lv avatar Jun 26 '19 05:06 junlei-lv

看看定义,和代码

2881099 avatar Jun 26 '19 05:06 2881099

嗯嗯 刚才git上不能提交图片。我给你邮件截图。

[cid:_Foxmail.1@62becd44-426d-376d-bfc5-6bf8966612e3]

构造里边初始化了pool对象。然后在getconn的时候获取一个连接。 刚才我本地调试还发现了一个问题。基于这个conn如果不开启事务的话,就没问题。但是在开启事务的时候 就会提示未将对象引用到实例 也就是conn对象有问题。


[email protected]

发件人: 2881099mailto:[email protected] 发送时间: 2019-06-26 13:54 收件人: 2881099/SafeObjectPoolmailto:[email protected] 抄送: junlei-lvmailto:[email protected]; Authormailto:[email protected] 主题: Re: [2881099/SafeObjectPool] mysql的连接池获取 (#3)

看看定义,和代码

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHubhttps://github.com/2881099/SafeObjectPool/issues/3?email_source=notifications&email_token=AJ7SYWPO4XEFCGO5PACJZKLP4MACLA5CNFSM4H3OVW32YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGODYSNHNA#issuecomment-505729972, or mute the threadhttps://github.com/notifications/unsubscribe-auth/AJ7SYWLYHWMFQPTOD7U2PXDP4MACLANCNFSM4H3OVW3Q.

junlei-lv avatar Jun 26 '19 05:06 junlei-lv

用qq等工具截图,在github回复框可以直接粘贴的

2881099 avatar Jun 26 '19 05:06 2881099

图片

junlei-lv avatar Jun 26 '19 05:06 junlei-lv

https://github.com/2881099/FreeSql/blob/master/Providers/FreeSql.Provider.MySql/MySqlAdo/MySqlConnectionPool.cs

看看这个

2881099 avatar Jun 26 '19 06:06 2881099

嗯嗯


[email protected]

发件人: 2881099mailto:[email protected] 发送时间: 2019-06-26 14:00 收件人: 2881099/SafeObjectPoolmailto:[email protected] 抄送: junlei-lvmailto:[email protected]; Authormailto:[email protected] 主题: Re: [2881099/SafeObjectPool] mysql的连接池获取 (#3)

https://github.com/2881099/FreeSql/blob/master/Providers/FreeSql.Provider.MySql/MySqlAdo/MySqlConnectionPool.cs

看看这个

― You are receiving this because you authored the thread. Reply to this email directly, view it on GitHubhttps://github.com/2881099/SafeObjectPool/issues/3?email_source=notifications&email_token=AJ7SYWN6NYWAX27RSHTUTODP4MAZJA5CNFSM4H3OVW32YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGODYSNR6I#issuecomment-505731321, or mute the threadhttps://github.com/notifications/unsubscribe-auth/AJ7SYWKDUR5U5V5NSPNHC3LP4MAZJANCNFSM4H3OVW3Q.

junlei-lv avatar Jun 26 '19 06:06 junlei-lv

已经引用了freesql.mysql的组件。 但是

var pool = new MySql.Data.MySqlClient.MySqlConnectionPool("名称", connectionString, 可用时触发的委托, 不可用时触发的委托); var conn = pool.Get(); try { // 使用 ... pool.Return(conn); //正常归还 } catch (Exception ex) { pool.Return(conn, ex); //发生错误时归还 }

这个实例中的 MySqlConnectionPool 还是引用出来。


[email protected]

发件人: 2881099mailto:[email protected] 发送时间: 2019-06-26 14:00 收件人: 2881099/SafeObjectPoolmailto:[email protected] 抄送: junlei-lvmailto:[email protected]; Authormailto:[email protected] 主题: Re: [2881099/SafeObjectPool] mysql的连接池获取 (#3)

https://github.com/2881099/FreeSql/blob/master/Providers/FreeSql.Provider.MySql/MySqlAdo/MySqlConnectionPool.cs

看看这个

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHubhttps://github.com/2881099/SafeObjectPool/issues/3?email_source=notifications&email_token=AJ7SYWN6NYWAX27RSHTUTODP4MAZJA5CNFSM4H3OVW32YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGODYSNR6I#issuecomment-505731321, or mute the threadhttps://github.com/notifications/unsubscribe-auth/AJ7SYWKDUR5U5V5NSPNHC3LP4MAZJANCNFSM4H3OVW3Q.

junlei-lv avatar Jun 26 '19 06:06 junlei-lv

还是引用出来是什么意思?

2881099 avatar Jun 26 '19 06:06 2881099

另外一个问题就是。使用pool开启的conn对象如果在不开启事务的情况下是可以正常使用的。但是一旦使用事务。在开启事务的时候就会检查到conn为null


[email protected]

发件人: 2881099mailto:[email protected] 发送时间: 2019-06-26 14:00 收件人: 2881099/SafeObjectPoolmailto:[email protected] 抄送: junlei-lvmailto:[email protected]; Authormailto:[email protected] 主题: Re: [2881099/SafeObjectPool] mysql的连接池获取 (#3)

https://github.com/2881099/FreeSql/blob/master/Providers/FreeSql.Provider.MySql/MySqlAdo/MySqlConnectionPool.cs

看看这个

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHubhttps://github.com/2881099/SafeObjectPool/issues/3?email_source=notifications&email_token=AJ7SYWN6NYWAX27RSHTUTODP4MAZJA5CNFSM4H3OVW32YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGODYSNR6I#issuecomment-505731321, or mute the threadhttps://github.com/notifications/unsubscribe-auth/AJ7SYWKDUR5U5V5NSPNHC3LP4MAZJANCNFSM4H3OVW3Q.

junlei-lv avatar Jun 26 '19 06:06 junlei-lv

找不到MysqlConnectionPool这个类


[email protected]

发件人: 2881099mailto:[email protected] 发送时间: 2019-06-26 14:52 收件人: 2881099/SafeObjectPoolmailto:[email protected] 抄送: junlei-lvmailto:[email protected]; Authormailto:[email protected] 主题: Re: [2881099/SafeObjectPool] mysql的连接池获取 (#3)

还是引用出来是什么意思?

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHubhttps://github.com/2881099/SafeObjectPool/issues/3?email_source=notifications&email_token=AJ7SYWPZ57FN6WVHFBEJVRDP4MG2ZA5CNFSM4H3OVW32YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGODYSQ56Y#issuecomment-505745147, or mute the threadhttps://github.com/notifications/unsubscribe-auth/AJ7SYWLI565I3KO36QX7XF3P4MG2ZANCNFSM4H3OVW3Q.

junlei-lv avatar Jun 26 '19 07:06 junlei-lv

那是一个内部类,可以把代码 copy 出来使用

2881099 avatar Jun 26 '19 07:06 2881099

弱弱的问下,你封装mysql用来作什么

2881099 avatar Jun 26 '19 07:06 2881099

mysql有个类型是不支持事务的 难道是用了mysam?

batsword avatar Sep 11 '19 09:09 batsword

长连接维持也需要开销,这个我也懵了, 到底是需要维持长连接呢,还是使用官方推荐的不复用,每次都新开连接. 目前用的mysqlconnector说不推荐用长连接或者复用, 请教下大佬,高频率查询和插入数据,长连接好还是短连接好?

senioi avatar Oct 10 '19 21:10 senioi

@senioi 内部服务用长连接好,效率高,且数量有限。

其实ado.net基本都实现了pooling,SafeObjectPool除了池化作用,还有断熔和恢复功能。

当数据库连接不上的时候,程序基本会一直请求数据库,15秒连超时才响应。断熔后即刻响应,不影响程序的其他功能。恢复功能,可以等到数据库可用时自动切换回来。

2881099 avatar Oct 10 '19 23:10 2881099