We are trying to use GO to create a web app that connects to ProxySQL, which will in turn connect to the various MySQL database servers based on MySQL query rules that are set in ProxySQL.
We can connect to ProxySQL via command line in linux and everything works as expected:
use database1;
select id from users where user_id=1;
use database2;
select id from posts where user_id=1;
The above databases are located on physically different servers. Rules in ProxySQL know how to route them.
While connected to ProxySQL, the rules work fine and we are connected to the correct database servers and the requests are processed as expected.
When we try the same thing with GO and transactions, we cannot switch the databases as the it returns that the database does not exist.
The database exists on the back end servers, which ProxySQL will direct traffic to.
For whatever reason, it works on a command line, but not in GO
We can make it work if we connect with GO and specify the database on the connection, then disconnect and reconnect with a new database connection.
We are trying to use connection pool so we don't waste the time in opening and closing the connections.
//open the connection to ProxySQL
db, err = sql.Open("mysql", "user:password@tcp(x.x.x.x:6033)/")
//The above only allows us to stay on the default database for the user
//in order to access each of the databases we would need to open a
//connection to database1 then open another connection to database2
//which defeats the whole purpose of connection pooling.
db, err = sql.Open("mysql", "user:password@tcp(x.x.x.x:6033)/database1")
... do something...
db, err = sql.Open("mysql", "user:password@tcp(x.x.x.x:6033)/database2")
... do something ...
//code below is an example of what is not working but works from
//command line in linux. Results are not processed below because we
//cannot get past the first error of no database
tx, err := db.Begin()
res, err := tx.Exec("use database1")
res, err = tx.Exec("select id from users where user_id=1;")
res, err = tx.Exec("use database2")
res, err = tx.Exec("select id from posts where user_id=1;")
tx.Commit()
ProxySQL knows how to route properly, but we cannot seem to get the functions working correctly in GOLANG