title: Embedded H2 + Tomcat 7 JDBC realm + Hibernate = "Locked by another process"
body: <p>Perhaps the title is self explanatory, but I am trying to create a web application with an embedded instance of the H2 database. I am configuring Tomcat 7 to use the JDBC realm to form-based authentication. <code>server.xml</code> has:</p>
  
  <pre><code>&lt;Realm className="org.apache.catalina.realm.JDBCRealm"
  driverName="org.h2.Driver"
  connectionURL="jdbc:h2:/someDir/myDB"
  connectionName="userName"
  connectionPassword="password"
  userTable="user_enabled"
  userNameCol="user_name"
  userCredCol="pass"
  userRoleTable="user_role"
  roleNameCol="role_name" /&gt;
  </code></pre>
  
  <p>I am also using Hibernate for persistence. <code>persistence.xml</code> has:</p>
  
  <pre><code>&lt;property name="hibernate.connection.driver_class" value="org.h2.Driver" /&gt;
  &lt;property name="hibernate.connection.url" value="jdbc:h2:/someDir/myDB" /&gt;
  &lt;property name="hibernate.connection.username" value="userName" /&gt;
  &lt;property name="hibernate.connection.password" value="password" /&gt;
  &lt;property name="hibernate.dialect" value="org.hibernate.dialect.H2Dialect" /&gt;
  &lt;property name="hibernate.hbm2ddl.auto" value="validate" /&gt;
  &lt;property name="hibernate.show_sql" value="true" /&gt;
  &lt;property name="current_session_context_class" value="org.hibernate.context.internal.ThreadLocalSessionContext" /&gt;
  </code></pre>
  
  <p>If I try to start the server I get:</p>
  
  <pre><code>(...) Caused by: org.h2.jdbc.JdbcSQLException: Database may be already in use: "Locked by another process".
  </code></pre>
  
  <p>And my web application fails to start.</p>
  
  <p>Starting an H2 TCP server and replacing JDBC URLs with <code>jdbc:h2:tcp://myIP/</code> works fine. So no problem with my database files. Also, I am sure nothing else is trying to use those files. So the conflict can only be between the JDBC realm and Hibernate.</p>
  
  <p>Why do I want an embedded server? For the classic reasons: I want to distribute this application and I don't want users to have to start two processes instead of just one. Also, this will not be a shared database, so no need to create a new process just for that. Finally, no additional server ports are open for database, which is good for security.</p>