Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thospfuller/59d5de080fb4771709c00b387fa20af8 to your computer and use it in GitHub Desktop.
Save thospfuller/59d5de080fb4771709c00b387fa20af8 to your computer and use it in GitHub Desktop.
Example regarding how to configure the H2 Database web server programmatically.
package com.thospfuller
/**
* Tutorial regarding how to configure the H2 Database web server programmatically.
*
* Note that if we don't use the systemClassLoader we'll get the following exception:
*
* java.sql.SQLException: No suitable driver found for jdbc:h2:mem:example-db
*/
@GrabConfig(systemClassLoader=true)
@Grapes(
@Grab(group='com.h2database', module='h2', version='2.2.224')
)
import java.sql.DriverManager
import org.h2.tools.Server
def connection = DriverManager.getConnection("jdbc:h2:mem:example-db", "sa", "sa")
/* webPort : H2 console port, set to 19999 for this example.
*
* webSSL : HTTPS connections.
*/
def server = Server.createWebServer("-webPort", "19999", "-webSSL").start()
def url = server.getURL ()
def minutes = 60L
println "Server URL: ${url}, will sleep for $minutes minutes."
Server.openBrowser (url)
try {
Thread.currentThread ().sleep (1000 * 60 * minutes)
} catch (InterruptedException interruptedException) {
interruptedException.printStackTrace (System.err)
} finally {
connection.close ()
server.stop ()
}
println "...done!"
return
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment