Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set port number for the embedded h2 database

Tags:

spring

heroku

h2

I use h2 in-memory database only for the testing purposes. The default port seems to be 8082, which causes my tests to fail on heroku.

I want to change this port number. How can I do that ?

What I've done so far:

  1. There seems to be a file on my local machine ($USER_HOME/.h2.server.properties) that specifies this port. Let alone it is very strange to have that file outside of the application workspace, I cannot check it in heroku.

  2. I tried to set webPort, port in application-test.properties (it is a properties file I use for my tests) but it didn't work (I tried different combinations server.port, `spring.data.source.h2.webPort, etc.)

application-test.properties

spring.datasource.url=jdbc:h2:mem:tesdb;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE
spring.datasource.driverClassName=org.h2.Driver
spring.datasource.username=sa
spring.datasource.password=
spring.datasource.testWhileIdle = true
spring.datasource.validationQuery = SELECT 1

build.gradle

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:1.5.8.RELEASE")
    }
}

apply plugin: 'java'

sourceCompatibility = 1.8
targetCompatibility = 1.8
tasks.withType(JavaCompile) {
    options.encoding = 'UTF-8'
}

test{

    forkEvery = 1
    testLogging {
        showStandardStreams = true
    }
}

repositories {
     maven { url "http://repo.maven.apache.org/maven2" }
}

dependencies {
    compile group: 'org.springframework.boot', name: 'spring-boot-starter-web', version:'1.5.8.RELEASE'
    compile group: 'org.springframework.boot', name: 'spring-boot-starter-data-jpa', version:'1.5.8.RELEASE'
    compile group: 'mysql', name: 'mysql-connector-java', version:'5.1.44'
    compile group: 'org.javassist', name: 'javassist', version:'3.18.0-GA'
    compile group: 'org.springframework.boot', name: 'spring-boot-starter-security', version:'1.5.8.RELEASE'
    compile group: 'org.springframework.security.oauth', name: 'spring-security-oauth2', version:'2.0.14.RELEASE'
    compile group: 'org.springframework.boot', name: 'spring-boot-configuration-processor', version:'1.5.8.RELEASE'
    compile group: 'io.jsonwebtoken', name: 'jjwt', version:'0.7.0'
    compile group: 'org.springframework.security', name: 'spring-security-cas', version:'4.2.3.RELEASE'
    compile group: 'org.springframework.security', name: 'spring-security-acl', version:'4.2.3.RELEASE'
    compile group: 'bsf', name: 'bsf', version:'2.4.0'
    compile group: 'org.springframework.boot', name: 'spring-boot-starter-batch', version:'1.5.8.RELEASE'
    compile group: 'org.springframework.boot', name: 'spring-boot-starter-actuator', version:'1.5.8.RELEASE'
    compile group: 'org.springframework.security', name: 'spring-security-ldap', version:'4.2.3.RELEASE'
    compile group: 'javax.jdo', name: 'jdo-api', version:'3.0.1'
    compile group: 'com.querydsl', name: 'querydsl-core', version:'4.1.4'
    compile group: 'org.springframework', name: 'springloaded', version:'1.2.8.RELEASE'
    compile group: 'org.springframework.boot', name: 'spring-boot-starter-data-rest', version:'1.5.8.RELEASE'
    compile group: 'org.jetbrains.kotlin', name: 'kotlin-stdlib', version:'1.0.6'
    compile group: 'org.scala-lang', name: 'scala-library', version:'2.11.0'
    compile group: 'commons-validator', name: 'commons-validator', version:'1.6'
    compile group: 'commons-io', name: 'commons-io', version:'2.5'
    compile group: 'org.springframework.boot', name: 'spring-boot-starter-jersey', version:'1.5.8.RELEASE'
    compile group: 'org.modelmapper', name: 'modelmapper', version:'0.7.5'
    compile(group: 'org.apache.httpcomponents', name: 'httpclient', version:'4.5.3') {
exclude(module: 'commons-logging')
    }
    compile group: 'org.apache.commons', name: 'commons-lang3', version:'3.4'
    compile(group: 'org.springframework.boot', name: 'spring-boot-starter-test', version:'1.5.8.RELEASE') {
exclude(module: 'commons-logging')
    }
    compile group: 'org.eclipse.jetty.aggregate', name: 'jetty-all', version:'9.2.13.v20150730'
    compile group: 'com.h2database', name: 'h2', version:'1.4.196'
    compile group: 'org.springframework.integration', name: 'spring-integration-test', version:'4.3.12.RELEASE'
    compile group: 'org.springframework.security', name: 'spring-security-test', version:'4.2.3.RELEASE'
    compile group: 'org.apache.hadoop', name: 'hadoop-core', version:'1.0.0'
    compile group: 'com.sleepycat', name: 'je', version:'5.0.73'
    compile group: 'commons-dbcp', name: 'commons-dbcp', version:'1.4'
    compile group: 'org.springframework', name: 'spring-webmvc', version:'4.3.12.RELEASE'
    compile group: 'org.springframework.data', name: 'spring-data-commons', version:'1.13.8.RELEASE'
    compile group: 'org.springframework.hateoas', name: 'spring-hateoas', version:'0.23.0.RELEASE'
    compile group: 'org.springframework.data', name: 'spring-data-rest-core', version:'2.6.8.RELEASE'
    compile group: 'org.springframework.plugin', name: 'spring-plugin-core', version:'1.2.0.RELEASE'
    compile group: 'org.springframework', name: 'spring-orm', version:'4.3.12.RELEASE'
    testCompile group: 'junit', name: 'junit', version:'4.11'
    testCompile group: 'org.springframework', name: 'spring-test', version:'4.3.12.RELEASE'
}
like image 891
Arian Avatar asked Dec 01 '17 06:12

Arian


People also ask

What is the default port for H2 database?

However, the default port is 8080, and that port is already being used on my machine. EDIT As mentioned by Atul K below, the h2 web console is available on the configured server.

How do I connect to an embedded H2 database?

Connect to the embedded H2 database using the H2 console Alternatively you can connect using the browser based H2 console. The easiest way to access the console is to double click the H2 database jar file at <installation-directory>\confluence\WEB-INF\lib\h2-x.x.x.jar .

What port is H2 console?

Accessing the H2 Console Then, after starting the application, we can navigate to http://localhost:8080/h2-console, which will present us with a login page.


1 Answers

I found the solution. Earlier, I created a base class for my tests, where I create my H2 server. The port was set in the same class.

@BeforeClass
public static void init() throws SQLException {
    webServer = Server.createWebServer("-web", "-webAllowOthers", "-webPort", "8082");
    webServer.start();
}

But changing the port doesn't solve the problem. I found out that in contrary to how eclipse runs each test separately, heroku runs all of them once, so the server doesn't get destroyed and is kept alive between two tests. Therefore I have to kill the server after each test class is finished running:

@AfterClass
public static void tearDown() throws SQLException {
    webServer.stop();
}
like image 94
Arian Avatar answered Nov 16 '22 02:11

Arian