According to other answers (1, 2), as well as some documentation in the mariadb-connector-j repo, the consensus seems to be:
On a master/slave cluster, driver will use underlying 2 connections: one to a master instance, one to a slave instance.
A "connection" to aurora mean 2 underlying connection to instances: one to master, one to slave. Driver will use the underlying connection to master or slave according to Connection.setReadonly().
When using the "aurora" keyword, driver , under the hood, create 2 connections: a connection to the primary server, a connection to one of the replicas if any. The goal is always to save resources on the main server. Generally, only one pool is configured. The driver then uses the connection to the primary / replica according to [Connection.setReadOnly]
However, I am trying this for myself and do not see this behavior with MariaDB Connector/J 2.6.2 (in a Play 2.8.x application, which uses HikariCP as the connection pool). I created a new MySQL Aurora cluster (with a master and read-replica) and modified my test application to connect to this cluster with a HikariCP pool that's configured at a fixed size of 8 connections, using the cluster endpoint url as follows:
"jdbc:mysql:aurora://test.cluster-abcdefg.us-east-1.rds.amazonaws.com:3306/test"
From what I can see in the logs, as well as the attached image from the RDS web dashboard's monitoring tab, once the application starts up, the master goes to 8 connections (blue line) and the read-replica has 0 connections (orange line). This seems to be contrary to all the above points that, under the hood, the driver makes 2 underlying connections and chooses between them based on the status of a given Connection
's read-only property.
The Play configuration for this connection is as follows:
test {
driver = org.mariadb.jdbc.Driver
url = "jdbc:mysql:aurora://test.cluster-abcdefg.us-east-1.rds.amazonaws.com:3306/test"
hikaricp {
autoCommit = false
readOnly = false
}
}
If I instead configure the connection like:
test {
driver = org.mariadb.jdbc.Driver
url = "jdbc:mysql:aurora://test.cluster-abcdefg.us-east-1.rds.amazonaws.com:3306/test"
hikaricp {
autoCommit = true
readOnly = true
}
}
then I do see connections made to both the master and read-replica:
Please help me understand this behavior.
What I'm trying to achieve is to have separate write and read pools. The write pool would always point to the master, and the read pool would always point to a read replica (assuming one exists). I want to avoid dual underlying connections, as the readOnly
setting is configured at the pool level and will never change on a given Connection
during its lifetime. How can I achieve this?
Additionally, I would like to know whether I am expected to use the autoReconnect
parameter when specifying the aurora
failover mode. Does the value of that parameter matter with the aurora
failover mode?
conn.setReadOnly(true);
It does not do this automatically when you use a SELECT statement, you need to explicitly mark the connection as read-only. – blagerweij Dec 13 '20 at 22:21