Setting readOnly on database connections

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

Setting readOnly on database connections

Björn Raupach
Hi there,

this is a follow up on a question I asked two years ago. How time flies!

We are using Amazon Aurora Database with the MariaDB Connector/J. This jdbc driver has a nice feature to distribute load on readers. All you need to do is setReadOnly(true) on the JDBC connection. We use this a lot, but unfortunately this yields to ugly code. Maybe someone has an idea to improve things?

Here is an example, how parts of our code looks like:

public List<Foo> selectAll() {
  Connection con = null;
  try (SqlSession session = sqlSessionFactory.openSession()) {
    con = session.getConnection();
    con.setReadOnly(true);
    FooMapper mapper = session.getMapper(FooMapper.class);
    return mapper.selectAll();
  } catch (SQLException e) {
    throw new RuntimeException(e);
  } finally {
    if (con != null) {
      try {
        con.setReadOnly(false);
      } catch (SQLException ignored) {}
    }
}
}

This is rather ugly and far from the usual, clean pattern MyBatis provides. Does anyone know a better way to approach this?

kind regards and thank you all for keeping MyBatis up and running!
Björn

--
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 [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/mybatis-user/a8e0499f-adc7-430e-a4ad-a50c71d40d1dn%40googlegroups.com.