net.craftforge.commons.database.memory
Class InMemoryDb

java.lang.Object
  extended by java.lang.Thread
      extended by net.craftforge.commons.database.memory.InMemoryDb
All Implemented Interfaces:
Runnable

public class InMemoryDb
extends Thread

An in-memory db is a wrapper for holding derby in-memory database instances. An in-memory db will be destroyed after a certain timeout has been reached, so it acts as temporary database which is only used for a certain time. The timeout is reset to its initial value with entity manager factory call. So an in-memory database keeps alive as long as it is frequently used.

Since:
08.08.11
Author:
Christian Bick

Nested Class Summary
 
Nested classes/interfaces inherited from class java.lang.Thread
Thread.State, Thread.UncaughtExceptionHandler
 
Field Summary
static long DEFAULT_INITIAL_TIMEOUT
          Default maximum idle time before a database is dropped: 1 hour.
static long DEFAULT_TIMEOUT_CHECK_INTERVAL
          Default update interval to check if the database should be dropped: 10 seconds.
 
Fields inherited from class java.lang.Thread
MAX_PRIORITY, MIN_PRIORITY, NORM_PRIORITY
 
Constructor Summary
protected InMemoryDb(InMemoryDbManager manager, String databaseName, long timeout, long timeoutCheckInterval)
          Create a new in memory-db with the given database name.
 
Method Summary
protected  javax.persistence.EntityManagerFactory buildEntityManagerFactory()
          Builds an entity manager factory associated to this in-memory database using the EJB configuration of the in-memory database manager.
protected  org.hibernate.SessionFactory buildSessionFactory()
          Builds a hibernate session factory associated to this in-memory database for the database setup.
protected  void destroyDatabase()
          Destroys an associated Derby in-memory database instance.
protected  void executeStatements(org.hibernate.Session session, String[] statements)
          Executes all given SQL statements using the given hibernate session.
 javax.persistence.EntityManagerFactory getEntityManagerFactory()
          Gets the entity-manager factory creating entity-managers for entities persisted in this database.
 void run()
          Sets up the database and then waits until the timeout is reached
protected  void setupDatabase()
          Sets up an associated Derby in-memory database instance and executes the SQL setup script of the in-memory database manager.
protected  boolean waitForCreationCompleted()
          The first process calling this method will trigger the creation of a Derby in-memory database.
protected  void waitForTimeout()
          Waits for the timeout to be reaches by frequently checking if the timeout has already been reached an decreasing the remaining time.
 
Methods inherited from class java.lang.Thread
activeCount, checkAccess, countStackFrames, currentThread, destroy, dumpStack, enumerate, getAllStackTraces, getContextClassLoader, getDefaultUncaughtExceptionHandler, getId, getName, getPriority, getStackTrace, getState, getThreadGroup, getUncaughtExceptionHandler, holdsLock, interrupt, interrupted, isAlive, isDaemon, isInterrupted, join, join, join, resume, setContextClassLoader, setDaemon, setDefaultUncaughtExceptionHandler, setName, setPriority, setUncaughtExceptionHandler, sleep, sleep, start, stop, stop, suspend, toString, yield
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

DEFAULT_INITIAL_TIMEOUT

public static final long DEFAULT_INITIAL_TIMEOUT
Default maximum idle time before a database is dropped: 1 hour.

See Also:
Constant Field Values

DEFAULT_TIMEOUT_CHECK_INTERVAL

public static final long DEFAULT_TIMEOUT_CHECK_INTERVAL
Default update interval to check if the database should be dropped: 10 seconds.

See Also:
Constant Field Values
Constructor Detail

InMemoryDb

protected InMemoryDb(InMemoryDbManager manager,
                     String databaseName,
                     long timeout,
                     long timeoutCheckInterval)
Create a new in memory-db with the given database name. Intended to be called by the managing in-memory manager only. The database will be destroyed after the no new entity manager was called within the given timeout. If the timeout has already been reached is checked in the given interval.

Parameters:
manager - The in-memory database manager for this database
databaseName - The database name
timeout - The timeout in milliseconds
timeoutCheckInterval - The interval to check for timeout in milliseconds
Method Detail

run

public void run()
Sets up the database and then waits until the timeout is reached

Specified by:
run in interface Runnable
Overrides:
run in class Thread

getEntityManagerFactory

public javax.persistence.EntityManagerFactory getEntityManagerFactory()
Gets the entity-manager factory creating entity-managers for entities persisted in this database. Calling this method resets the remaining timeout its initial value.

Returns:
The entity manager factory

waitForCreationCompleted

protected boolean waitForCreationCompleted()
The first process calling this method will trigger the creation of a Derby in-memory database. All following database will be queued for waiting until the creation process completed.

Returns:
Whether the database has already been created or not

setupDatabase

protected void setupDatabase()
Sets up an associated Derby in-memory database instance and executes the SQL setup script of the in-memory database manager.


waitForTimeout

protected void waitForTimeout()
Waits for the timeout to be reaches by frequently checking if the timeout has already been reached an decreasing the remaining time.


destroyDatabase

protected void destroyDatabase()
Destroys an associated Derby in-memory database instance.


buildEntityManagerFactory

protected javax.persistence.EntityManagerFactory buildEntityManagerFactory()
Builds an entity manager factory associated to this in-memory database using the EJB configuration of the in-memory database manager.

Returns:
The entity manager factory

buildSessionFactory

protected org.hibernate.SessionFactory buildSessionFactory()
Builds a hibernate session factory associated to this in-memory database for the database setup.

Returns:
The hibernate session factory

executeStatements

protected void executeStatements(org.hibernate.Session session,
                                 String[] statements)
Executes all given SQL statements using the given hibernate session.

Parameters:
session - The hibernate session
statements - The SQL statements


Copyright © 2011. All Rights Reserved.