'sqlite cannot connect to database error: java.lang.UnsatisfiedLinkError: org.sqlite.core.NativeDB._open_utf8([BI)V

I get the following error: I have this code I have tried several things going before I posted this, I determined that this was the source error it did not connect to database. I am running this code in tomcat as a backend database for a microservice. It is trying to connect to my temp directory for some reason it does not have access. I have use

System.setProperty("java.io.tmpdir", "C:\\Dev\\temp");

it does write the dll file to this location. but it does not connect to database. It has to create the database first if it is not there

Caused by: **java.lang.UnsatisfiedLinkError: org.sqlite.core.NativeDB._open_utf8([BI)V**
at org.sqlite.core.NativeDB._open_utf8(Native Method)
at org.sqlite.core.NativeDB._open(NativeDB.java:78)
at org.sqlite.core.DB.open(DB.java:202)
at org.sqlite.SQLiteConnection.open(SQLiteConnection.java:243)
at org.sqlite.SQLiteConnection.<init>(SQLiteConnection.java:61)
at org.sqlite.jdbc3.JDBC3Connection.<init>(JDBC3Connection.java:28)
at org.sqlite.jdbc4.JDBC4Connection.<init>(JDBC4Connection.java:21)
at org.sqlite.JDBC.createConnection(JDBC.java:115)
at org.sqlite.JDBC.connect(JDBC.java:90)
at java.sql.DriverManager.getConnection(DriverManager.java:664)
at java.sql.DriverManager.getConnection(DriverManager.java:270)java.lang.UnsatisfiedLinkError: org.sqlite.core.NativeDB._open(Ljava/lang/String;I)V

When I try to connect to a sqlite database I get the above error

Failed to load native library:sqlite-3.31.1-0d2f436c-54b0-48ed-8a6b-044f659f50d6-sqlitejdbc.dll. osinfo: Windows/x86_64
java.lang.UnsatisfiedLinkError: C:\Users\home\AppData\Local\Temp\sqlite-3.31.1-0d2f436c-54b0-48ed-8a6b-044f659f50d6-sqlitejdbc.dll: Access is denied

My base code is:

public class Database {

private static Database instance = new Database();
 Connection conn = null;
 Statement stmt = null;

private Database() { 
    getInstance();
}

public static Database getInstance() {
    return instance;
}

public Connection dbConnection() {

    String createTableQuery = "CREATE TABLE IF NOT EXIST fileBlock (pid not null, filename not null, PRIMARY KEY(pid,filename))";
    try {
        //create database if not exist and then connect
        Class.forName("org.sqlite.JDBC");
        conn = DriverManager.getConnection("jdbc:sqlite:blockedFiles.db");
        //verify table exist if not create it
        stmt = conn.createStatement();
        stmt.execute(createTableQuery);
        return conn;            
    }
    catch(Exception e){
        return null;
    }
}

public void closeConnection() {
    try {
        conn.close();
    }
    catch(Exception e) {}   
}

}

My implementation is

 Connection conn = Database.getInstance().dbConnection();
    if(conn != null ) {
        System.out.println("true");         
    }
    else {
        System.out.println("false");
    }       
    Database.getInstance().closeConnection();


Solution 1:[1]

I found out that this is mostly a permission issue on the local machine.

How I was able to work around it was to use the SQLiteConfig Class.

This Class allows you to pass configuation options. The settings that I added to the database connection class:

 SQLiteConfig config = new SQLiteConfig();
  // config.setCacheSize(cache_size);
   config.setBusyTimeout(30000);
   config.setSynchronous(SynchronousMode.NORMAL);
   config.setTempStore(TempStore.MEMORY);
   config.setTempStoreDirectory("some path, I used same as java.io.tmpdir");
   config.setJournalMode(JournalMode.MEMORY);
 //create database if not exist and then connect
    Class.forName("org.sqlite.JDBC");
    conn = DriverManager.getConnection("jdbc:sqlite:blockedFiles.db", config.toProperties());
    //verify table exist if not create it
    stmt = conn.createStatement();
    stmt.execute(createTableQuery);
    return conn;

Before you connect to the database, I told it to load the drivers in memory and the log file (WAL) is also in memory.

Solution 2:[2]

Specify your own tmp path with -Dorg.sqlite.tmpdir like:

java -jar -Dorg.sqlite.tmpdir=./tmp {your-jar-file}.jar 

Reference: https://github.com/lmenezes/cerebro/issues/121

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 Joe Mwa