简体   繁体   English

context.xml中SQLite DB的相对路径

[英]Relative path to SQLite DB in context.xml

Is it possible to use a relative path to an SQLite database file in the context.xml file of a Java web application? 是否可以在Java Web应用程序的context.xml文件中使用SQLite数据库文件的相对路径?

At present, I have: 目前,我有:

<?xml version="1.0" encoding="UTF-8"?>
<Context path="/mywebapp">
    <Resource name="jdbc/mydb" auth="Container" type="javax.sql.DataSource"
        maxActive="100" maxIdle="30" maxWait="10000"
        username="myusername" password="mypassword" driverClassName="org.sqlite.JDBC"
        url="jdbc:sqlite://Users/me/NetBeansProjects/MyApp/web/my.db"/>
</Context>

As you can see, the url attribute of the Resource tag currently specifies an absolute path to my.db. 如您所见,Resource标记的url属性当前指定了my.db的绝对路径。 However, I am working on this project with a small team of developers as a class project. 但是,我正在与一个小型开发团队合作开展这个项目。 We are sharing the code through Subversion and currently, every time one of us updates our project, we have to manually replace the above path with the correct path for our computer. 我们通过Subversion共享代码,目前,每当我们中的一个人更新我们的项目时,我们必须手动将上述路径替换为我们计算机的正确路径。

Thanks in advance. 提前致谢。


UPDATE: It appears that relative paths are not relative to the context.xml file, but to CATALINA_HOME. 更新:相对路径似乎与context.xml文件无关,而是与CATALINA_HOME相关。 Unfortunately that directory (where Tomcat is located) is in another user's home folder (1) to which I have ony read access: 不幸的是,该目录(Tomcat所在的目录)位于另一个用户的主文件夹(1)中,我对其进行了读访问:

(1) /home/myprof/Tomcat/bin/ (1)/ home / myprof / Tomcat / bin /
(2) /home/me/NetBeansProjects/MyApp/web/my.db (2)/home/me/NetBeansProjects/MyApp/web/my.db

I cannot include /me in the relative path because, as stated above, this project will run on multiple computers via a svn repository and the directory in which the project files are contained (2) will change. 我不能在相对路径中包含/ me,因为如上所述,此项目将通过svn存储库在多台计算机上运行,​​并且包含项目文件的目录(2)将更改。 However, since all of the accounts are on a shared virtual network, CATALINA_HOME (1) will not change. 但是,由于所有帐户都在共享虚拟网络上,因此CATALINA_HOME(1)不会更改。


Also, how can I get the path to the database file my.db from the context.xml file above inside a servlet? 另外,如何从servlet中的context.xml文件中获取数据库文件my.db的路径?

I have found following examples of using JDBC with SQLite: 我找到了以下使用JDBC和SQLite的示例:

  • For memory access: 对于内存访问:

      jdbc:sqlite::memory: 
  • For relative paths: 对于相对路径:

      jdbc:sqlite:relative/path.db 
  • For absolute paths: 对于绝对路径:

      jdbc:sqlite:/absolute/path.db 

I am still looking for a way to use a relative path to the database in the context.xml file. 我仍在寻找一种在context.xml文件中使用数据库相对路径的方法。

However, I figured out how to get extract the path to the database from the context.xml file. 但是,我想出了如何从context.xml文件中提取数据库的路径。 It's not pretty, but it works. 它不漂亮,但它的工作原理。 I couldn't figure out how to parse the context.xml file as XML so this ugly hack looks for a line beginning with " url= ". 我无法弄清楚如何将context.xml文件解析为XML,因此这个丑陋的hack会查找以“ url= ”开头的行。

URL url = getServletContext().getResource("/META-INF/context.xml");
Properties props = new Properties();
props.load(url.openStream());
String strval = props.getProperty("url");
Pattern pattern = Pattern.compile("\\\W*\"(.\*)\"\\\W*");
Matcher matcher = pattern.matcher(strval);
matcher.find();
String constr = matcher.group(1);
if (constr != null)
    this.jdbcConnectionString = constr;

As a different approach: did you try to use jdbc:sqlite:~/myapp.db ? 作为一种不同的方法:你是否尝试使用jdbc:sqlite:~/myapp.db This should be a writable location on the computers of everyone running that application. 应该是运行该应用程序的每个人的计算机上的可写位置。 (Although I've seen the evil that are read-only home directories.) (虽然我已经看到了只读主页的邪恶。)

I'm not exactly sure if SQLite supports ~ in the database URL. 我不确定SQLite是否支持数据库URL中的~ I know H2 does, and I can nothing but recommend it as an embedded Java DB. 我知道H2确实如此,我只能推荐它作为嵌入式Java数据库。 If you don't want to change databases, you might want to see if Tomcat allows some parameter substitution in its configuration files, like ${user.home} . 如果您不想更改数据库,您可能希望查看Tomcat是否允许在其配置文件中进行某些参数替换,例如${user.home} (Some preliminary Googling gives me mailing list results that seem to imply that's be case in at least server.xml , so it could be worth just trying it.) (一些初步的谷歌搜索给我的邮件列表结果似乎暗示至少在server.xml是这样的,所以值得尝试它。)

I'm still not exactly sure what you're trying to achieve though: are you the one running the Tomcat instance, or is it a shared tomcat instance running under some system account? 我仍然不确定你想要实现的目标:你是运行Tomcat实例的那个,还是在某个系统帐户下运行的共享tomcat实例?

You could also try to set up another world-writable location on the network to put the database in. Somewhere under /var/ maybe, or just a folder that's set to ug=rwx in one person's home directory. 您还可以尝试在网络上设置另一个可写入世界的位置以放入数据库。位于/var/ maybe下的某个位置,或者只是在一个人的主目录中设置为ug=rwx的文件夹。

I had the same issue with Spring and jdbc.properties I fix that by removing // and write the relative path 我和Spring和jdbc.properties有同样的问题我通过删除//并写入相对路径来解决这个问题

Try this 尝试这个

<?xml version="1.0" encoding="UTF-8"?>
<Context path="/mywebapp">
    <Resource name="jdbc/mydb" auth="Container" type="javax.sql.DataSource"
        maxActive="100" maxIdle="30" maxWait="10000"
        username="myusername" password="mypassword" driverClassName="org.sqlite.JDBC"
        url="jdbc:sqlite:web/my.db"/>
</Context>

I found the answer to my question here . 我在这里找到了我的问题的答案。 The solution is to get the resource rather than try to parse the database URL out of the context.xml file. 解决方案是获取资源而不是尝试从context.xml文件中解析数据库URL。

This answer is already given by honzas. 这个答案已经由honzas给出。 I am reiterating it to make it more clear (b/c I also didn't understand it at first). 我重申它要更清楚(b / c我起初也不理解它)。

jdbc:sqlite:relative/path.db

That is correct for a relative path. 这对于相对路径是正确的。 Notice the lack of a / before relative. 注意缺少一个/之前的亲戚。 That makes the path absolute. 这使得路径绝对。 You can also use this: 你也可以用这个:

jdbc:sqlite:./relative/path.db

The . 的。 refers to the current folder. 指的是当前文件夹。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM