Menu

Unable to release file after closing connection

Help
Igor
2014-11-26
2014-11-26
  • Igor

    Igor - 2014-11-26

    Hello everyone,

    I am facing an issue when I open an Access 2010 file via creating the connection.
    Them I'm creating a statement and executing it. After I get the result set I'm closing it, closing statement and finally the connection.

    Even though everything seems to be closed and released, my Access file is still locked and the only thing that can release it is a server shutdown.

    My code example:

        public static void main(String[] args) throws Exception {
        String fileAbsolutePath = "D:\\CNA\\input\\service_usage.accdb";
        Connection conn = getConnection("jdbc:ucanaccess://" + fileAbsolutePath, null, null);
        Statement statement = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);
        statement.close();
        conn.close();
        System.out.println("The file is still locked :(");
    }
    

    As you can see, the file is getting locked even without the ResultSet.

    Does anyone know what could be missing in my code to release the file?

    P.S. the purpose of the service being implemented is to re-load the data from the Access DB (constantly changing).
    Since the file is locked, external tool is unable to overwrite it.

    Thanks in advance,
    Igor

     

    Last edit: Igor 2014-11-26
  • Igor

    Igor - 2014-11-26

    Hi everyone,

    I tried every single method on connection and statement objects.
    The solution came itself.
    Extra command is needed in order to fully release the resource from Ucanaccess perspective.

    ((UcanaccessConnection) conn).unloadDB();

    I hope this helps,
    Igor

     
  • Marco Amadei

    Marco Amadei - 2014-11-26

    Hi Igor,
    the same thing may be done without an explicit call to an UCanAccess implementation method,
    but using the singleconnection=true connection parameter setting:

    conn=DriverManager.getConnection("jdbc:ucanaccess://c:/pippo.mdb;singleconnection=true");

    UCanAccess can't close the resources it uses without this being explicited because of caching and efficiency needs.
    Cheers Marco

     

    Last edit: Marco Amadei 2014-11-26

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.