How to use a database as your database source in J

2019-03-03 09:50发布

问题:

I'm new in developing apps in JBoss so I followed their tutorials specially in making the Ticket Monster. For now, I've created an Event JPA Entity and in the run, it shows that I can save an event. But when I restart my computer, it seems the event I've saved is lost, so research about it and found some info about in-memory database. My problem is how do I tell/configure my project not to use in-memory database but to use the typical database so every time I restarted my computer the data was in the database. I would like to use PostgreSQL for my database. My current datasource profile that is set to JPA is TestDB but the datasource of my persistence.xml is

<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="primary">
      <!-- If you are running in a production environment, add a managed 
         data source, this example data source is just for development and testing! -->
      <!-- The datasource is deployed as WEB-INF/ticket-monster-ds.xml, you
         can find it in the source at src/main/webapp/WEB-INF/ticket-monster-ds.xml -->
      <jta-data-source>java:jboss/datasources/ticket-monsterDS</jta-data-source>
      <properties>
         <!-- Properties for Hibernate -->
         <property name="hibernate.hbm2ddl.auto" value="create-drop" />
         <property name="hibernate.show_sql" value="false" />
      </properties>
   </persistence-unit>
</persistence>

it said that <jta-data-source>java:jboss/datasources/ticket-monsterDS</jta-data-source> not the TestDB. Also, I would like not use the create-drop but instead a value that could tell if there is something in the database do not drop it but use it and or create if no existing.

UPDATE

Heres my ticket-monster-ds.xml

<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">
    <!-- The datasource is bound into JNDI at this location. We reference 
        this in META-INF/persistence.xml -->
    <datasource jndi-name="java:jboss/datasources/ticket-monsterDS"
        pool-name="ticket-monster" enabled="true"
        use-java-context="true">
        <connection-url>jdbc:h2:mem:ticket-monster;DB_CLOSE_ON_EXIT=FALSE;DB_CLOSE_DELAY=-1</connection-url>
        <driver>h2</driver>
        <security>
            <user-name>sa</user-name>
            <password>sa</password>
        </security>
    </datasource>
</datasources>

回答1:

You can continue to use the H2 database from the example. H2 can be configured to write to a file. You just need to change your connect string.

eg. <connection-url>jdbc:h2:~/test;<connection-url>

See the H2 documentation for further options.



回答2:

So after I posted on the jboss forum, here's the link Wildfly 10: Cannot upload deployment , Wolfgang Mayer answered that I should not declared a datasource more than once. Here's the step.

1.You can set a datasource via your Admin Console(localhost:9990) or via your Project(*-ds.xml), not both. Please remember the JNDI(Java Naming and Directory Interface) of your Datasource.

Example: Datasource Name:PostgresDS JNDI:java:/PostgresDS Connection URL: jdbc:postgresql://host:port/databasename

2.Tell your project or war to use your datasource you've created. Edit your pesistence.xml and put the JNDI of your datasource.

Example:

<?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="primary">
      <jta-data-source>java:/PostgresDS</jta-data-source>
      <properties>
         <!-- Properties for Hibernate -->
         <property name="hibernate.hbm2ddl.auto" value="create-drop" />
         <property name="hibernate.show_sql" value="false" />
      </properties>
   </persistence-unit>
</persistence>

3.(Optional) If you created your datasource in Admin Console, delete your *-ds.xml file in your project folder to prevent the "Datasource is already registered" error.