Relative path to hsqldb files in a web app doesn't work?

情到浓时终转凉″ 提交于 2019-12-10 11:15:36

问题


I'm using hsqldb for my Spring-based 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.

In datasource configuration, I specify this relative path to JVM working directory. As guided in hsqldb documents.

portal.jdbc.url=jdbc:hsqldb:file:/data/mydb (Is this seperator right for Windows?)

But Spring seem not find this path and insist on claiming

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? Any help is appreciated.


回答1:


It seems that Tomcat doesn't provide us a properties variable (like "webroot") to refer to my application context. So my solutions is to register such a properties in Servlet context listener.

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

<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.

portal.jdbc.url=jdbc:hsqldb:file:${webroot}WEB-INF/classes/data/mydb

Hope this helpful for someone who may run into the same problem.




回答2:


HSQLDB 2.2.8 and later allows a variable in the connection URL. The variable can be any system property, such as the web application directory path.

http://hsqldb.org/doc/2.0/guide/dbproperties-chapt.html#dpc_variables_url




回答3:


Refer Section "Variables In Connection URL"

http://hsqldb.org/doc/2.0/guide/dbproperties-chapt.html

Example

jdbc:hsqldb:file:${mydbpath};sql.enforce_types=true




回答4:


Figured it out on Tomcat 8 thanks to fredt

url = "jdbc:hsqldb:file:" + mydbpath;

Where mydbpath is a variable with realtive path to the database specified. This somehow works



来源:https://stackoverflow.com/questions/11289245/relative-path-to-hsqldb-files-in-a-web-app-doesnt-work

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!