0

I am trying to create an spring boot application utilizing hibernate and h2. From what I have found online this can be done but I am having a problem starting the application. Hibernate is complaining that it cannot make a connection to the h2 database I have created.

Caused by: org.hibernate.HibernateException: Unable to make JDBC Connection [jdbc:h2:~/todo]

My theory is that the application needs to start for the database be available but hibernate is not letting the application start without the connection.

Am I on the right track with this theory, has there been similar issues that someone knows how to get around this?

Hibernate config

**<?xml version="1.0" encoding="UTF-8"?>
<hibernate-configuration>
    <session-factory>
        <!--Database connection settings -->
        <property name="connection.driver_class">com.mysql.cj.jdbc.Driver</property>
        <property name="connection.url">jdbc:h2:~/todo</property>
        <property name="connection.username">username</property>
        <property name="connection.password" />

        <!--Set the database dialect -->
        <property name="dialect">org.hibernate.dialect.H2Dialect</property>

        <!--Echo all executed SQL to stdout-->
        <property name="show_sql">true</property>

        <!--Drop and re-create the database schema on startup-->
        <property name="hbm2ddl.auto">create</property>

        <!--Name the annotated Entity classes -->
        <mapping class="com.todo.beans.User" />

    </session-factory>
</hibernate-configuration>**

h2 config

import org.h2.server.web.WebServlet;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class WebConfiguration {
    @Bean
    ServletRegistrationBean h2servletRegistration(){
        ServletRegistrationBean registrationBean = new ServletRegistrationBean( new WebServlet());
        registrationBean.addUrlMappings("/console/*");
        return registrationBean;
    }
}
1
  • Are you on linux? Commented Jun 1, 2017 at 1:36

2 Answers 2

2

change following properties in hibernate config <property name="connection.driver_class">org.h2.Driver</property> <property name="connection.url">jdbc:h2:mem:todo;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE </property>

Problem is with driver class; you may keep url as it is.

Sign up to request clarification or add additional context in comments.

2 Comments

This worked! mind supplying a little background as to why this was the solution?
Actually DriverManager class of java.sql package uses connection.driver_class property.The DriverManager class acts as an interface between user and drivers. It keeps track of the drivers that are available and handles establishing a connection between a database and the appropriate driver. DriverManager loads this initial drivers.
0

This sample project can help you.

https://github.com/dornala/HotelStackoverflow

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.