Unable to delete the .odb file programatically

#1

Hi,

I created the test.odb file, read the contents and now trying the delete the odb file using objectd2.x.

I see the folder there are two files created test.odb and test.odb$.

Using the following code to delte the odb files

    PersistenceManager pm = (PersistenceManager) getPM();
    if (pm.currentTransaction().isActive()) {
        pm.currentTransaction().rollback();
    }
    if (!pm.isClosed()) {
        pm.evictAll();
        pm.close();
    }

  After executing the above code, when i am trying to delete the file using the followng code

    File[] files = parentDir.listFiles();

    for (File temp : files ) {
        boolean deleted = file.delete();  // getting false value for deleted boolean
    }

When i am calling pm.close(), should not the test.odb$ file automatically be deleted. Can you please let me know if i need to do any thing more. The same was working with objectdb 1.x jar. I upgraded to object db 2.X and now i am unable to delete the odb files.

Note: i am using the default objectdb.conf file.

 

Thanks & regards,

Binit Bhaskar

 

#2

In ObjectDB 2.x you have to close the PersistenceManagerFactory as well to close the database file (and release the lock) .

ObjectDB Support
ObjectDB - Fast Object Database for Java (JPA/JDO)
#3

I tried closing persistanceManagerFactory too but no luck :(

Code used for creating PersistanceManager from PersistanceFactory

    PersistenceManagerFactory pmf = JDOHelper.getPersistenceManagerFactory(getProperties(physicalPath,objectName), JDOHelper.class.getClassLoader());
    pmf.setMultithreaded(true);

    PersistenceManager pm = pmf.getPersistenceManager();
    if (codeLogger.isDebugEnabled()) {
        codeLogger.debug("Created a new PM ("+pm+")");
    }
    try {
        pm.currentTransaction().begin();
        pm.currentTransaction().commit();
    } catch(JDOException e){
        codeLogger.error(e, e);
    }

    return pm;

 

Code for releasing the lock so that database files can be deleted

    PersistenceManager pm = getPM();
    if (pm.currentTransaction().isActive()) {
        pm.currentTransaction().rollback();
    }
    if (!pm.isClosed()) {
        pm.evictAll();
        pm.close();
    }

    PersistenceManagerFactory pmfactory = JDOHelper.getPersistenceManagerFactory(getProperties(fromPath,objectName), JDOHelper.class.getClassLoader());
    if (pmfactory != null && !pmfactory.isClosed())
    {
        pmfactory.close();
        pmfactory = null;    
    }

Please let me know if you need any more info. When the test.odb$ (temp database file) will be deleted ? After closing the PersistenceManagerFactory also, I see tesst.odb$ file in the file system.

#4

Hi,

I just found that 3 ODB-FileWriter threads are running. I dont know what this thread do. May be, this thread is having the lock and that is the reason, we are unable to delete the odb file.

I have totally 3 odb files in the file system.

Thanks,

Binit Bhaskar

#5

These 3 running threads indicate that there are 3 open databases.

Verify that you have only one PersistenceManagerFactory instance per database and it is closed.

The problem may indicate that your application creates multiple PersistenceManagerFactory instances and some of them are not closed.

ObjectDB Support
ObjectDB - Fast Object Database for Java (JPA/JDO)
#6

Thanks for the reply. I got it working by closing all the pmf


Post Reply

To post a reply and/or subscribe to update notifications - please login