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:
- GRANT appropriate permissions on the database level to only allow SELECT operations;
- 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.
精彩评论