Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java + sqlite: how to open database as read-only?

I have a large database that I want to open in read-only mode... I'm using SQLiteJDBC and am not sure what to use to get it to work as read-only.

Can anyone help?

like image 878
Jason S Avatar asked Jan 01 '11 14:01

Jason S


4 Answers

Test demonstrate how to set connection to be read-only:

SQLiteConfig config = new SQLiteConfig();

config.setReadOnly(true); 

Connection conn = DriverManager.getConnection("jdbc:sqlite:sample.db",
config.toProperties());
like image 95
deadlock Avatar answered Nov 13 '22 17:11

deadlock


Try this.

Properties config = new Properties();
config.setProperty("open_mode", "1");  //1 == readonly
Connection conn = DriverManager.getConnection("jdbc:sqlite:sample.db", config);
like image 24
szcoder Avatar answered Nov 13 '22 19:11

szcoder


Just want to add that when using HikariCP, you need two datasource properties:

readOnly=true
dataSource.open_mode=1
like image 20
rustyx Avatar answered Nov 13 '22 17:11

rustyx


I had a slightly different scenario, but the same issue. If you have a spring boot service using Hikari you need to provide 2 configuration settings in your application.properties

spring.datasource.url=jdbc:sqlite:myDatabase.db?open_mode=1
spring.datasource.hikari.read-only=true
like image 21
hecko84 Avatar answered Nov 13 '22 17:11

hecko84