0

I am trying to configure JPA to run with a basic spring application and the h2 database. I am receiving the following error message ( I think this is the most relevant segment of the log ):

Sep 15, 2017 10:23:26 PM org.hibernate.tool.hbm2ddl.SchemaUpdate execute
ERROR: HHH000319: Could not get database metadata
org.h2.jdbc.JdbcSQLException: Connection is broken: "java.net.ConnectException: Connection refused: connect: localhost" [90067-175]

This comes as a surprise to me as this is code from the Wrox Beginning Spring 4 text I am trying to run. My persistence.xml (located in Meta-Inf) is as follows:

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.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_2_0.xsd">
    <persistence-unit name="test-jpa" transaction-type="RESOURCE_LOCAL">
        <properties>
            <property name="hibernate.connection.driver_class" value="org.h2.Driver" />
            <property name="hibernate.connection.url" value="jdbc:h2:tcp://localhost/~/test" />
            <property name="hibernate.connection.username" value="sa" />
            <property name="hibernate.connection.password" value="" />
            <property name="hibernate.dialect" value="org.hibernate.dialect.H2Dialect" />
            <property name="hibernate.hbm2ddl.auto" value="update" />
        </properties>
    </persistence-unit>
</persistence>

My Main application class sans imports and pom follow:

public class Main {

    public static void main(String[] args) {
        EntityManagerFactory entityManagerFactory = 
            Persistence.createEntityManagerFactory("test-jpa");
        System.out.println(entityManagerFactory.isOpen());
    }
}

<dependencies>
        <dependency>
            <groupId>jstl</groupId>
            <artifactId>jstl</artifactId>
            <version>1.2</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-beans</artifactId>
            <version>4.3.10.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>4.3.10.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>4.3.10.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>4.3.10.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aop</artifactId>
            <version>4.3.10.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>4.0.0</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-jpa</artifactId>
            <version>1.11.6.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-entitymanager</artifactId>
            <version>5.2.10.Final</version>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
            <version>5.2.10.Final</version>
        </dependency>
        <dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
            <version>1.4.196</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-orm</artifactId>
            <version>4.3.10.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-validator</artifactId>
            <version>6.0.2.Final</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>6.0.6</version>
        </dependency>
        <dependency>
            <groupId>commons-dbcp</groupId>
            <artifactId>commons-dbcp</artifactId>
            <version>1.4</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>4.3.10.RELEASE</version>
        </dependency>
    </dependencies>
</project>

Thank you for your help, Marc

Dewey Banks
  • 323
  • 1
  • 7
  • 19
  • Check this one - https://stackoverflow.com/questions/32906232/h2-jdbc-connection-refused-connect-90067-147 – Bala Sep 16 '17 at 03:44
  • Thank you - this comment I saw there: "the version update was a good step, I found an issue with the db path, I update it //localhost/./testdb and now everything is ok." appears to address my issue but I am a rank novice with Spring and still have a few questions. Where is this path? The persistence.xml file? I changed it to the above but still get the same error. – Dewey Banks Sep 16 '17 at 03:56

1 Answers1

0

Ok, I found an answer here https://gist.github.com/mortezaadi/8619433 This is a good listing of sample persistence.xml files for various common database types.

The line of persistence.xml that contains "hibernate.connection.url"should read:

<property name="hibernate.connection.url" value="jdbc:h2:target/h2-1" />

I understand that this is telling the jdbc h2 driver where to look for the h2 database, correct? My code works now but I want to understand why. Is it because the old code was calling localhost in a plain java Spring console application? Is the old configuration appropriate for Spring web projects? Why was the old line (quoted below) causing the error?

<property name="hibernate.connection.url" value="jdbc:h2:tcp://localhost/~/test" />

Thanks for your help, Marc

Dewey Banks
  • 323
  • 1
  • 7
  • 19