ObjectDB ObjectDB

Drop the entire database, Change the schema

#1

I am trying to port and example app to ObjectDb. As part of the tests I drop the database and reload it with the new schema automatically between test cycles.

Is there a simple way to do this with ObjectDb, or is the only way to delete the database file everytime?

Thanks

edit
delete
#2

You can delete the database file (and associated files if any) using your code, e.g.

    new File(path).delete();

However, this could be a useful ObjectDB feature.

Maybe adding ;drop as a url parameter:

    EntityManagerFactory emf =
        Persistence.createEntityManagerFactory("myDbFile.odb;drop");

What do you think? Any other suggestions?

ObjectDB Support
edit
delete
#3

I was porting from Eclipselink ;-)

As you doubtless know it has the following property ;

<property name="eclipselink.ddl-generation" value="drop-and-create-tables"/>

So I have two PUs configured which are the same apart from this property, I use one to 'drop' the database and the other for the normal stuff.  

On the other hand the URL would work just as well for me and is in some senses clearer and more transparent...

I used the File technique already, but it means I have to stop the container to be sure the DB file is not locked - I do this anyway in the tests ...

 

Thanks.

 

 

edit
delete
#4

Build 2.2.9_05 adds this new feature, which should be very useful in tests.

You can override an existing database file by adding ;drop to the url:

    EntityManagerFactory emf =
        Persistence.createEntityManagerFactory("objectdb:myDbFile.tmp;drop");

A new configuration line was added to specify database file name extensions for which drop is enabled:

    <extensions drop="temp,tmp" />

This is a defensive restriction to avoid deleting databases by mistake, but the conventional odb extension could also be added to that configuration setting.

In client-server mode this configuration should be set on the server side.

ObjectDB Support
edit
delete
#5

where in the configuration file does this go?

 

edit
delete
#6

figured out that it goes in <database>

So, the following creates in a database named test.odb which isn't dropable (Expected):
emf = Persistence.createEntityManagerFactory ("test.odb;drop");

BUT changing the url as follows:

emf = Persistence.createEntityManagerFactory ("test.tmp;drop");

results in this:

Exception in thread "main" [ObjectDB 2.3.1_03] javax.persistence.PersistenceException
Persistence unit 'test.tmp;drop' is not found (by sun.misc.Launcher$AppClassLoader) (error 222)
at com.objectdb.jpa.Provider.createEntityManagerFactory(Provider.java:87)
at com.objectdb.jpa.Provider.createEntityManagerFactory(Provider.java:32)
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:78)
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:54)
at javax.persistence.Persistence$createEntityManagerFactory.call(Unknown Source)
at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:42)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:108)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:116)

edit
delete
#7

and the work around seems to be:

<extensions drop="odb" />

which frankly suits me fine, the 'drop' on the url ought to be enough safety,

and keeping the .odb allows the explorer to find the database more easily.

 

 

 

edit
delete
#8

"test.tmp;drop" is not a valid url but "objectdb:test.tmp;drop" is valid.

You may omit the objectdb: protocol prefix only if the database extension is either odb or objectdb (otherwise when JPA's createEntityManagerFactory method asks ObjectDB if it knows that persistence unit - ObjectDB cannot identify it as related to ObjectDB).

But your solution of adding support of drop to the odb extension is fine.

See the manual for more information on database urls and this configuration.

ObjectDB Support
edit
delete

Reply

To post on this website please sign in.