My favorites | Sign in
Project Home Downloads Issues Source
Checkout   Browse   Changes    
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
/*
* Copyright 2004-2011 H2 Group. Multiple-Licensed under the H2 License,
* Version 1.0, and under the Eclipse Public License, Version 1.0
* (http://h2database.com/html/license.html).
* Initial Developer: H2 Group
*/
package org.h2.server.web;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

import org.h2.tools.Server;
import org.h2.util.StringUtils;

/**
* This class can be used to start the H2 TCP server (or other H2 servers, for
* example the PG server) inside a web application container such as Tomcat or
* Jetty. It can also open a database connection.
*/
public class DbStarter implements ServletContextListener {

private Connection conn;
private Server server;

public void contextInitialized(ServletContextEvent servletContextEvent) {
try {
org.h2.Driver.load();

// This will get the setting from a context-param in web.xml if defined:
ServletContext servletContext = servletContextEvent.getServletContext();
String url = getParameter(servletContext, "db.url", "jdbc:h2:~/test");
String user = getParameter(servletContext, "db.user", "sa");
String password = getParameter(servletContext, "db.password", "sa");

// Start the server if configured to do so
String serverParams = getParameter(servletContext, "db.tcpServer", null);
if (serverParams != null) {
String[] params = StringUtils.arraySplit(serverParams, ' ', true);
server = Server.createTcpServer(params);
server.start();
}

// To access the database in server mode, use the database URL:
// jdbc:h2:tcp://localhost/~/test
conn = DriverManager.getConnection(url, user, password);
servletContext.setAttribute("connection", conn);
} catch (Exception e) {
e.printStackTrace();
}
}

private static String getParameter(ServletContext servletContext, String key, String defaultValue) {
String value = servletContext.getInitParameter(key);
return value == null ? defaultValue : value;
}

/**
* Get the connection.
*
* @return the connection
*/
public Connection getConnection() {
return conn;
}

public void contextDestroyed(ServletContextEvent servletContextEvent) {
try {
Statement stat = conn.createStatement();
stat.execute("SHUTDOWN");
stat.close();
} catch (Exception e) {
e.printStackTrace();
}
try {
conn.close();
} catch (Exception e) {
e.printStackTrace();
}
if (server != null) {
server.stop();
server = null;
}
}

}

Change log

r3339 by thomas.tom.mueller on Feb 1, 2011   Diff
Make methods static if possible.
Go to: 
Project members, sign in to write a code review

Older revisions

r3289 by thomas.tom.mueller on Jan 13, 2011   Diff
Change copyright years.
r3161 by thomas.tom.mueller on Nov 30, 2010   Diff
The DbStarter servlet context listener
now starts the server before opening a
connection, so that connecting using
the server mode works.
r3136 by thomas.tom.mueller on Nov 20, 2010   Diff
Various smaller changes (FindBugs)
All revisions of this file

File info

Size: 2968 bytes, 90 lines
Powered by Google Project Hosting