Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Java Spring with Hibernate Integrating Hibernate with Spring Fetching Data with Hibernate in Spring

Lewis Cowles
Lewis Cowles
74,902 Points

Script to run hibernate with spring

So looking at how Chris is running hibernate with spring example I came up with the following

#!/bin/bash
echo "Starting H2 Server"
java -cp h2-1.4.190.jar org.h2.tools.Server &

gradle bootRun --stacktrace

I basically don't want to have to remember all the commands, and would like to be able to start any iteration of the application with this script (I will eventually add one parameter so that the script lives separately to the code)

What I'd like to happen is have the H2 server shut down upon exit of the main gradle bootRun task.

2 Answers

Lewis Cowles
Lewis Cowles
74,902 Points

After a little digging on http://www.h2database.com (which also seems to be where the h2*.jar in Chris's code came from) I found this little gem

java org.h2.tools.Server -tcpShutdown tcp://localhost:9092

it needs the classpath, at least on my setup as I have not formally installed the h2 database jar, so below is my script (I'll test it out on different revisions, see how it goes)

#!/bin/bash
echo "Starting H2 Server"
java -cp h2-1.4.190.jar org.h2.tools.Server &

echo "Opening Application URL in Browser"
x-www-browser 'http://127.0.0.1:8080'&

gradle bootRun --stacktrace

echo "Killing H2 Server"
# From http://www.h2database.com/html/tutorial.html#using_server
java -cp h2-1.4.190.jar org.h2.tools.Server -tcpShutdown tcp://localhost:9092
Brice Roberts
Brice Roberts
22,415 Points

Alternatively, you can create a servlet bean as a new class.

@Configuration
public class WebConsoleConfig {

  //create servlet that serves H2 console to /console/
  @Bean
  ServletRegistrationBean h2servletRegistration(){
    ServletRegistrationBean registrationBean = new ServletRegistrationBean( new WebServlet());
    registrationBean.addUrlMappings("/console/*");
    return registrationBean;
  }
}

That you could use to access the console at /localhost:8080/console