Hi everyone.

First of all I would like to thank the developers/maintainers of H2 
database. It has always served me perfectly for my needs (especially for 
automated testing of persistence layer).

Iam running *H2 database version 1.4.200* and using *Maven* to manage 
dependencies. I am using *Java 8* and running on *Ubuntu 18.04*.

I am currently in this situation:

   - I am developing a *Java EE 8 REST application* which uses a database
   - I am using *Hibernate* for persistence
   - I am using Wildfly 18 as application server
   - I want to test the endpoints of my application (using *Arquillian*), 
   e.g. test that if some entities are in the database and i call the 
   respective endpoint, the entities are returned.


My integration tests for endpoints are structured in the following way:

   - Start an H2 TCP server
   - Create a non-managed EntityManagerFactory with a persistence.xml 
   referring to the H2 database, which will be used for populating the 
   database in the tests
   - Use Arquillian to deploy a micro-deployment for the application on 
   Wildfly
   - The deployment will internally use another persistence.xml, with a 
   connection to the same H2 database 
   - (The Arquillian tests are executed outside the container, with 
   @RunAsClient and @Deployment(testable = false))


To my understanding, at this point I should have two connections to a 
single H2 database (running in the TCP server):

   1. The first one using a *non-managed* EntityManagerFactory, which 
   should be used to insert instances in the database from the tests. I will 
   call this "external" connection
   2. The second one, using a *managed* Factory, handled by the Wildfly 
   environment, which is used internally by the deployed application when I 
   call the REST API from outside the container. I will call this "internal" 
   connection, in that it is used inside the deployed war application

*The problem is that I cannot establish the two connections to the same 
database*: from the logs I can see that two connections are created for the 
same database (at least it seems so), but when I add some entities to the 
database from the test case using the "external" connection, no entities 
are retrieved from the "internal" connection.

*So it seems that two separate databases are created, instead of the single 
one that I need.*

The configurations that I tried are the following:

   1. *In-Memory*. Using an in-memory database, following the recommended 
   approach 
   
<https://stackoverflow.com/questions/5077584/h2-database-in-memory-mode-cannot-be-accessed-by-console>
   :
      - Create the "external" EntityManagerFactory manually, using 
      "jdbc:h2:mem:H2-db;DB_CLOSE_DELAY=-1"
      as url inside persistence.xml. This should create the database
      - Start a TCP server in the test case programmatically: 
      Server.createTcpServer("-tcp", "-tcpAllowOthers", "-tcpPort", "9128", 
      "-trace");
      - Use a tcp url for the persistence.xml url inside the 
      micro-deployment: 
      
      "jdbc:h2:tcp://localhost:9128/mem:H2-db;DB_CLOSE_DELAY=-1;IFEXISTS=TRUE"
      - Let Arquillian deploy the micro-deployment (I can see the logs that 
      the persistence unit is created successfully)
   2. *Persistent*. Using persistent databases:


   - Manually start the server with the following command:
      - java -cp h2-1.4.200.jar org.h2.tools.Serve-tcp -tcpAllowOthers -tcpPort 
      9128 -trace -web -webAllowOthers
      - Use the same TCP url for both persistence.xml files: 
      "jdbc:h2:tcp://localhost:9128/~/H2-db;DB_CLOSE_DELAY=-1;IFEXISTS=TRUE"
      . As far as I understand this means that both the connections will be 
      made to the same "remote" database, as in other DMBSs normal server mode.
      - Let Arquillian deploy the micro-deployment (I can see the logs that 
      the persistence unit is created successfully)
   

Unfortunately both of the approaches seem correct but do not work, in the 
sense that they do not throw errors and from the logs I can see that I can 
both the persistence units being created successfully, but if I insert 
entities from one persistence unit, the other does not see them, as if they 
were linked to different databases.

I would prefer an in-memory approach, as these are data used only for tests 
and can be destroyed. I only tried the persistent approach because the in 
memory one did not work.

I hope I made myself clear, because it is a quite complex situation for 
myself too.

-- 
You received this message because you are subscribed to the Google Groups "H2 
Database" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/h2-database/6417dc69-369a-4f62-9223-65dfede9e2a7o%40googlegroups.com.

Reply via email to