开发者

Open JdbcTemplate connection in read only mode?

is it possible to open a JdbcTemplat开发者_JS百科e connection in read only mode, so that I can't perform any changes to the underlying data source?


Use Spring Transactions and declare the transaction as readOnly. See http://static.springsource.org/spring/docs/2.5.6/reference/transaction.html#transaction-declarative-annotations


I use a helper method like this

private void setConnectionReadOnly(boolean readOnly) {
    try {
        jdbcTemplate.getDataSource().getConnection().setReadOnly(readOnly);
    } catch (SQLException e) {
        e.printStackTrace();
    }
}


I don't believe the JDBC connection API allows this.

You have two choices:

  1. GRANT appropriate permissions on the database level to only allow SELECT operations;
  2. Use Spring AOP and Security to intercept calls to write operations on the DAO and forbid them for certain roles.

The second choice is obviously more flexible and in the spirit of Spring's natural idiom.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜