Failed to delete database file after closing UCanAccess connection

I have used UCanAccess to connect to temporary read access files, after which I want to delete those temporary files. However, I always get file.delete()=false

.

Here is my code

public class JAccess {
    static Connection conn;
    static Statement stat;

    public static void connect(String DBFilename) {
        try {used ucanaccess to connect temporary access files
            Class.forName("net.ucanaccess.jdbc.UcanaccessDriver");
            String DataSource = "jdbc:ucanaccess://" + DBFilename;
            String user = "user";
            String pw = "pw";
            conn = DriverManager.getConnection(DataSource, user, pw);
            stat = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY);
        } catch (Exception e) {
            System.out.println(e);
        }
    }

    public static void disconnect() {
       try {
            stat.close();
            conn.close();
        } catch (Exception e) {
            System.out.println(e);
        }
        stat = null;
        conn = null;
    }
}


public void SelectTable(String DBFilename) throws SQLException {
    JAccess.connect(DBFilename);
    JAccess.disconnect();
}

public static void main(String[] args) throws SQLException {
.........
    SelectTable(DBFilename);
    File f = new File(DBFilename);
    f.delete();

      

Why f.delete()

doesn't it work?

+3


source to share


1 answer


By default, UCanAccess opens a connection to the database file using immediatelyReleaseResources=false

. Under these circumstances, when java.sql.Connection

closed, UCanAccess will actually save the file descriptor and HSQLDB resources for a short period of time if the application wants to reestablish a connection to the database. For example, this can happen with an ORM that opens and closes database connections for every operation. However, this also means that the temporary database file cannot be deleted immediately after the connection is closed, as UCanAccess still has the file open.



If we specify a immediatelyReleaseResources=true

connection in the URL link, UCanAccess will close the file descriptor and release the HSQLDB resources as soon as possible after closing java.sql.Connection

. This is often the preferred behavior for ETL jobs and other tasks that require a one-time connection to the database file.

+1


source







All Articles