日期:2014-05-16  浏览次数:20383 次

H2数据库的项目使用(二)
   项目使用H2数据库,部署到服务器上以后,开始我打算的是,单独写一个sh文件然后再项目安装包里面调用,比如写一个exech2.sh 里面就这样调用

   #!/bin/bash

   exec h2.sh &

这样做的坏处是单独开启了一个进程,不方便管理,然后看了下he.pdf文档里面有介绍如何把H2植入java web项目(第25页的样子), 然后我就试着在自己工程的web.xml文件里面加入了

	<listener>
		<listener-class>org.h2.server.web.DbStarter</listener-class>
	</listener>

	<!-- H2 DB Info -->
	<context-param>
		<param-name>db.url</param-name>
		<param-value>jdbc:h2:tcp://localhost/~/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>


     然后在启动服务以后一直报连接出错,但是居然可以登录界面,想到这个项目使用JPA在数据库里面建表并插值,发现在web.xml里面有这么一段加载
<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>
	classpath:applicationContext.xml
	classpath:applicationContext-security.xml
    </param-value>
</context-param>

同时在applicationContext.xml里面加载了JPA的persistence.xml,也就是说需要先启动H2的数据库,然后再让JPA连接数据库进行建表和插值。
于是把关于H2的配置放在web.xml的最开始的位置,高于filter即可。