Search code examples
javaglassfish-4javadb

DB contents get deleted every time I launch the app


First I want to say that Im in a programming school atm, and in the course called project development I received a half finished project from seniors to continue. So needless to say I dont know the project by heart and that paired with my inexperience, I hope you can forgive my possibly stupid questions.

It is a Maven Web Application, the language used is Java. The database is a simpe JAVA DB (Derby), the server is Glassfish 4.0. Both are used within the Netbeans 8.0 environment.

Now to the actual problem. The App is a online Questionnaire creation tool, so the user can create Questions, answers, upload a picture etc.. And it does work, everything is stored in the DB and persisted, I checked manually and the data is there and stays there after closing the App. However as soon as I re-launch the App, all DB content is lost. Yet not quite entirely, it seems that only the ID gets deleted, since when I upload a picture again, the old ones (having the same ID) that I uploaded last session appear instead.

Ofc, my first thought was the persistence.xml and I did set it Table Generation Strategy to "none". Here is my persistence.xml (EclipseLink(JPA 2.1)):

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.1" xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
  <persistence-unit name="OnlineTestCreatorPU" transaction-type="JTA">
    <jta-data-source>jdbc/online_test_creator_db</jta-data-source>
    <properties>
    </properties>
  </persistence-unit>
</persistence>

This didnt solve the issue either, so next I went to the glassfish-ressources.xml and removed the "create=true" Properties. This didnt solve it either.

I heard from a friend that sometimes the programm doesnt rebuild with the new settings properly and I have to make sure it cleans correctly. So I cleaned the App 10 times succesfully and Build everything again and tried but still no result. I also made sure that the DB name matched up with the entries in my persistence.xml and glassfish-ressources.xml. I am honestly at the end of my (rather limited) knowledge.

So what could be causing all my DB content to dissapear everytime I launch the App?

If anything is missing and you need further information just let me know and I'll try to provied it to the best of my ability.


Solution

  • There are different things you can check:

    1.

    However as soon as I re-launch the App, all DB content is lost. Yet not quite entirely, it seems that only the ID gets deleted, since when I upload a picture again, the old ones (having the same ID) that I uploaded last session appear instead.

    You should make sure which content is really in the database after starting it. The pictures may be cached by your browser. To check this, you should start the database alone and inspect the content. If you are using the Derby DB which comes with NetBeans, you can do that in NetBeans Services tab.

    2.

    Maybe Derby is configured with the inmemory-mode, in this mode all the database content is stored completly in memory and gets lost if you restart the database. You should check the JDBC URL which is set for the persistence-unit. If the URL looks similar to this

    jdbc:derby://myhost:1527/memory:myDB;create=true
    

    the database is configured with the inmemory-mode. You can check the settings via the GlassFish admin GUI in the Additional Properties tab of the specific Derby connection pool.

    3.

    There may be some logic in your application which deletes or overwrites the old database entries. You can easily debug this by activating the SQL statement logging. In your persistence.xml add the following:

    <property name="eclipselink.logging.level" value="ALL"/>
    <property name="eclipselink.logging.parameters" value="true"/>
    

    If your are using EclipseLink 2.4.0 or above:

    <property name="eclipselink.logging.level.sql" value="ALL"/>
    <property name="eclipselink.logging.parameters" value="true"/>
    

    This will show all SQL statements which are coming from your application.