I have no special talent. I'm only passionately curious - Albert Einstein
Using DBUnit to transfer data between databases Comment on Using DBUnit to transfer data between databases 116

  At my current client, I've ran into the need to have the ability to transfer data from our production database to our lower level environments for QA and other user testing purposes.  One of the other consultants here mentioned a tool called DBUnit, and he said he thought it might have the capabilities I was looking for.

DBUnit has turned out to be a great tool for performing database tasks with Ant.  I was able to set up an Ant task and create a flat file (XML Format) data dump of the entire database.  The task was very straightforward, called Export, and has a quite a few options for generating the exported database file.

  Export options include creating a DTD for your XML files using the database model, generating the exported data into either a 'flat' or XML file.  Although the flat file is an XML file anyways, the XML option offers a nested element structure which is easier to read. 

  The caveat I encountered immediately was memory concerns while exporting large databases.  It seems that DBUnit will read the entire database into memory when performing an export task.  I ended up breaking out the export into multiple files, specifying a specific subset of tables for each file:

Export Data to Flat Files

<dbunit driver="${sql.driver}"
            password="${sql.password}"
            userid="${sql.userid}"
            url="${sql.url}"
            schema="${target.schema}"           
            classpath="${sql.classpath}">
            <dbconfig>
                <property name="datatypeFactory" value="org.dbunit.ext.db2.Db2DataTypeFactory" />               
                <property name="batchSize" value="50" />
                <!--feature name="qualifiedTableNames" value="true" /-->
                <feature name="batchedStatements" value="true" />
                <!--feature name="datatypeWarning" value="false" / -->               
            </dbconfig>
           
            <export dest="${target.schema}.export.dtd" format="dtd"/>           
            <!-- Export the dataset to three files.  If you try to export the database
            to one file you will run into local OutOfMemory Exceptions -->
            <export dest="${targetDir}\${target.schema}.export.part1.xml" doctype="${target.schema}.export.dtd" encoding="ISO-8859-1">
                <table name="table_1" />
                ...
                <table name="table_9" />                               
            </export>
            <export dest="${targetDir}\${target.schema}.export.part2.xml" doctype="${target.schema}.export.dtd"  encoding="ISO-8859-1">
                <table name="table_10" />
                ...
                <table name="table_22" />
            </export>
            <export dest="${targetDir}\${target.schema}.export.part3.xml" doctype="${target.schema}.export.dtd"  encoding="ISO-8859-1">
                <table name="table_23" />               
            </export>

        </dbunit>

The end result is an xml file which is portable to any database (any vendor, any version, etc.).  As you can see in my configuration above, I'm performing a transfer from a DB2 database.  My next configuration shows how to import the database into another DB2 database, but you could transfer the data into an SQL Server, Oracle, or any other database which contained the necessary schema.  It's important to note that I am importing and exporting to three seperate data files due to memory concerns.  If you have a smaller database, you may not need to break it out like I did. 

Import Data from Flat files to any database you want:

        <dbunit driver="${sql.driver}"
            password="${sql.password}"
            userid="${sql.userid}"
            url="${sql.url}"
            schema="${target.schema}"           
            classpath="${sql.classpath}">
            <dbconfig>
                <property name="datatypeFactory" value="org.dbunit.ext.db2.Db2DataTypeFactory" />               
                <property name="batchSize" value="50" />
                <!--feature name="qualifiedTableNames" value="true" /-->
                <feature name="batchedStatements" value="true" />
                <!--feature name="datatypeWarning" value="false" / -->               
            </dbconfig>
            <!--Import the dataset from three files.  If you try to import the database
                from one file you will run into local OutOfMemory Exceptions.  The
                files here are created using the db-export tasks.  For Operation type,
                use CLEAN_INSERT to completely delete all existing data and insert data from
                the export files, or use REFRESH, which will update existing data and will
                in a delta-style format using your export files.  See http://www.dbunit.org/components.html
                under DatabaseOperation for more information -->
            <operation type="CLEAN_INSERT" src="${targetDir}\${fromschema}.export.part1.xml" />
            <operation type="CLEAN_INSERT" src="${targetDir}\${fromschema}.export.part2.xml" />
            <operation type="CLEAN_INSERT" src="${targetDir}\${fromschema}.export.part3.xml" />
        </dbunit>

Resources:

DBUnit DatabaseOperations Documentation

DBUnit Configuable Features and Properties

DBUnit Javadoc (use this to find your Database Specific dataTypeFactory)


116 comments

cialis deutschland - January 30, 2012

Aloha! cialis deutschland

generic phentermine - February 02, 2012

Aloha! generic phentermine

generic valium - January 31, 2012

Aloha! generic valium

tramadol - January 23, 2012

Aloha! tramadol

phentermine online - February 02, 2012

Aloha! phentermine online

buy tramadol online - January 26, 2012

Aloha! buy tramadol online

valium - January 28, 2012

Aloha! valium

phentermine online - January 18, 2012

Aloha! phentermine online

tramadol online - January 19, 2012

Aloha! tramadol online

zolpidem tartrate - January 28, 2012

Aloha! zolpidem tartrate

buy valium - January 31, 2012

Aloha! buy valium

valium - January 19, 2012

Aloha! valium

buy ambien online - January 20, 2012

Aloha! buy ambien online

phentermine online - January 24, 2012

Aloha! phentermine online

tramadol hcl - January 24, 2012

Aloha! tramadol hcl

zolpidem tartrate - January 18, 2012

Aloha! zolpidem tartrate

adipex p online - January 28, 2012

Aloha! adipex p online

cheap phentermine - February 03, 2012

Aloha! cheap phentermine

buy phentermine - February 01, 2012

Aloha! buy phentermine

buy phentermine - January 29, 2012

Aloha! buy phentermine

ambien cr - January 18, 2012

Aloha! ambien cr

phentermine 37.5mg - February 01, 2012

Aloha! phentermine 37.5mg

buy phentermine - January 19, 2012

Aloha! buy phentermine

phentermine 37.5mg - January 27, 2012

Aloha! phentermine 37.5mg

ambien - January 19, 2012

Aloha! ambien

valium online - February 02, 2012

Aloha! valium online

buy valium online - January 30, 2012

Aloha! buy valium online

cialis deutschland - February 06, 2012

Aloha! cialis deutschland

phentermine - January 28, 2012

Aloha! phentermine

acheter viagra france - January 28, 2012

Aloha! acheter viagra france

phentermine online - January 28, 2012

Aloha! phentermine online

adipex - January 25, 2012

Aloha! adipex

phentermine - January 23, 2012

Aloha! phentermine

valium online - January 19, 2012

Aloha! valium online

tramadol online - January 26, 2012

Aloha! tramadol online

valium - February 02, 2012

Aloha! valium

generic phentermine - January 21, 2012

Aloha! generic phentermine

cheap phentermine - January 29, 2012

Aloha! cheap phentermine

generic phentermine - January 25, 2012

Aloha! generic phentermine

cheap phentermine - January 21, 2012

Aloha! cheap phentermine

zolpidem - January 21, 2012

Aloha! zolpidem

phentermine 37.5mg - January 22, 2012

Aloha! phentermine 37.5mg

phentermine 37.5mg - January 19, 2012

Aloha! phentermine 37.5mg

phentermine - January 18, 2012

Aloha! phentermine

cheap phentermine - January 19, 2012

Aloha! cheap phentermine

diazepam - January 18, 2012

Aloha! diazepam

buy phentermine - January 20, 2012

Aloha! buy phentermine

tramadol - January 27, 2012

Aloha! tramadol

adipex phentermine - January 28, 2012

Aloha! adipex phentermine

adipex - January 30, 2012

Aloha! adipex

buy phentermine - February 03, 2012

Aloha! buy phentermine

buy valium online - January 20, 2012

Aloha! buy valium online

generic valium - January 21, 2012

Aloha! generic valium

diazepam - January 21, 2012

Aloha! diazepam

valium - January 29, 2012

Aloha! valium

diazepam - February 01, 2012

Aloha! diazepam

phentermine online - February 01, 2012

Aloha! phentermine online

buy ambien - January 20, 2012

Aloha! buy ambien

phentermine 37.5mg - January 31, 2012

Aloha! phentermine 37.5mg

buy tramadol - January 25, 2012

Aloha! buy tramadol

cheap phentermine - January 24, 2012

Aloha! cheap phentermine

phentermine online - January 20, 2012

Aloha! phentermine online

ambien online - January 19, 2012

Aloha! ambien online

adipex - January 29, 2012

Aloha! adipex

phentermine online - January 23, 2012

Aloha! phentermine online

tramadol hcl - January 28, 2012

Aloha! tramadol hcl

generic phentermine - February 04, 2012

Aloha! generic phentermine

ambien cr - January 23, 2012

Aloha! ambien cr

viagra - January 19, 2012

Aloha! viagra

buy phentermine - January 24, 2012

Aloha! buy phentermine

adipex - January 20, 2012

Aloha! adipex

phentermine - January 31, 2012

Aloha! phentermine

xanax online - January 24, 2012

Aloha! xanax online

cheap tramadol - January 25, 2012

Aloha! cheap tramadol

generic ambien - January 21, 2012

Aloha! generic ambien

generic phentermine - January 20, 2012

Aloha! generic phentermine

generic phentermine - January 30, 2012

Aloha! generic phentermine

buy valium - January 20, 2012

Aloha! buy valium

buy phentermine online - February 05, 2012

Aloha! buy phentermine online

valium online - January 30, 2012

Aloha! valium online

cheap phentermine - February 02, 2012

Aloha! cheap phentermine

adipex - February 04, 2012

Aloha! adipex

phentermine - February 02, 2012

Aloha! phentermine

phentermine - January 19, 2012

Aloha! phentermine

Comments are currently disabled

About

David Malone is a Java developer residing in the Twin Cities area.  He has been developing enterprise applications since 2004.  This is his personal blog, as well as his design and development workspace.