Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create an in-memory database structure from an Oracle instance

I have an application where many "unit" tests use a real connection to an Oracle database during their execution.

As you can imagine, these tests take too much time to be executed, as they need to initialize some Spring contexts, and communicate to the Oracle instance. In addition to that, we have to manage complex mechanisms, such as transactions, in order to avoid database modifications after the test execution (even if we use usefull classes from Spring like AbstractAnnotationAwareTransactionalTests).

So my idea is to progressively replace this Oracle test instance by an in-memory database. I will use hsqldb or maybe better h2.

My question is to know what is the best approach to do that. My main concern is related to the construction of the in-memory database structure and insertion of reference data.

Of course, I can extract the database structure from Oracle, using some tools like SQL Developer or TOAD, and then modifying these scripts to adapt them to the hsqldb or h2 language. But I don't think that's the better approach.


In fact, I already did that on another project using hsqldb, but I have written manually all the scripts to create tables. Fortunately, I had only few tables to create. My main problem during this step was to "translate" the Oracle scripts used to create tables into the hsqldb language.

For example, a table created in Oracle using the following sql command:

CREATE TABLE FOOBAR (     SOME_ID NUMBER,     SOME_DATE DATE, -- Add primary key constraint     SOME_STATUS NUMBER,     SOME_FLAG NUMBER(1) DEFAULT 0 NOT NULL); 

needed to be "translated" for hsqldb to:

CREATE TABLE FOOBAR (     SOME_ID NUMERIC,     SOME_DATE TIMESTAMP PRIMARY KEY,     SOME_STATUS NUMERIC,     SOME_FLAG INTEGER DEFAULT 0 NOT NULL); 

In my current project, there are too many tables to do that manually...


So my questions:

  • What are the advices you can give me to achieve that?
  • Does h2 or hsqldb provide some tools to generate their scripts from an Oracle connection?

Technical information

Java 1.6, Spring 2.5, Oracle 10.g, Maven 2


Edit

Some information regarding my unit tests:

In the application where I used hsqldb, I had the following tests: - Some "basic" unit tests, which have nothing to do with DB. - For DAO testing, I used hsqldb to execute database manipulations, such as CRUD. - Then, on the service layer, I used Mockito to mock my DAO objects, in order to focus on the service test and not the whole applications (i.e. service + dao + DB).

In my current application, we have the worst scenario: The DAO layer tests need an Oracle connection to be run. The services layer does not use (yet) any mock objects to simulate the DAO. So services tests also need an Oracle connection.

I am aware that mocks and in-memory database are two separates points, and I will address them as soon as possible. However, my first step is to try to remove the Oracle connection by an in-memory database, and then I will use my Mockito knowledges to enhance the tests.

Note that I also want to separate unit tests from integration tests. The latter will need an access to the Oracle database, to execute "real" tests, but my main concern (and this is the purpose of this question) is that almost all of my unit tests are not run in isolation today.

like image 866
Romain Linsolas Avatar asked Sep 24 '10 06:09

Romain Linsolas


People also ask

How do I create an in memory database?

Use create inmemory database to create an in-memory database, using model or another user database as its template. You can also create temporary databases as in-memory databases that reside entirely in in-memory storage. However, you cannot specify a template database for an in-memory temporary database.

Does Oracle have in memory database?

Oracle Database In-Memory provides a unique dual-format architecture that enables tables to be simultaneously represented in memory using traditional row format and a new in-memory column format.

What is the memory structure of Oracle instance called?

The basic memory structures associated with Oracle Database include: System global area (SGA) The SGA is a group of shared memory structures, known as SGA components, that contain data and control information for one Oracle Database instance. All server and background processes share the SGA.


1 Answers

Use an in-memory / Java database for testing. This will ensure the tests are closer to the real world than if you try to 'abstract away' the database in your test. Probably such tests are also easier to write and maintain. On the other hand, what you probably do want to 'abstract away' in your tests is the UI, because UI testing is usually hard to automate.

The Oracle syntax you posted works well with the H2 database (I just tested it), so it seems H2 supports the Oracle syntax better than HSQLDB. Disclaimer: I'm one of the authors of H2. If something doesn't work, please post it on the H2 mailing list.

You should anyway have the DDL statements for the database in your version control system. You can use those scripts for testing as well. Possibly you also need to support multiple schema versions - in that case you could write version update scripts (alter table...). With a Java database you can test those as well.

By the way, you don't necessarily need to use the in-memory mode when using H2 or HSQLDB. Both databases are fast even if you persist the data. And they are easy to install (just a jar file) and need much less memory than Oracle.

like image 113
Thomas Mueller Avatar answered Oct 08 '22 15:10

Thomas Mueller