How to manage read-only DB connections at an application level

谁说我不能喝 提交于 2019-12-05 06:16:41

问题


We are using Java/Spring/Ibatis/MySql. Is there a way with these technologies to manage read-only connections at an application level. I am looking to add an extra layer of safeguarding on top of having read-only MySql users. It would be nice if BasicDataSource or SqlMapClientTemplate provided configuration for read-only connections. Otherwise, it seems like I'm left to only MySql read users and enforcing interfaces with only read methods.

Thanks


回答1:


for example Connection#CreateStatement can takes parameters

statement = connection.createStatement
(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);



回答2:


BasicDataSource has a property called defaultReadOnly property to set it as readonly. You can use it.

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
    <property name="driverClassName" value="org.postgresql.Driver"/>
    <property name="url" value="jdbc:postgresql://${db.host}:5432/${db.name}"/>
    <property name="username" value="${db.username}"/>
    <property name="password" value="${db.password}"/>
    <property name="defaultReadOnly" value="true"/>
</bean>

http://commons.apache.org/dbcp/apidocs/org/apache/commons/dbcp/BasicDataSource.html




回答3:


Try to get the underlying java.sql.Connection object and setReadOnly(true).



来源:https://stackoverflow.com/questions/6220709/how-to-manage-read-only-db-connections-at-an-application-level

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!