jdbc - Embedding an H2 Database within the WEB-INF Directory -


i have embedded h2 database i'd put in web-inf directory of web application.

what proper way refer in jdbc url?

ideally i'd solution work both war, , expanded war (if possible).

thank-you help!

fyi, i've tried following:

jdbc:h2:/web-inf/data/mydb;cipher=aes 

but results in:

org.h2.jdbc.jdbcsqlexception: file path implicitly relative current working directory not allowed in database url "jdbc:h2:/web-inf/data/mydb;cipher=aes". use absolute path, ~/name, ./name, or basedir setting instead. [90011-187] 

changing to: jdbc:h2:./web-inf/data/mydb;cipher=aes

results in following error, shows trying put database in tomcat's bin directory, rather true web-inf directory want it:

org.h2.jdbc.jdbcsqlexception: error while creating file "c:/program files/apache software foundation/tomcat 7.0/bin/web-inf" [90062-187] 

i managed make embedded solution work without aes this:

try {     class.forname("org.h2.driver");     connection conn = drivermanager.getconnection(         "jdbc:h2:" + getservletcontext().getrealpath("/") +          "/web-inf/data/mydb", "sa", "");     statement stmt = conn.createstatement();     resultset rs = stmt.executequery("select * information_schema.tables");     while (rs.next()) {     }     rs.close();     stmt.close();     conn.close(); } catch(sqlexception e) { } catch(classnotfoundexception e) { } { } 

this tested h2 1.3.176 on tomcat8. should work h2 1.4 , cipher=aes provided embedded database inside war file guess.

the idea following: need absolute path, , deployment path may not same depending on how deployed war file.

so need use servlet context , request real path. use getservletcontext().getrealpath("/") , append /web-inf/data/mydb per needs.

i did not test cipher=aes part i've never used it.

update:

getting reference servlet context tricky. 1 use raw request, underlying session , servlet context.

but have embedded h2 database opened application deployed/started in tomcat, , closed application stopped.

in order perform that, use of listener needed. here's propose update previous answer. time solution complete aes cipher , should easy plug code.

suggestion: listener java code can modified start h2 tcp server well, useful enable automatic mixed mode (embedded+tcp).

add 3 lines file web.xml:

<listener>   <listener-class>com.mine.myservletcontextlistener</listener-class> </listener> 

file myservletcontextlistener.java:

package com.mine;  import javax.servlet.*; import java.sql.*;  public class myservletcontextlistener implements servletcontextlistener {   connection conn;    public void contextinitialized(servletcontextevent sce) {      try {       class.forname("org.h2.driver");       conn = drivermanager.getconnection( "jdbc:h2:" + sce.getservletcontext().getrealpath("/") + "/web-inf/data/mydb;cipher=aes", "sa", "aespassword dbpassword");       statement stmt = conn.createstatement();       resultset rs = stmt.executequery("select * information_schema.tables");       while (rs.next()) {       }       rs.close();       stmt.close();     } catch(sqlexception e) {     } catch(classnotfoundexception e) {     } {     }    }    public void contextdestroyed(servletcontextevent sce) {      try {       conn.close();     } catch(sqlexception e) {     } {     }    }  } 

Comments

Popular posts from this blog

node.js - Using Node without global install -

How to access a php class file from PHPFox framework into javascript code written in simple HTML file? -

java - Null response to php query in android, even though php works properly -