Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

275
Views
setReadOnly not working on PostgreSQL Connection

I have a problem setting a read-only connection with a PostgreSQL database using JDBC.

I'm creating an application which loads queries to execute from a file, and I want to execute only SELECT (read-only) queries. Setting permissions on users who will run the application is not an option, so setting permissions at code level through the Connection.setReadOnly(boolean) method was the only option I could think of.

Now the problem is that I tried my application for an insert and delete query and they run normally, so my solution does not work.

Any tips on how to solve the problem?

Here below the simple code snippet which I'm testing:

public class Test{

public static void main(String[] args) {
    connectAndExecute();
}

public static void connectAndExecute() {
    try {
        Class.forName("org.postgresql.Driver");
    } catch (ClassNotFoundException e1) {
        e1.printStackTrace();
        return;
    }

    Connection connection;

    String connString = "jdbc:postgresql://localhost:5432/testdb";
    try {
        connection = DriverManager.getConnection(connString, "user", "password");
    } catch (SQLException e) {
        e.printStackTrace();
        return;
    }

    try {
        connection.setReadOnly(true);
        connection.createStatement().execute("delete from testtable");
        // connection.createStatement().execute("insert intotesttable(testfield1)values('test')");
    } catch (SQLException e) {
        e.printStackTrace();
    }
}

and this is the JDBC driver I'm using

<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>42.2.10</version>

PostgreSQL version: 11.6

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

Problem seems related to a bug of the driver.

These two workaround both worked for me:

  • proposed by kayaman:

executing first the sql command

SET SESSION CHARACTERISTICS AS TRANSACTION READ ONLY
  • proposed by a_horse_with_no_name:

disabling auto-commit makes the setReadOnly call effective

Connection.setAutoCommit(false);
Connection.setReadOnly(true);
over 4 years ago · Santiago Trujillo Report

0

Just to be clear : the specification for setReadOnly method does not aim to prevent from updating the database.

As the Java's API says :

https://docs.oracle.com/javase/8/docs/api/java/sql/Connection.html#setReadOnly-boolean-

"Puts this connection in read-only mode as a hint to the driver to enable database optimizations"

So it is not a good idea to rely on this method in order to secure anything related to the need of having readonly transactions :)

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!