Search code examples
javajpapersistenceembedded-databasejavadb

Why my data does not get persisted?


I am using JPA and Java Embedded DB in my application. I try to write some data to the database and when i try to read it back I am able to do it. But the application is closed and when I open it again none of the data exists.

Here is my persistence.xml file

    <?xml version="1.0" encoding="UTF-8"?>
<persistence version="1.0" 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_1_0.xsd">
  <persistence-unit name="myMoneyPU" transaction-type="RESOURCE_LOCAL">
    <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
    <class>mymoney.Money</class>
    <properties>
      <property name="eclipselink.jdbc.password" value="adminadmin"/>
      <property name="eclipselink.jdbc.user" value="admin"/>
      <property name="eclipselink.jdbc.driver" value="org.apache.derby.jdbc.EmbeddedDriver"/>
      <property name="eclipselink.jdbc.url" value="jdbc:derby:pocketmoney;create=true"/>
      <property name="eclipselink.ddl-generation" value="create-tables"/>
    </properties>
  </persistence-unit>
</persistence>

Is there any problem in my database URL.

jdbc:derby:pocketmoney


Solution

  • Is there any problem in my database URL.

    No, it is correct and should create a database in the current working directory.

    Some ideas to debug your issue:

    • Use an absolute path for full control jdbc:derby:/path/to/pocketmoney;create=true.
    • Check that the data get written (i.e. that the files change).
    • Activate EcliseLink logging to see what is happening exactly using the following properties:

      <properties>
        ...
        <property name="eclipselink.debug" value="ALL"/>          
        <property name="eclipselink.logging.level" value="FINEST" />
        <property name="eclipselink.logging.level.sql" value="FINEST" />
        <property name="eclipselink.logging.level.cache" value="FINEST" />
      </properties>
      
    • Double check that you do commit transactions appropriately (you are saying your code is doing things but you're not showing it so there is still a doubt).

    References