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]. 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.
