I have setup TiDB for my .net core app. I'm connecting TiDB with the MYSQL connector in the .net core app. Below is my connection string
server=server1,server2,server3;Port=4000;UID=test_user;PWD=****;database=Test_database;ConnectionReset=false;
As I have specified all the nodes in the connection string with comma separator and when tried to put some load it's always going to the first node. Ideally requests load should have been distributed across all the nodes.
I ran the SHOW FULL PROCESSLIST
command to check app requests which are showing in the first node only.
Here is my source code to connect TiDB.
public async Task<Error> ExecuteStatementAsync(Context ctx, MySqlCommand command)
{
Error error = default;
try
{
using (MySqlConnection connection = new MySqlConnection(_connectionString))
{
MySqlTransaction trans = default;
try
{
await connection.OpenAsync();
trans = connection.BeginTransaction();
using MySqlCommand cmd = command;
cmd.Connection = connection;
cmd.Transaction = trans;
cmd.Prepare();
await cmd.ExecuteNonQueryAsync();
trans.Commit();
return null;
}
catch (Exception ex)
{
// populate error
if (trans != null)
{
trans.Rollback();
}
return error;
}
}
}
catch (Exception ex)
{
// populate error
return error;
}
}
public async Task<(DataTable, Error)> SelectAsync(Context ctx, MySqlCommand command, string queryIdentifier)
{
Error error = default;
try
{
using (MySqlConnection connection = new MySqlConnection(_connectionString))
{
DataTable dt = new DataTable();
MySqlCommand cmd = command;
try
{
await connection.OpenAsync();
cmd.Connection = connection;
cmd.Prepare();
using (DbDataReader reader = await cmd.ExecuteReaderAsync())
{
dt.Load(reader);
}
return (dt, null);
}
catch (Exception ex)
{
return (null, error);
}
}
}
catch (Exception ex)
{
return (null, error);
}
}
Is there anything wrong with the code or connection string? Why it's happening so?