[英]Relative path to hsqldb files in a web app doesn't work?
I'm using hsqldb for my Spring-based java webapp.我将 hsqldb 用于基于 Spring 的 java webapp。 I put database files (mydb.lck, mydb.properties,..) in src\main\java\data folder so that they're published into WEB-INF\classes\data.
我将数据库文件(mydb.lck、mydb.properties、..)放在 src\main\java\data 文件夹中,以便将它们发布到 WEB-INF\classes\data 中。
In datasource configuration, I specify this relative path to JVM working directory.在数据源配置中,我将此相对路径指定为 JVM 工作目录。 As guided in hsqldb documents.
按照 hsqldb 文档中的指导。
portal.jdbc.url=jdbc:hsqldb:file:/data/mydb
(Is this seperator right for Windows?) portal.jdbc.url=jdbc:hsqldb:file:/data/mydb
(这个分隔符适合 Windows 吗?)
But Spring seem not find this path and insist on claiming但是Spring似乎没有找到这条路并坚持要求
java.sql.SQLSyntaxErrorException: user lacks privilege or object not found: CUSTOMER org.hsqldb.jdbc.Util.sqlException(Unknown Source)
However, if I specify an absolute path, it works flawlessly但是,如果我指定一个绝对路径,它会完美地工作
portal.jdbc.url=jdbc:hsqldb:file:d:\\TomcatServer\\apache-tomcat-7.0.10\\wtpwebapps\\myportal-app\\data\\mydb
Should I miss understanding JVM working directory on a web app?我应该不了解 Web 应用程序上的 JVM 工作目录吗? Any help is appreciated.
任何帮助表示赞赏。
It seems that Tomcat doesn't provide us a properties variable (like "webroot") to refer to my application context.似乎 Tomcat 没有为我们提供属性变量(如“webroot”)来引用我的应用程序上下文。 So my solutions is to register such a properties in Servlet context listener.
所以我的解决方案是在 Servlet 上下文侦听器中注册这样的属性。
My code:我的代码:
public class WebAppPropertiesListener implements ServletContextListener{
@Override
public void contextInitialized(ServletContextEvent sce) {
String rootPath = sce.getServletContext().getRealPath("/");
System.setProperty("webroot", rootPath);
}
...
}
And add listener in web.xml before Spring context is triggered并在触发 Spring 上下文之前在 web.xml 中添加监听器
<listener>
<listener-class>com.iportal.util.WebAppPropertiesListener</listener-class>
</listener>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
Then I put the property in Hsqldb setting.然后我把属性放在 Hsqldb 设置中。
portal.jdbc.url=jdbc:hsqldb:file:${webroot}WEB-INF/classes/data/mydb
Hope this helpful for someone who may run into the same problem.希望这对可能遇到同样问题的人有所帮助。
HSQLDB 2.2.8 and later allows a variable in the connection URL. HSQLDB 2.2.8 及更高版本允许在连接 URL 中使用变量。 The variable can be any system property, such as the web application directory path.
该变量可以是任何系统属性,例如 Web 应用程序目录路径。
http://hsqldb.org/doc/2.0/guide/dbproperties-chapt.html#dpc_variables_url http://hsqldb.org/doc/2.0/guide/dbproperties-chapt.html#dpc_variables_url
Refer Section "Variables In Connection URL"请参阅“连接 URL 中的变量”部分
http://hsqldb.org/doc/2.0/guide/dbproperties-chapt.html http://hsqldb.org/doc/2.0/guide/dbproperties-chapt.html
Example例子
jdbc:hsqldb:file:${mydbpath};sql.enforce_types=true jdbc:hsqldb:文件:${mydbpath};sql.enforce_types=true
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.