3
votes

create a sqlite database at build time with the following Maven configuration.

            <execution>
                <id>create-db</id>
                <phase>compile</phase>
                <goals>
                    <goal>execute</goal>
                </goals>
                <configuration>
                    <url>jdbc:sqlite:src/main/webapp/WEB-INF/sqlitedb.db</url>
                    <driver>org.sqlite.JDBC</driver>
                    <enableAnonymousPassword>true</enableAnonymousPassword>
                    <autocommit>true</autocommit>
                    <srcFiles>
                        <srcFile>src/main/sql/build.sql</srcFile>
                    </srcFiles>
                </configuration>
            </execution> 

This gets packaged into the war file. It then gets referenced from the peristence.xml.

        <property name="hibernate.connection.url" value="jdbc:sqlite:????/sqlitedb.db"/>

and the spring configuration.

   <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close" lazy-init="true">
        <property name="driverClassName" value="org.sqlite.JDBC"/>
        <property name="url" value="jdbc:sqlite:????/sqlitedb.db"/>
        <property name="minIdle" value="2"/>
        <property name="maxActive" value="20"/>
        <property name="maxIdle" value="5"/>
        <!--<property name="poolPreparedStatements" value="true" />-->
    </bean>

What I can't figure out is how to make the Spring configuration and the Hibernate configuration look into the war file for the sqlitedb.db.

2

2 Answers

11
votes

Answering it after 2 years of this post :) ; might help a lot of people:

use this url: "jdbc:sqlite:${catalina.base}/webapps/package_name/WEB-INF/classes/sqlitedb.db"

Considering you have placed your sqlitedb.db file in project resources.

0
votes

If you want to connect to Payara, or Glassfish. You would use something similar a JDBC URL like the following:

jdbc:sqlite:${com.sun.aas.instanceRoot}/applications/APPLICATION_NAME/WEB-INF/sqlite.db

Where APPLICATION_NAME is the name of your deployment.