简介
H2(http://www.h2database.com)数据库是一个使用纯Java编写的、内嵌模式和客户服务器模式的内存数据库。H2十分适合作为嵌入式数据库使用。比如:手机应用程序。
运行模式
在H2数据库服务器支持以下运行模式:
1. 嵌入模式
这种模式下,数据库和程序运行在同一个进程中。访问数据库不需要经过网络。
2. 客户/服务器模式
这是传统的模式,其他数据库比如Oracle, SQL Server都是采用这种模式运行。这种方式客户端使用网络与服务器交互,速度要比第一种方式慢。
3. 混合模式
这种模式综合了以上两种模式。
连接模式
H2数据库支持的链接模式有以下几种:
· 针对使用嵌入式模式运行的数据库,支持使用JDBC本地连接。
· 针对客户/服务器模式运行的数据库,支持通过TCP/IP使用JDBC或者ODBC进行连接。
· 针对混合模式运行的数据库,同时支持一项两种连接方式。
内部服务
H2数据库内部,包含三个服务:
· webServer
用于支持web界面管理和访问数据库的web服务器。
· tcpServer
用于支持JDBC连接和访问的TCP服务。
· pgServer
用于支持使用PostgreSQLODBC连接和访问的服务。
注:H2数据库本身不带ODBC驱动程序,H2通过使用PostgreSQLODBC来支持ODBC连接访问。可以到这里下载相关驱动程序:http://www.postgresql.org/ftp/odbc/versions/msi。以上的pg是PostgresSQL缩写。
运行H2数据库
H2数据库支持多种方式运行。其中包括命令行服务器模式和Web模式。
命令行运行H2数据库服务器
可以通过在命令行中运行以下命令来启动H2数据库
java -cp h2*.jar org.h2.tools.Server |
默认情况下,系统会启动webServer, tcpServer和pgServer三个服务。
数据库嵌入web模式
可以通过在web.xml加入以下配置来启动数据库
<listener> <listener-class>org.h2.server.web.DbStarter</listener-class> </listener> <context-param> <param-name>db.url</param-name> <param-value>jdbc:h2:~/test</param-value> </context-param> <context-param> <param-name>db.user</param-name> <param-value>sa</param-value> </context-param> <context-param> <param-name>db.password</param-name> <param-value>sa</param-value> </context-param> <context-param> <param-name>db.tcpServer</param-name> <param-value>-tcpAllowOthers</param-value> </context-param> |
在这种模式下,系统支持运行webServer和tcpServer。不支持运行pgServer。针对这个问题,我们通过修改H2的以下源代码实现。
从H2的源码中找到以下Java文件:
src\main\org\h2\server\web\ DbStarter.java
做以下修改:
/* * Copyright 2004-2014 H2 Group. Multiple-Licensed under the MPL 2.0, * and the EPL 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 tcpServer; private Server pgServer;
@Override 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); tcpServer = Server.createTcpServer(params); tcpServer.start(); }
// Start the pg server if configured to do so String pgServerParams = getParameter(servletContext, "db.pgServer", null); if (pgServerParams != null) { String[] params = StringUtils.arraySplit(pgServerParams, ' ', true); pgServer = Server.createPgServer(params); pgServer.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; }
@Override 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 (tcpServer != null) { tcpServer.stop(); tcpServer = null; }
if (pgServer != null) { pgServer.stop(); pgServer = null; } }
} |
将以上文件编译,然后需要放到web服务器上。我使用的是Tomcat服务器,以下是我放置的位置:
C:\Program Files\Apache Software Foundation\Tomcat6.0\webapps\test\WEB-INF\classes\org\h2\server\web
然后在web.xml文件中加入以下配置:
<context-param> <param-name>db.pgServer</param-name> <param-value>-pgAllowOthers</param-value> </context-param> |
通过一项修改,然后启动Web服务器,就可以使用ODBC访问运行在Web应用的H2数据库了。