439 words

Drop the entire database, Change the schema

#1
2011-08-24 12:19

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

JFK
JFK's picture
Joined on 2011-08-24
User Post #1
#2
2011-08-24 17:32

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
ObjectDB - Fast Object Database for Java (JPA/JDO)
support
support's picture
Joined on 2010-05-03
User Post #557
#3
2011-08-24 18:45

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.

 

 

JFK
JFK's picture
Joined on 2011-08-24
User Post #2
#4
2011-08-26 11:47

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
ObjectDB - Fast Object Database for Java (JPA/JDO)
support
support's picture
Joined on 2010-05-03
User Post #572
#5
2011-10-07 21:07

where in the configuration file does this go?

 

dmoshal
dmoshal's picture
Joined on 2010-05-22
User Post #67
#6
2011-10-07 23:51

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)

dmoshal
dmoshal's picture
Joined on 2010-05-22
User Post #68
#7
2011-10-08 00:00

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.

 

 

 

dmoshal
dmoshal's picture
Joined on 2010-05-22
User Post #69
#8
2011-10-08 17:17

"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
ObjectDB - Fast Object Database for Java (JPA/JDO)
support
support's picture
Joined on 2010-05-03
User Post #653

Post Reply

Please read carefully the posting instructions - before posting to the ObjectDB website.

  • You may have to disable pop up blocking in order to use the toolbar (e.g. in Chrome).
  • Use ctrl + right click to open the browser context menu in the editing area (e.g. for using a browser spell checker).
  • To insert formatted lines (e.g. Java code, stack trace) - select a style in the toolbar and then insert the text in the new created block.
  • Avoid overflow of published source code examples by breaking long lines.
  • You may mark in paragraph code words (e.g. class names) with the code style (can be applied by ctrl + D).
  • Long stack traces (> 50 lines) and complex source examples (> 100 lines) should be posted as attachments.
Attachments:
Maximum file size: 32 MB
Cancel