• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

embedded DB in spring mvc application

 
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am creating a spring mvc crud application and using H2 as the embedded database using the following config:
<jdbc:embedded-database id="dataSource" type="H2">
<jdbc:script location="classpath:schema.sql"/>
<jdbc:script location="classpath:test-data.sql"/>
</jdbc:embedded-database>

The h2 db works fine for me. My only problem is that whenever I restart the server, all my changes are lost as each time the server starts it creates the DB again. Is there a way to store that information and not wipe it on restart. The reason I want this is because I don't want a separate DB server installed. I want to create a war file and all the client has to do is deploy this file and not care about installing a db. This is a small desktop application which would not have huge data hence I want to use an embedded db.
 
Rancher
Posts: 2759
32
Eclipse IDE Spring Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I haven't done this with jdbc:embedded-database. I've done it by configuring a BasicDataSource, and specifying HSQL in the jdbc url. The JDBC URL for the HSQL can points to a file in the local system.
 
andy kumar
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It is possible for you to share the configuration info. Also if I do specify the url, don't I have to manually start the DB before running the app?
 
andy kumar
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok I got it working here it is:

<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="org.h2.Driver" />
<property name="url" value="jdbc:h2:~/myDB" />
</bean>

Now when I start my webapp it will create the DB in my home folder by name myDB.h2.db. I can also initialize my db using the below config:

<jdbc:initialize-database data-source="dataSource" >
<jdbc:script location="classpath:schema.sql"/>
<jdbc:script location="classpath:test-data.sql"/>
</jdbc:initialize-database>

My second requirement was to initialize the DB only when I run my wepapp for the very first time and after that I should use the same old DB, so for that i had to add ignore-failures="ALL" to my init config i.e:
<jdbc:initialize-database data-source="dataSource" ignore-failures="ALL">

Even though I consider the ignore-failures as a hack it solves my purpose right now.

 
What does a metric clock look like? I bet it is nothing like this tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic