stevebot
stevebot

Reputation: 24005

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

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

Upvotes: 3

Views: 7444

Answers (3)

suDocker
suDocker

Reputation: 8534

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

Upvotes: 2

Abdullah Jibaly
Abdullah Jibaly

Reputation: 54810

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

Upvotes: 1

mKorbel
mKorbel

Reputation: 109823

for example Connection#CreateStatement can takes parameters

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

Upvotes: 2

Related Questions