3

I have Java application with Hibernate framework(no spring) connect to MySQL DB , manage connection pooling via c3p0

i try to configure my apllication to read from slave db and write to master db , i have following this link to some extend Master/Slave load balance

let's say if the application already got a session with connection in pool and it need to execute a read-only method , like this

public someReadOnlyMethod()
{
    Session session = (get session from current Thread)

            //set read-only so that it read from slave db
            session.connection().setReadOnly(true);

            (...connect to db to do something...)

            //set it back in case of this method is followed by write method so that it go to master db
            session.connection().setReadOnly(false);


}

Is the pooling create a new connection to connect to db 2 times for read-only and write operation(if so,this will heavily impact performance) or it smart enough to swap the operation to already existing read-only and writable connection pool ?

thx for your advice.

4

2 回答 2

0

所以这与池无关;这一切都在mysql驱动程序中。c3p0 会将您对 setReadOnly 的调用(无论是真还是假)传递给底层连接,并且连接将相应地路由到主服务器或从服务器。

如果您不喜欢 Connections 默认的方式(可能默认情况下它们不是只读的),您可以在 c3p0 ConnectionCustomizer 的 onAcquire 方法中设置只读属性,并且使用设置的值(true 或 false)将变为c3p0 将连接重置为的默认值。

祝你好运!

于 2013-03-30T09:24:46.050 回答
0

tl; dr:每当您切换时,它都会重新使用现有连接setReadOnly(true/false)

当您这样做时,JDBC 将连接到您的连接 URL 中列出的所有服务器ReplicationDriver().connect(url)。无论您切换多少次,这些连接都将保持打开状态以供重复使用setReadOnly()

资料来源:我刚刚使用com.mysql.jdbc.ReplicationDriver.

于 2016-03-19T00:06:49.450 回答