I haven't used in memory databases. But H2 usually closes a database after all connections to it close. So you either need to use a connection pool. Keep a single connection open as a "control" connection. Delay the closing of the database, or have it not close the database until you close the JVM.

See documentation under In memory databases on the features page

"By default, closing the last connection to a database closes the database. For an in-memory database, this means the content is lost. To keep the database open, add |;DB_CLOSE_DELAY=-1| to the database URL. To keep the content of an in-memory database as long as the virtual machine is alive, use |jdbc:h2:mem:test;DB_CLOSE_DELAY=-1|. "

Hope this helps,

Ryan


On 23/03/2010 9:44 PM, ramon_garcia wrote:
According to the documentation, one should use URLs like
jdbc:h2:mem:name

so that an H2 database in memory persists accross connections.
However, this does not work with the simple example bellow:

---------------------------
package mytest;

import java.sql.Connection;
import java.sql.SQLException;



import org.h2.jdbcx.JdbcDataSource;
import org.junit.Test;


public class TestH2DataSource {
        @Test
        public void doTest() throws SQLException {
                JdbcDataSource s = new JdbcDataSource();
                s.setURL("jdbc:h2:mem:database");
                Connection c1 = s.getConnection();
                c1.createStatement().execute("create table foo (a integer, b
integer)");
                c1.close();
                Connection c2 = s.getConnection();
                c2.createStatement().execute("insert into foo (a, b) VALUES (1,
3)");
                c2.close();
        }
}


-----------------------------------------------------

The statement insert fails because the table foo does not exist. The
test works correctly if both statements are executed against the same
connection.


Best regards,

Ramon


--
You received this message because you are subscribed to the Google Groups "H2 
Database" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/h2-database?hl=en.

Reply via email to