Skip to main content

Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » EclipseLink » Problem removing directory with database
Problem removing directory with database [message #1326370] Thu, 01 May 2014 18:14 Go to next message
Rainer H. is currently offline Rainer H.Friend
Messages: 21
Registered: July 2009
Junior Member

I'm using EclipseLink 2.5.1 and Apache Derby I wrote a simple JUnit test class. At the end I close the EntitiyManager and the EntityManagerFactory and try to delete the Directory containing the database. But this fails because a file could not be deleted. For deletion of the Directory I use Apache Common IO because it has a simple method for deleting non-empty Directory. Also log4j2 is used for logging Messages.

In my example I've make a Workaround which only prints an error message. But I want to delete the directory after the tests because they were only used for tests.

Any idea how to reach the Goal?

Here my code:
import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.TypedQuery;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.junit.AfterClass;
import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Test;

import rh.jpa.entities.ToDo;

 * Example from
 * (modified to use JUnit)
public final class JPAexample {
	/** The one and only logging instance of this class. */
	private static final Logger LOGGER = LogManager.getLogger(JPAexample.class);
	 * Name of the persistence unit.<br>
	 * (Have to be the same as in persistence.xml)
	private static final String PERSISTENCE_UNIT_NAME = "todos"; //$NON-NLS-1$
	/** The entity manager factory. */
	private static EntityManagerFactory factory;
	/** An entity manager. Used in all tests. */
	private static EntityManager em;

	/** Constructor requested by checkstyle. */
	public JPAexample() {


	/** Removing the database. */
	private static  void removeDatabase() {
		final File directory = new File("databases"); //$NON-NLS-1$

		try {
		} catch (final IOException exception) {
			// FIXME often the file databases\simpleDB\seg0\cf0.dat will not be deleted: why, is it open

	 * Do some preparations for all tests.<br>
	 * (here: create an EntityManagerFactory)
	 * @throws Exception if any error occurs(e.g. EntityManagerFactory could not created
	public static void setUpBeforeClass() throws Exception {


		JPAexample.factory = Persistence.createEntityManagerFactory(JPAexample.PERSISTENCE_UNIT_NAME);
		JPAexample.em = JPAexample.factory.createEntityManager();


	 * Do some cleanup after the tests. (e.g. remove created database)
	 * @throws Exception if any error occurs
	public static void tearDownAfterClass() throws Exception {

		JPAexample.em = null;
		JPAexample.factory = null;


	/** Checks whether the initialization in <code>setUpBeforeClass()</code> was successful. */
	public void initialize() {

		Assert.assertNotNull("EntityManagerFactory not initialized", JPAexample.factory); //$NON-NLS-1$

	 * Read the existing ToDo's from the database.<br>
	 * No special tests are defined. (Failures cause exceptions.)
	public void read() {
		// read the existing entries and write to the logger
		final TypedQuery<ToDo> q = JPAexample.em.createQuery("select t from ToDo t", ToDo.class); //$NON-NLS-1$
		final List<ToDo> todoList = q.getResultList();

		// JAVA8 todoList.forEach(element -> JPA_3_1_7.LOGGER.debug(element));
		for (final ToDo todo : todoList) {

		JPAexample.LOGGER.debug("size: {}", Integer.valueOf(todoList.size())); //$NON-NLS-1$

	 * Create a new ToDo and save it in the database.<br>
	 * No special tests are defined. (Failures cause exceptions.)
	public void write() {

		// create new todo

		final ToDo todo = new ToDo();

		todo.setSummary("test: summary"); //$NON-NLS-1$
		todo.setDescription("test: description"); //$NON-NLS-1$


Re: Problem removing directory with database [message #1328327 is a reply to message #1326370] Fri, 02 May 2014 16:40 Go to previous message
Shaun Smith is currently offline Shaun SmithFriend
Messages: 197
Registered: July 2009
Senior Member
For testing I use the embedded Derby and use Maven to clean up the database file by placing it in the target folder. Look at the pom.xml in this example:


This doesn't exactly answer your question but I hope it helps!


[Updated on: Fri, 02 May 2014 16:40]

Report message to a moderator

Previous Topic:Unique constraints in unidirectional one to one relationship
Next Topic:Unmapped Columns in ResultSet
Goto Forum:

Current Time: Sun May 27 08:16:25 GMT 2018

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

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

Back to the top