setReadOnly

49 views
Skip to first unread message

Björn Raupach

unread,
May 16, 2018, 10:25:40 AM5/16/18
to mybatis-user
Hi there,

is there way to set setReadOnly on the connection? Ideally as an attribute in the statement in the Mapper XML file. Then we could cherry pick which statements are eligible for reading from the slave and which are not. So before the statement is executed, actually before a transaction happens, set setReadOnly(true), run the statement and then set setReadOnly(false). Since Connections are usually pooled it makes no sense to toggle readOnly for a longer scope.

Background: As far as I understand we can distribute load among our master mysql and his slaves (lol), but we need to set the setReadOnly(true) on the connection.

Any ideas?

kind regards
Björn

Björn Raupach

unread,
May 28, 2018, 3:38:30 AM5/28/18
to mybatis-user
Ok, guess I am the only one. lol.

Lets say I implement this. PR possible? Anyone else thinks this is a nice feature to add?

Guy Rouillier

unread,
May 29, 2018, 1:19:50 AM5/29/18
to mybatis-user
Both SqlSession and SqlSessionManager have a getConnection() method; you can then setReadOnly() on the returned Connection (standard JDBC method, nothing to do with MyBatis.)  This doesn't seem very secure, though.  If you really want to enforce readonly, you'd be better off creating a account in the database that only has SELECT privilege, and then creating a connection pool using those credentials.

--
Guy Rouillier
--
You received this message because you are subscribed to the Google Groups "mybatis-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mybatis-user...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Björn Raupach

unread,
May 29, 2018, 2:56:00 AM5/29/18
to mybati...@googlegroups.com
Hi Guy,

thanks for getting in touch. It is not about security. Just a way to distribute load. From my understanding the cluster sends connection with the readOnly set to a slave instead of the master.

True, getConnection from SqlSession is simple enough. Just have to make sure I set it back to false in the finally block. That should work for now. I just thought this is maybe a common pattern. Guess not.

Björn
Reply all
Reply to author
Forward
0 new messages