C
C#•10mo ago
moneycollector

Sometimes: Object Null Exception when using var rdr = await cmd.ExecuteReaderAsync();

The cmd.ExecuteReaderAsync sometimes get NullException. Source: public async Task<GetNavigationBarBulletsContent> Get([FromServices] MySqlDataSource dbBroadcast ) { await using var conn = await dbBroadcast.OpenConnectionAsync(); string qChannelsToScan = "SELECT channel_id FROM channels_members WHERE member_id = @my_id"; using var cmdChannelsToScan = new MySqlCommand(qChannelsToScan, conn); cmdChannelsToScan.Parameters.AddWithValue("my_id", internal_id); using var rdrChannelsToScan = await cmdChannelsToScan.ExecuteReaderAsync(); // Here is the NullException thrown }
13 Replies
moneycollector
moneycollector•10mo ago
This happens in around 1% of the cases. I hoped to use a MySQLDataSource dependency injection would help, but it did not. In most cases, after this primary error, i got MySqlConnector.MySqlProtocolException: Packet received out-of-order. Expected 1; got 2. either which took my thinking it has a connectionpool problem. Unfortunately it didn't solve the case.
Mkp
Mkp•10mo ago
use ```cs // code here ``` To format code
moneycollector
moneycollector•10mo ago
{
await using var conn = await dbBroadcast.OpenConnectionAsync();
string qChannelsToScan = "SELECT channel_id FROM channels_members WHERE member_id = @my_id";
using var cmdChannelsToScan = new MySqlCommand(qChannelsToScan, conn);
cmdChannelsToScan.Parameters.AddWithValue("my_id", internal_id);
using var rdrChannelsToScan = await cmdChannelsToScan.ExecuteReaderAsync(); // Here is the NullException thrown
}
{
await using var conn = await dbBroadcast.OpenConnectionAsync();
string qChannelsToScan = "SELECT channel_id FROM channels_members WHERE member_id = @my_id";
using var cmdChannelsToScan = new MySqlCommand(qChannelsToScan, conn);
cmdChannelsToScan.Parameters.AddWithValue("my_id", internal_id);
using var rdrChannelsToScan = await cmdChannelsToScan.ExecuteReaderAsync(); // Here is the NullException thrown
}
aha i see, new for me 🙂 thanks
Mkp
Mkp•10mo ago
What is the type of internal_id You should also not be making a new connection per call Perhaps you are hitting an upper connection limit? I dont think on the first line of the function needs a second await
moneycollector
moneycollector•10mo ago
long therefor i tried to use the dependency injection, but no improvement. Besides that, the MySqlConnector keeps a pool of connections available anyway and re-uses that with the OpenConnection command.. So it also doesn't make sense there i think myeah something like that would be it.. But can't see it as nowhere are signs of that for the rest. All other APIs / calls programs / connections etc are just fine. It looks like the system gets confused, especially with often hitting the second error that it expected more packets ( MySqlConnector.MySqlProtocolException: Packet received out-of-order. Expected 1; got 2 ) So it expects results on 1 query but gets another result, it seems. you are right, that first await can be ditched. using var will be enough.
SuperBrain
SuperBrain•10mo ago
You need to make sure that internal_id is never null, which is probably what causes your NRE at that line. Side-note: Don't use MySQL, it's bad 👀
moneycollector
moneycollector•10mo ago
i could test that but pretty sure it cant be null as prelim code would already crash on that point what do you advice mr SB?
SuperBrain
SuperBrain•10mo ago
PostgreSQL Both faster, more secure and with far more features than MySQL could even dream of ever having.
moneycollector
moneycollector•10mo ago
aight sounds good, will check it later on. for now i have a 1% unstable connectionpool bothering users hehe
Mkp
Mkp•10mo ago
Postgresql is a good one. I like mongodb personally.
Angius
Angius•10mo ago
If you don't like to think of pesky things like "schema", "type safety", or "data integrity" then yes, Mongo is a choice
SuperBrain
SuperBrain•10mo ago
Personally, I don't like and I don't generally recommend NoSQL databases, unless you have a very specific use-case which modern RDBMS don't provide for you. I have yet to find such use-cases. Because any requirements I had did not necessitate a NoSQL database.
SuperBrain
SuperBrain•10mo ago
The Hacker News
MongoDB Suffers Security Breach, Exposing Customer Data
A security incident at MongoDB has led to unauthorized access to corporate systems, compromising customer account info.
Want results from more Discord servers?
Add your server