Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SQLException when calling setReadOnly #247

Closed
Synesso opened this issue Aug 7, 2017 · 6 comments
Closed

SQLException when calling setReadOnly #247

Synesso opened this issue Aug 7, 2017 · 6 comments
Labels
enhancement:JDBC Enhancement specific to the JDBC standard

Comments

@Synesso
Copy link

Synesso commented Aug 7, 2017

This is an unresolved bug (2014) migrated from bitbucket. I did not find it in github issues and it is still a problem.


Since version 3.7.15-M1, we see the following exception:

Exception in thread "main" java.sql.SQLException: Cannot change read-only flag after establishing a connection. Use SQLiteConfig#setReadOnly and QLiteConfig.createConnection().
at org.sqlite.SQLiteConnection.setReadOnly(SQLiteConnection.java:447)
at org.apache.commons.dbcp.DelegatingConnection.setReadOnly(DelegatingConnection.java:377)
at org.apache.commons.dbcp.PoolingDataSource$PoolGuardConnectionWrapper.setReadOnly(PoolingDataSource.java:338)
at scalikejdbc.DBConnection$class.readOnlySession(DB.scala:138)
at scalikejdbc.DB.readOnlySession(DB.scala:498)
...

Such behaviour differs from other JDBC drivers.

see also:
http://stackoverflow.com/questions/23390278/scalikejdbc-sqlite-cannot-change-read-only-flag-after-establishing-a-connecti

@gwenn
Copy link

gwenn commented Aug 11, 2017

It seems PRAGMA query_only may be used.

@spaudanjo
Copy link

Any plans to address this in the near future? :)

@shevek
Copy link
Contributor

shevek commented Feb 22, 2018

We hit this as well with HikariCP. HikariCP is explicitly setting readOnly to a value which it chooses, which may differ from the setting of the underlying DataSource. The solution was to tell HikariCP to leave it alone, in our case, by setting HCP's readOnly flag to the same as that of SQLite.

There is an argument to say that the result of an "advisory" (doc: "hint") call to sqlite should not be fatal. However, the doc on setReadOnly permits exceptions to be thrown under somewhat loosely defined circumstances ("if a database error occurs").

No strong conclusion about validity.

@timmc-bcov
Copy link

I ran into this at work, and the query_only pragma didn't work for me, I suppose because it interferes with prepared statements. (I couldn't set parameters since there were two statements in the string.) This workaround seems OK, though:

        PoolProperties props = new PoolProperties();
        props.setDriverClassName("org.sqlite.JDBC");
        props.setUrl("jdbc:sqlite:...");

        Properties extraProps = new Properties();
        extraProps.setProperty("open_mode", SQLiteOpenMode.READONLY.flag + "");
        props.setDbProperties(extraProps);
        // This line can be left in or removed; it no longer causes a problem
        // as long as the open_mode code is present.
        props.setDefaultReadOnly(true);

        return new DataSource(props);

@timmc-bcov
Copy link

This also worked (in 3.28.0), and was considerably simpler:

String path = ...
SQLiteConfig config = new SQLiteConfig();
config.setReadOnly(true);
return DriverManager.getConnection("jdbc:sqlite:" + path, config.toProperties());

@gotson
Copy link
Collaborator

gotson commented Jul 29, 2022

Would be fixed by #432 i suppose

@gotson gotson added the enhancement:JDBC Enhancement specific to the JDBC standard label Jul 29, 2022
@gotson gotson linked a pull request Aug 19, 2022 that will close this issue
@gotson gotson closed this as completed in 0ede2be Sep 9, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement:JDBC Enhancement specific to the JDBC standard
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants