H2 embedded in JBoss AS 7
richaud Jun 1, 2012 5:47 AMHi,
I am currently trying to reuse set up a datasource pointing to the embedded H2 database. However, my objective is not to use the in-memory approach, but rather file bsed pêrsistency. My datasource is thus declared as follows:
<datasources xmlns="http://www.jboss.org/ironjacamar/schema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.jboss.org/ironjacamar/schema http://docs.jboss.org/ironjacamar/schema/datasources_1_0.xsd">
<datasource jndi-name="java:jboss/datasources/FesDS" pool-name="fes" enabled="true" use-java-context="true">
<connection-url>jdbc:h2:${jboss.server.data.dir}${/}h2${/}fes;DB_CLOSE_ON_EXIT=FALSE</connection-url>
<driver>h2</driver>
<security>
<user-name>sa</user-name>
<password>sa</password>
</security>
</datasource>
</datasources>
A persistence.xml file points to that datasource.
I have declared a bean as follows:
@Stateless public class FesService { @Inject private Logger logger; @Inject private EntityManager em; public String getTimestamp() { DateFormat df = DateFormat.getDateTimeInstance(); Date now = Calendar.getInstance().getTime(); return df.format(now); } public List<Entry> addEntry(int value) { Entry entry = new Entry(); entry.setValue(value); em.persist(entry); logger.info("persisted: " + entry); return Collections.singletonList(entry); } public List<Entry> getEntries() { Query q = em.createQuery("select e from Entry e", Entry.class); @SuppressWarnings("unchecked") List<Entry> entries = q.getResultList(); return entries; } }
It works pretty well. I can see the teh 3 H2 files created in the directory designated by the datasource. I see the statement begin issued, and I can create and retrieve data.
What does not work is that if I restart the server, no durability is ensured, like if in-memory was used. I have set up the H2 web console, and I do not see any data persisted into the H2 database.
Is the H2 database coming with Jboss AS7 only limited to in-memory database?
Regards
Olivier
PS: using JBoss AS 7.1.1.Final