Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » EMF "Technology" (Ecore Tools, EMFatic, etc)  » [Teneo] Delete Database for Unit Tests
[Teneo] Delete Database for Unit Tests [message #1228470] Tue, 07 January 2014 10:01 Go to next message
Jürgen Weinberger is currently offline Jürgen WeinbergerFriend
Messages: 42
Registered: August 2012
Member
Hi!

I am very new to Teneo and am currently playing around with it.
I wrote some Unit Tests where i always want to start with a clean database. The database is file based so i thought i just remove the database directory at the setUp in my unit tests.

This doesn't work because some process seems to use the .log file and this leads to an IOException (java.io.IOException: Unable to delete file: c:\tmp\hsqldb.log).

My question now is how do i completly "shut down" the database so that no process uses this file anymore. I already tried it in the tearDown method but had no success. Here is some of my code:

	private final static String dbFilePath = "c:/tmp/";
	private final static Properties hibernateProperties = new Properties();
	static {
		hibernateProperties.setProperty(Environment.DRIVER, "org.hsqldb.jdbcDriver");
		hibernateProperties.setProperty(Environment.USER, "sa");
		hibernateProperties.setProperty(Environment.URL, "jdbc:hsqldb:file:"+dbFilePath+"hsqldb");
		hibernateProperties.setProperty(Environment.PASS, "");
		hibernateProperties.setProperty(Environment.DIALECT, org.hibernate.dialect.HSQLDialect.class.getName());
		hibernateProperties.setProperty(PersistenceOptions.CASCADE_POLICY_ON_NON_CONTAINMENT, "CREATE,REFRESH,PERSIST,MERGE");
		hibernateProperties.setProperty(PersistenceOptions.INHERITANCE_MAPPING, "JOINED");
	}


	@Before
	public void setup() {
		cleanUp();
		String dataStoreName = "LocationStore";

		adapterFactory = new ComposedAdapterFactory();
		domain = new AdapterFactoryEditingDomain(
				adapterFactory, new BasicCommandStack());
		
		hbDataStore = HbHelper.INSTANCE.createRegisterDataStore(dataStoreName);
		hbDataStore.setDataStoreProperties(hibernateProperties);
		hbDataStore.setEPackages(new EPackage[] {LocationPackage.eINSTANCE});
		hbDataStore.initialize();
		getLogger().debug(hbDataStore.getMappingXML());		
		
		sc = new SessionController();
		sc.setHbDataStore(hbDataStore);		 
		SessionController.registerSessionController("testsc", sc);

		String uriStr = "hibernate://?" + HibernateResource.SESSION_CONTROLLER_PARAM + "=" + "testsc";
		resource = domain.getResourceSet().createResource(URI.createURI(uriStr));
		
		locationStore = getLocationStore();
		
		createDefault();
	}
	
	@After
	public void tearDown() {
		resource.unload();
		resource = null;
		hbDataStore.close();
		
		SessionController.deRegisterSessionController("testsc");
		sc.getSessionWrapper().close();
		sc = null;
		
		HbHelper.INSTANCE.closeAll();
		HbHelper.INSTANCE.deRegisterDataStore(hbDataStore);
		hbDataStore = null;
		
		try {
			Thread.sleep(5000);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public void cleanUp() {
		try {
			FileUtils.deleteDirectory(new File(dbFilePath));
		} catch (IOException e) {
			e.printStackTrace();
		}
	}


What did i miss ?

Thanks in advanced and best regards
Weinma
Re: [Teneo] Delete Database for Unit Tests [message #1228565 is a reply to message #1228470] Tue, 07 January 2014 14:03 Go to previous messageGo to next message
Martin Taal is currently offline Martin TaalFriend
Messages: 5468
Registered: July 2009
Senior Member
Hi Jurgen,
For Teneo I do the same (see the drop database done there):
http://git.eclipse.org/c/teneo/org.eclipse.emf.teneo.git/tree/tests/org.eclipse.emf.teneo.commontest/src/org/eclipse/emf/teneo/test/stores/HsqldbTestDatabaseAdapter.java

But I am on linux/ubuntu which makes it apparently possible to do this. So not sure if/what other ways there are to
delete a db for hsqldb...

gr. Martin

On 01/07/2014 11:01 AM, Jürgen Weinberger wrote:
> Hi!
>
> I am very new to Teneo and am currently playing around with it. I wrote some Unit Tests where i always want to start
> with a clean database. The database is file based so i thought i just remove the database directory at the setUp in my
> unit tests.
>
> This doesn't work because some process seems to use the .log file and this leads to an IOException (java.io.IOException:
> Unable to delete file: c:\tmp\hsqldb.log).
>
> My question now is how do i completly "shut down" the database so that no process uses this file anymore. I already
> tried it in the tearDown method but had no success. Here is some of my code:
>
>
> private final static String dbFilePath = "c:/tmp/";
> private final static Properties hibernateProperties = new Properties();
> static {
> hibernateProperties.setProperty(Environment.DRIVER, "org.hsqldb.jdbcDriver");
> hibernateProperties.setProperty(Environment.USER, "sa");
> hibernateProperties.setProperty(Environment.URL, "jdbc:hsqldb:file:"+dbFilePath+"hsqldb");
> hibernateProperties.setProperty(Environment.PASS, "");
> hibernateProperties.setProperty(Environment.DIALECT, org.hibernate.dialect.HSQLDialect.class.getName());
> hibernateProperties.setProperty(PersistenceOptions.CASCADE_POLICY_ON_NON_CONTAINMENT,
> "CREATE,REFRESH,PERSIST,MERGE");
> hibernateProperties.setProperty(PersistenceOptions.INHERITANCE_MAPPING, "JOINED");
> }
>
>
> @Before
> public void setup() {
> cleanUp();
> String dataStoreName = "LocationStore";
>
> adapterFactory = new ComposedAdapterFactory();
> domain = new AdapterFactoryEditingDomain(
> adapterFactory, new BasicCommandStack());
>
> hbDataStore = HbHelper.INSTANCE.createRegisterDataStore(dataStoreName);
> hbDataStore.setDataStoreProperties(hibernateProperties);
> hbDataStore.setEPackages(new EPackage[] {LocationPackage.eINSTANCE});
> hbDataStore.initialize();
> getLogger().debug(hbDataStore.getMappingXML());
>
> sc = new SessionController();
> sc.setHbDataStore(hbDataStore); SessionController.registerSessionController("testsc", sc);
>
> String uriStr = "hibernate://?" + HibernateResource.SESSION_CONTROLLER_PARAM + "=" + "testsc";
> resource = domain.getResourceSet().createResource(URI.createURI(uriStr));
>
> locationStore = getLocationStore();
>
> createDefault();
> }
>
> @After
> public void tearDown() {
> resource.unload();
> resource = null;
> hbDataStore.close();
>
> SessionController.deRegisterSessionController("testsc");
> sc.getSessionWrapper().close();
> sc = null;
>
> HbHelper.INSTANCE.closeAll();
> HbHelper.INSTANCE.deRegisterDataStore(hbDataStore);
> hbDataStore = null;
>
> try {
> Thread.sleep(5000);
> } catch (InterruptedException e) {
> // TODO Auto-generated catch block
> e.printStackTrace();
> }
> }
>
> public void cleanUp() {
> try {
> FileUtils.deleteDirectory(new File(dbFilePath));
> } catch (IOException e) {
> e.printStackTrace();
> }
> }
>
>
> What did i miss ?
>
> Thanks in advanced and best regards
> Weinma


--

With Regards, Martin Taal

Springsite/Elver.org
Office: Hardwareweg 4, 3821 BV Amersfoort
Postal: Nassaulaan 7, 3941 EC Doorn
The Netherlands
Cell: +31 (0)6 288 48 943
Tel: +31 (0)84 420 2397
Fax: +31 (0)84 225 9307
Mail: mtaal@xxxxxxxx - mtaal@xxxxxxxx
Web: www.springsite.com - www.elver.org
Re: [Teneo] Delete Database for Unit Tests [message #1228859 is a reply to message #1228470] Wed, 08 January 2014 08:24 Go to previous messageGo to next message
Jürgen Weinberger is currently offline Jürgen WeinbergerFriend
Messages: 42
Registered: August 2012
Member
Hi!

Thanks for the answer!
The thing is that the first time the delete works but as soon i ran through the first test it doesn't anymore. So i think i don't clean the connection the right way or maybe forgot something to close.

I also inserted a break point before the delete and tried to remove it manually in the Explorer, the same result. Some process is using this file.

How do you close the connection?

Here is my code:
	public void shutDown() {
		if(resource != null) {
			resource.unload();
			resource = null;
		}

		if(sessionController != null) {
			SessionController.deRegisterSessionController(SESSIONCONTROLLER_NAME);
			sessionController.getSessionWrapper().close();
			sessionController = null;
		}

		if(hbDataStore != null) {
			hbDataStore.close();
			HbHelper.INSTANCE.closeAll();
			HbHelper.INSTANCE.deRegisterDataStore(hbDataStore);
			hbDataStore = null;
		}
	}


Thanks again and best regards
Weinma
Re: [Teneo] Delete Database for Unit Tests [message #1228864 is a reply to message #1228859] Wed, 08 January 2014 08:33 Go to previous messageGo to next message
Martin Taal is currently offline Martin TaalFriend
Messages: 5468
Registered: July 2009
Senior Member
Hi Jurgen,
Yes I do the same, I think it is more that in windows I think the checks on file-holds are more eager than in Linux/Unix.

Did you see this thread:
http://stackoverflow.com/questions/4990410/how-can-i-wipe-data-from-my-hsqldb-after-every-test

(I did not check further, but I guess there must be more people having the same question on the net).

gr. Martin

On 01/08/2014 09:24 AM, Jürgen Weinberger wrote:
> Hi!
>
> Thanks for the answer! The thing is that the first time the delete works but as soon i ran through the first test it
> doesn't anymore. So i think i don't clean the connection the right way or maybe forgot something to close.
>
> I also inserted a break point before the delete and tried to remove it manually in the Explorer, the same result. Some
> process is using this file.
>
> How do you close the connection?
>
> Here is my code:
> public void shutDown() {
> if(resource != null) {
> resource.unload();
> resource = null;
> }
>
> if(sessionController != null) {
> SessionController.deRegisterSessionController(SESSIONCONTROLLER_NAME);
> sessionController.getSessionWrapper().close();
> sessionController = null;
> }
>
> if(hbDataStore != null) {
> hbDataStore.close();
> HbHelper.INSTANCE.closeAll();
> HbHelper.INSTANCE.deRegisterDataStore(hbDataStore);
> hbDataStore = null;
> }
> }
>
> Thanks again and best regards
> Weinma


--

With Regards, Martin Taal

Springsite/Elver.org
Office: Hardwareweg 4, 3821 BV Amersfoort
Postal: Nassaulaan 7, 3941 EC Doorn
The Netherlands
Cell: +31 (0)6 288 48 943
Tel: +31 (0)84 420 2397
Fax: +31 (0)84 225 9307
Mail: mtaal@xxxxxxxx - mtaal@xxxxxxxx
Web: www.springsite.com - www.elver.org
Re: [Teneo] Delete Database for Unit Tests [message #1229344 is a reply to message #1228470] Thu, 09 January 2014 08:43 Go to previous message
Jürgen Weinberger is currently offline Jürgen WeinbergerFriend
Messages: 42
Registered: August 2012
Member
Hi!

It's me again and first of all Thanks for the answer and support!

I played around a little bit more and since you mentioned it may be a hsqldb problem i tried the h2 database. There the remove of the database file works fine but when my second test run tries to write to a newly created database there comes a new exception:

org.hibernate.StaleObjectStateException: Row was updated or deleted by another transaction (or unsaved-value mapping was incorrect): 
...


So again, i think i may forgot something to close or deinitialise!
Do you or anybody else have an idea where this problem could come from.
In the evening i will try it on my os x machine, maybe everthing is ok there.

Thank again and
best regards
Previous Topic:[Teneo] Delete Model and unset all CrossReferences
Next Topic:[EMFStore] Running EMFStore Client in a web app
Goto Forum:
  


Current Time: Tue Mar 19 10:31:11 GMT 2024

Powered by FUDForum. Page generated in 0.02613 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top