views:

126

answers:

1

Is it possible to setup HSQLDB in a way, so that the files with the db information are written into memory instead of using actual files? I want to use hsqldb to export some data structures together with hibernate mappings. Is is, however, not possible to write temporary files, so that I need to generate the files in-memory and return a stream with their contents as a response.

Setting hsqldb to use nio seems not to be a solution, because there is no way to get hold of those files before they get written onto the filesystem.

What I'm thinking of is a protocol handler for hsqldb, but I didn't find a suitable solution yet.

Just to describe in other words: A hack solution would be to pass hsqldb a stream or several streams. It would then during its operation write data into those streams. After all data is written, the user of the db could then use those streams to send it back over the network.

+1  A: 

Yes, of course, we use it all the time for integration testing.

use as url : jdbc:hsqldb:mem:aname

see here for more details

DbUnit offers a handy database dump method as part of their package :

    // database connection
    Class driverClass = Class.forName("org.hsqldb.jdbcDriver");
    Connection jdbcConnection = DriverManager.getConnection(
            "jdbc:hsqldb:sample", "sa", "");
    IDatabaseConnection connection = new DatabaseConnection(jdbcConnection);

    // full database export
    IDataSet fullDataSet = connection.createDataSet();
    FlatXmlDataSet.write(fullDataSet, new FileOutputStream("full.xml"));

see DbUnit FAQ for more details. Of course there are routines to restore the data, as that is actually the puropose of the package : prepare a test database for integration testing. Usually we do this with an annotation, but you'll have to use tha API for that.

Peter Tillemans
Sure, but how to get hold of the data after the end of hsqldb execution?
lewap
Or in other words: is it possible to get hold of the in-memory representation and serialize it into a stream after hsqldb has returnd?
lewap
@lewap I updated my answer. My apoplogies for nt reading the question attentively enough. With an uberjar like tool I guess that the overhead of all the test support can be minimized by only copying the classes needed for the dump.
Peter Tillemans
You're the best! Thanks for your solution!
lewap