加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

Using sqlite with Grails

发布时间:2020-12-12 20:10:58 所属栏目:百科 来源:网络整理
导读:http://flnkr.com/2011/06/using-sqlite-with-grails/ I’m working on a relatively small application for a some folks without an IT staff. In an effort to simplify the deployment,I’m planning on using H2 as the production database. It will w

http://flnkr.com/2011/06/using-sqlite-with-grails/


I’m working on a relatively small application for a some folks without an IT staff. In an effort to simplify the deployment,I’m planning on using H2 as the production database. It will work just fine for five users,and will prevent them from having to install and maintain MySQL or something else.

H2 is the default database in Grails,but you’ll need to make some changes to the production configuration for using a file-based H2 database with Tomcat. Here is the production section of my DataSource.groovy file:

production {
    dataSource {
        dbdir ="${System.properties['catalina.base']}/db/sample"
 
        dbCreate ="update"
        url ="jdbc:h2:file:${dbdir};MVCC=TRUE;LOCK_TIMEOUT=10000"
        pooled =true
        username ="sa"
        password ="db!admin"
 
        properties {
            maxActive =-1
            minEvictableIdleTimeMillis=1800000
            timeBetweenEvictionRunsMillis=1800000
            numTestsPerEvictionRun=3
            testOnBorrow=true
            testWhileIdle=true
            testOnReturn=true
            validationQuery="SELECT 1"}}}

Line 3 sets the location and name of the database file. In this case,I’m reading the system property set by Tomcat to determine the location of running instance (catalina.base) and specifying the db will be in a directory called sample in the db directory. You want to create the db directory first. For example,here’s what my Tomcat 7 directory looks like:

tomcat.png

On line 5,you’ll want to use update or validate as the dbCreate option so as not to blow away the database each time when you start the application. I prefer update so that Grails can keep simple fast-forward changes in sync.

Finally,on line 6,we build the full datasource URL,substituting in the file location we built above on line 3. I’m setting a password for the database on line 9,but that is optional and really depends on how secure you want the database to be.

Now,when you deploy the WAR file of the Grails application to Tomcat,it will use the embedded H2 database for production,saving the file under the Tomcat location. You can also put the DB file anywhere you want,but my goal was to keep the whole application (Tomcat + Grails) together so I could provide it as a preconfigured Zip file.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读