0

I am developing an application which would fetch data from an Oracle database. It is a reporting app, no insert, update, delete is required. I know that I can set privilege for the database user to only select. But can I configure the database connection in Play application to make it readonly? I would hate to make any change or create a new user for my app. I understand that Play Framework 2.1.3 uses BoneCp for data connectivity.

Edit

I am using Java version of Play!

Khalid Saifullah
  • 747
  • 2
  • 8
  • 21

1 Answers1

0

java.sql.Connection has a method called setReadOnly, which does exactly what it says:

DB.withConnection{ implicit connection =>
    connection.setReadOnly(true)
    ...
}

An exception will be thrown when trying to execute write queries. You could also wrap DB.withConnection to clean things up across the application:

def withReadOnlyConnection[T](block: => T): T = {
    DB.withConnection{ implicit connection =>
       connection.setReadOnly(true)
       block(connection)
    }
}

And use it just like DB.withConnection:

def listThings: List[Stuff] = {
     withReadOnlyConnection{ implicit connection =>
         SQL(...)
    }
}

Of course, it would still be better to have a separate database user with the limited permissions.


Edit:

For Java it's not going to be as pretty, but the same thing applies:

Connection connection = DB.getConnection();
connection.setReadOnly(true);

// Do something with this connection.
Michael Zajac
  • 55,144
  • 7
  • 113
  • 138
  • Thanks a lot for your answer. But I am using Java version, sorry that I forgot to mention. I have updated my question. – Khalid Saifullah Jun 17 '14 at 11:56
  • That doesn't really change anything. `java.sql.Connection` is the same object used for both Scala and Java, so you can do the same thing. – Michael Zajac Jun 17 '14 at 12:17