Skip to content
Advertisement

How close possibility to add new data in H2?

I have a problem. After adding some data to database, I need to set read-only mode for whole DB. What is the easiest way to do that using h2 embedded DB with driver manager (jdbc)?

Class.forName("org.h2.Driver").newInstance();
        con = DriverManager.getConnection("jdbc:h2:" + "./"+dbName, "hereIsUser", "hereIsPassword");

Advertisement

Answer

  1. You can open the whole database in read-only mode by appending ;ACCESS_MODE_DATA=r to the JDBC URL ("jdbc:h2:" + "./" + dbName + ";ACCESS_MODE_DATA=r" in your case.

  2. You can create a separate user (CREATE USER userName PASSWORD 'some_password') and give it only the SELECT grants (GRANT SELECT ON SCHEMA nameOfYourSchema TO userName) and use that user.

Advertisement