Hi,

I'm sorry but I would rather let it be the responsibility of the client to
use the right database URL. H2 already supports various mechanisms (the
baseDir option, relative, absolute, relative to the user home directory). I
fear that adding yet another mechanism would only add complexity, and there
would be a higher risk that people make something wrong (believe me, there
are a lot of people that have problems with the current features). If at
all, then the current mechanism should be simplified. For example, not
allow relative path of the form jdbc:h2:test, but require to use absolute
paths.

Regards,
Thomas



On Wednesday, August 7, 2013, wrote:

> Hi all,
>
> Because my web applications runs in bot Windows and Linux, i have a
> problem: I need to create h2 database ALWAYS into LOCAL TOMCAT
> (catalina.home) directory in server (-tcpAllowOthers) mode.
>
> So, this is not possibile, because h2 requires absolute path (or relative
> to running dir or user home), and this differs from linux to windows.
>
>
> Following this suggestion relative to environment variables into tomcat's
> web.xml
> http://www.coderanch.com/t/79094/Websphere/environment-variable-referance-Web-xmli've
>  created a custom listener (based on h2's DbStarter listener ).
>
> *ATTENTION: this works only if web application and db are both in same
> machine and in same web server, because the variable resolver is system
> restricted.*
>
>
> public class DbStarter implements ServletContextListener {
>
>     private Connection conn;
>     private Server server;
>
>     public void contextInitialized(ServletContextEvent
> servletContextEvent) {
>         try {
>             org.h2.Driver.load();
>
>             // This will get the setting from a context-param in web.xml
> if defined:
>             ServletContext servletContext =
> servletContextEvent.getServletContext();
>             String url = 
> *resolveEnvironmentVariable(*getParameter(servletContext,
> "db.url", "jdbc:h2:~/test")*);*
>             String user = getParameter(servletContext, "db.user", "sa");
>             String password = getParameter(servletContext, "db.password",
> "sa");
>
>             // Start the server if configured to do so
>             String serverParams = getParameter(servletContext,
> "db.tcpServer", null);
>             if (serverParams != null) {
>                 String[] params = StringUtils.arraySplit(serverParams, '
> ', true);
>                 server = Server.createTcpServer(params);
>                 server.start();
>             }
>
>             // To access the database in server mode, use the database URL:
>             // jdbc:h2:tcp://localhost/~/test
>             conn = DriverManager.getConnection(url, user, password);
>             servletContext.setAttribute("connection", conn);
>         } catch (Exception e) {
>             e.printStackTrace();
>         }
>     }
>
>     private String getParameter(ServletContext servletContext, String key,
> String defaultValue) {
>         String value = servletContext.getInitParameter(key);
>         return value == null ? defaultValue : value;
>     }
>
>    * /**
>      * Converts ${something} into relative environment variable value.
>      *
>      *
>      * @param value
>      * @return
>      */
>     public static String resolveEnvironmentVariable(String value) {
>         Properties props = System.getProperties();
>         if (value == null) {
>             return null;
>         }
>         String initialValue = null;
>         final Pattern p =
> Pattern.compile("\\$\\{([\\p{Alnum}|\\.|\\-]*)\\}");
>         while (!value.equals(initialValue)) {
>             initialValue = value;
>             final Matcher m = p.matcher(value);
>             while (m.find()) {
>                 final String propName = m.group(1);
>                 final String propValue = props.getProperty(propName, null);
>                 if (propValue != null) {
>                     value = value.replaceAll("\\$\\{" + propName + "\\}",
> Matcher.quoteReplacement(propValue));
>                 }
>             }
>         }
>         return value;
>     }*
>
>     /**
>      * Get the connection.
>      *
>      * @return the connection
>      */
>     public Connection getConnection() {
>         return conn;
>     }
>
>     public void contextDestroyed(ServletContextEvent servletContextEvent) {
>         try {
>             Statement stat = conn.createStatement();
>             stat.execute("SHUTDOWN");
>             stat.close();
>         } catch (Exception e) {
>             e.printStackTrace();
>         }
>         try {
>             conn.close();
>         } catch (Exception e) {
>             e.printStackTrace();
>         }
>         if (server != null) {
>             server.stop();
>             server = null;
>         }
>     }
> }
>
>
> Bold code is the added code.
> In this mode, my web.xml h2 db.url context parameter looks like this:
>
> <context-param>
>         <param-name>db.url</param-name>
>         <param-value>jdbc:h2:*${catalina.home}*
> /AXLOGDB/axLogDB</param-value>
> </context-param>
>
>
> Now the db is created into tomcat's home /AXLOGDB/axLogDB !!!!
>
> If i need to attach a connection pooling (like c3p0 or commons dbcp or
> tomcat dbcp), i can call the custom DbStarter's *resolveEnvironmentVariable
> *method, in this way:
>
> somedbcp.setJdbcUrl(*DbStarter.resolveEnvironmentVariable*
> ("jdbc:h2:tcp://localhost/${catalina.home}/AXLOGDB/axLogDB"));
>
>
> So, it works!
>
>
> I hope that a *resolveEnvironmentVariable *method* *could be included by
> default in h2 path resolver, to allow a standard way to include local
> variable resolver in h2 (and remote connection with remote variable
> resolver).
>
>
> Feel free to post suggestions or comments about my methods. And CORRECT ME
> if i am wrong.
>
> Best regards,
>
> Agharta
>
>
>
>  --
> You received this message because you are subscribed to the Google Groups
> "H2 Database" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected] <javascript:_e({},
> 'cvml', 'h2-database%[email protected]');>.
> To post to this group, send email to 
> [email protected]<javascript:_e({}, 'cvml', 
> '[email protected]');>
> .
> Visit this group at http://groups.google.com/group/h2-database.
> For more options, visit https://groups.google.com/groups/opt_out.
>
>
>

-- 
You received this message because you are subscribed to the Google Groups "H2 
Database" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/h2-database.
For more options, visit https://groups.google.com/groups/opt_out.


Reply via email to