UserException: Attempt to open odb database file that is currently in use by another process

458 views Asked by At

I have an application which runs correctly in Netbeans IDE but after building it, it won't just run sometime, the app stop responding to event at a particular point. I was able to reproduce this issue while running the jar file in command line and I got this exception which I have it stack attached here

enter image description here

This what my data class that i use in interacting with object db is

public class DataClass {

private EntityManager em = null;
private EntityManagerFactory emf = null;
private long accNo;
public DataClass() {
    connectDatabase();
}

public DataClass(long accNo) {
    this.accNo=accNo;
    connectDatabase();
}

private void connectDatabase() {
    if (emf == null) {
        emf = Persistence.createEntityManagerFactory("Atm.odb");

    }
    if (em == null) {
        em = emf.createEntityManager();
    }

}

public Customer getAccount(long id) {
    connectDatabase();
    try {
        TypedQuery q = em.createQuery("SELECT cu FROM Customer cu where cu.id= :id", Customer.class);
        q.setParameter("id", id);
        Customer c=(Customer)q.getSingleResult();
        closeConnections();
        return c;
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}


public void closeConnections(){
    emf.close();
    em.close();
}
}

This I still don't really get how to work around after searching google. However this exception only happen after running the app the second time. If I'm runnig the app the first time after rebooting my system. Everything is ok but after closing the app and rerunning it the this issue occur. The app was built on javafx though

2

There are 2 answers

0
Alex On BEST ANSWER

You didn't close close your app in the first case. Use Task Manager and kill it. It may happen if some thread (usually GUI) is still running. Actually you should anticipate such behavior and make sure within Java app that it is closed properly - all threads are down.

0
ObjectDB On

As observed by Alex above, your first run has probably not finished.

Check also that you close the EntityManagerFactory to release the database file.

If you have to access the database from several processes concurrently then use client-server mode rather than embedded mode.