Reputation: 49
I have seen several questions broaching this issue, but I haven't come across a clear answer.
I need to package a SQLite .db file into a WAR and work with that DB file within the WAR or exploded WAR.
My project is Spring, using Spring JDBC.
Everything works fine with the .db file in the classpath for testing of the non-web portion of the code. I can run unit tests against the database without problem using just
<bean id="datasource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="connectionInitSqls">
<list>
<value>PRAGMA foreign_keys = ON</value>
</list>
</property>
<property name="driverClassName" value="org.sqlite.JDBC" />
<property name="url" value="jdbc:sqlite:app2.db" />
</bean>
When I go to deploy, I'm able to pull the db into the WAR file only if I place it in the WebContent/resources
directory. I can't seem to get this working with alternate URIs. (for example, jdbc:sqlite::resource:app2.db
, jdbc:sqlite:WebContent:resources:app2.db
, jdbc:sqlite:/resources/app2.db
, etc.). I've seen answers suggesting that I tie it to tomcat's deployment directory location specifically, but I'd really rather not have the location be dependent on anything outside of the WAR.
Does anyone have a working example (using XML config for the datasource in Spring applicationContext) of a datasource mapping (includig jdbc URI string), and corresponding pom.xml maven-war-plugin
specification (if applicable) to achieve the solution of letting the project work with a SQLite .db
file stored with the project in its WAR?
Thanks in advance.
--EDIT (additional clarifying information in response to comments) --
I am able to get the .db into the classes subidirectory of the WAR via this directory (or through something in the WebApps subdirectory by a workaround resource mapping in the maven-war-plugin specification), but this is not the main problem.
My issue is: How do I reference the .db via relative URL/URI once it is within the WAR. I need to point the jdbc driver to this location via a relative path so that it can be accessed regardless of deployment location.
I have options of absolute URL on the filesystem (I've gotten direct location on my computer to work with
or path within Tomcat, but again, by hard-coded URL, or best case, URL tied to CATALINA_BASE with
<property name="url" value="jdbc:sqlite:/var/lib/tomcat7/webapps/APPNAME/WEB-INF/classes/app2db.db" />
I've seen it suggested that I use
<property name="url" value="jdbc:sqlite:${catalina.base}/webapps/APPNAME/WEB-INF/classes/app2db.db" />
though this does not seem to work with my installation (and is still not an optimal solution, as it is tied to the surrounding deployment location).
jdbc:sqlite allows mapping of relative paths using no preceding forward slash, but I can't get this to work within the war with something like
<property name="url" value="jdbc:sqlite:WEB-INF/classes/app2db.db" />
This is what I'm looking for -- the relative path mapping such that I can send the .db off with the WAR and just tell someone to deploy to tomcat -- or any server, within reason, since I have the jars needed to execute mostly packaged up with the app through Maven --(even if their directory structure is unusual or they don't have path variables mapped), and have it still be able to access the database with rw access from within the exploded WAR.
Upvotes: 2
Views: 4223
Reputation: 14762
When I go to deploy, I'm able to pull the db into the WAR file only if I place it in the
WebContent/resources
directory.
If I place my.db
in Maven's default for resources <project>/src/main/resources/my.db
it is packaged in /my.war/WEB-INF/classes/my.db
here.
Upvotes: 2