2

I'm currently working on JBoss 7.1.1 with JPA (with Hibernate) and Spring. I have spring's @Transactional to take care of Transactions. I'm currently using the internal H2 database of JBoss 7.1.1. All the transactions work fine when the server is up. But once I shutdown my server, all the changes made to the entity beans are not saved in H2 database files on JBoss server restart. After the JBoss server is shutdown, when I connect to H2 database using the files, I see that the schema is created but the data is not saved.

I feel that it is not the issue with my code but some configuration because it works fine when the application is running.

Here is my persistence.xml:

<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
    version="2.0">
    <persistence-unit name="myappname" transaction-type="JTA">
        <jta-data-source>java:/DefaultDS</jta-data-source>
        <properties>
            <property name="hibernate.dialect" value="org.hibernate.dialect.H2Dialect" />
            <property name="hibernate.hbm2ddl.auto" value="update" />
            <property name="jboss.entity.manager.factory.jndi.name" value="java:app/myappname"/>
        </properties>
    </persistence-unit>

</persistence>

Datasource in standalone.xml:

<datasource jta="true" jndi-name="java:/DefaultDS" pool-name="DefaultDS" enabled="true" use-java-context="true">
    <connection-url>jdbc:h2:file:${jboss.server.data.dir}${/}h2${/}localDB</connection-url>
    <driver>h2</driver>
    <pool>
        <min-pool-size>5</min-pool-size>
        <max-pool-size>20</max-pool-size>
    </pool>
    <security>
        <user-name>sa</user-name>
    </security>
    <timeout>
        <idle-timeout-minutes>0</idle-timeout-minutes>
    </timeout>
    <statement>
        <prepared-statement-cache-size>32</prepared-statement-cache-size>
    </statement>
</datasource>

How can I get this working ?

Thanks in advance

Chris
  • 5,584
  • 9
  • 40
  • 58
  • 1
    I'm not sure, but could you try enforcing to use an absolute path, I think that would be `jdbc:h2:file:/${jboss.server.data.dir}/h2/localDB` or maybe `jdbc:h2:file:${/}${jboss.server.data.dir}${/}h2${/}localDB` (not sure if / why would `${/}` need to be used instead of simply `/`). – Thomas Mueller Sep 18 '12 at 09:06
  • @ThomasMueller Thanks for the reply. Forgot to update you. I tried what you suggested. It does not seem to work :-( – Chris Sep 18 '12 at 10:48
  • I still think the database URL is the problem. I would test with a fully hardcoded database URL, for example: `jdbc:h2:file:/data/jboss/h2/localDB`. – Thomas Mueller Sep 18 '12 at 18:08

2 Answers2

0

The only thing that looks unusual about the persistence.xml and datasource configuration shown is the database path, however, I have tried your configuration on JBoss AS 7.1.1 with a similar database path and I can't replicate the problem. I can definitely access data persisted by the application, even after the server has been restarted or shut down. However, my application does not use Spring; perhaps the problem lies in the Spring configuration, or elsewhere? Are you certain you are connecting to the same H2 database file that your application is using?

Katie J. Ots
  • 873
  • 9
  • 13
0

in your hibernate.cfg.xml file you should have

<property name="hibernate.hbm2ddl.auto">create</property>

instead of create which always drops and recreates the table use validate.

<property name="hibernate.hbm2ddl.auto">validate</property>