titaniumdecoy
titaniumdecoy

Reputation: 19251

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?

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

Thanks in advance.


UPDATE: It appears that relative paths are not relative to the context.xml file, but to CATALINA_HOME. Unfortunately that directory (where Tomcat is located) is in another user's home folder (1) to which I have ony read access:

(1) /home/myprof/Tomcat/bin/
(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. However, since all of the accounts are on a shared virtual network, CATALINA_HOME (1) will not change.


Also, how can I get the path to the database file my.db from the context.xml file above inside a servlet?

Upvotes: 4

Views: 23236

Answers (6)

gagarwa
gagarwa

Reputation: 1492

This answer is already given by honzas. I am reiterating it to make it more clear (b/c I also didn't understand it at first).

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.

Upvotes: 1

titaniumdecoy
titaniumdecoy

Reputation: 19251

I am still looking for a way to use a relative path to the database in the context.xml file.

However, I figured out how to get extract the path to the database from the context.xml file. 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=".

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;

Upvotes: 1

titaniumdecoy
titaniumdecoy

Reputation: 19251

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.

Upvotes: 0

fero46
fero46

Reputation: 125

I had the same issue with Spring and jdbc.properties I fix that by removing // and write the relative path

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>

Upvotes: 0

millimoose
millimoose

Reputation: 39950

As a different approach: did you try to use 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. I know H2 does, and I can nothing but recommend it as an embedded Java DB. 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}. (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.)

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?

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.

Upvotes: 1

honzas
honzas

Reputation: 1831

I have found following examples of using JDBC with SQLite:

  • For memory access:

     jdbc:sqlite::memory:
    
  • For relative paths:

     jdbc:sqlite:relative/path.db
    
  • For absolute paths:

     jdbc:sqlite:/absolute/path.db
    

Upvotes: 9

Related Questions