Issue
I'm trying to connect my Jakarta EE 9.1.0 project to H2 database but I get this exception:
org.h2.jdbc.JdbcSQLNonTransientConnectionException: Duplicate property "USER" [90066-212]
Here is my persistance.xml file (version="3.0") persistence-unit
part:
<persistence-unit name="PersistenceUnit" transaction-type="JTA">
<jta-data-source>java:global/dataSource</jta-data-source>
<properties>
<property name="jakarta.persistence.schema-generation.database.action" value="drop-and-create"/>
</properties>
</persistence-unit>
And web.xml file (version="5.0") data-source
and H2Console servlet
parts:
<data-source>
<name>java:global/dataSource</name>
<class-name>org.h2.jdbcx.JdbcDataSource</class-name>
<url>jdbc:h2:file:D:/testdb;USER=sa;PASSWORD=123</url>
</data-source>
<servlet>
<servlet-name>H2Console</servlet-name>
<servlet-class>org.h2.server.web.JakartaWebServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>H2Console</servlet-name>
<url-pattern>/h2/*</url-pattern>
</servlet-mapping>
And in pom.xml file I add this H2 dependency:
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>2.1.212</version>
</dependency>
Can someone please help fix the coding issue?
Solution
I've just added this two lines in persistence.xml file and the error fixed:
<property name="jakarta.persistence.jdbc.user" value="user"/>
<property name="jakarta.persistence.jdbc.password" value="password"/>
Complete persistence-unit
tag of persistence configuration (persistence.xml) is as follows:
<persistence-unit name="PersistenceUnit" transaction-type="JTA">
<jta-data-source>java:global/dataSource</jta-data-source>
<properties>
<property name="jakarta.persistence.schema-generation.database.action" value="create"/>
<property name="jakarta.persistence.jdbc.user" value="user"/>
<property name="jakarta.persistence.jdbc.password" value="password"/>
</properties>
</persistence-unit>
Answered By - Mehdi Rahimi
Answer Checked By - Mary Flores (JavaFixing Volunteer)