title: How to resolve "Caused by: java.lang.IllegalStateException: The file is locked: nio:/Development/hiber/data/contactmgr.mv.db [1.4.192/7]"
body: <p>I am trying to insert mock data in H2 db using hibernate, but I am facing following error </p>
  
  <blockquote>
    <p>Caused by: java.lang.IllegalStateException: The file is locked:
    nio:/Development/hiber/data/contactmgr.mv.db [1.4.192/7]</p>
  </blockquote>
  
  <p>, earlier I thought it was because of username and password property but I stumbled upon this error even after removing it from hibernate.cfg.xml.</p>
  
  <blockquote>
    <p>Exception in thread "main" java.lang.ExceptionInInitializerError
        Caused by: org.hibernate.exception.JDBCConnectionException: Error calling Driver#connect
          at org.hibernate.engine.jdbc.connections.internal.BasicConnectionCreator$1$1.convert(BasicConnectionCreator.java:122)
          at org.hibernate.engine.jdbc.connections.internal.BasicConnectionCreator.convertSqlException(BasicConnectionCreator.java:140)
          at org.hibernate.engine.jdbc.connections.internal.DriverConnectionCreator.makeConnection(DriverConnectionCreator.java:58)
          at org.hibernate.engine.jdbc.connections.internal.BasicConnectionCreator.createConnection(BasicConnectionCreator.java:75)
          at org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl.configure(DriverManagerConnectionProviderImpl.java:106)
          at org.hibernate.boot.registry.internal.StandardServiceRegistryImpl.configureService(StandardServiceRegistryImpl.java:111)
          at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:234)
          at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:206)
          at org.hibernate.engine.jdbc.internal.JdbcServicesImpl.buildJdbcConnectionAccess(JdbcServicesImpl.java:260)
          at org.hibernate.engine.jdbc.internal.JdbcServicesImpl.configure(JdbcServicesImpl.java:94)
          at org.hibernate.boot.registry.internal.StandardServiceRegistryImpl.configureService(StandardServiceRegistryImpl.java:111)
          at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:234)
          at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:206)
          at org.hibernate.id.factory.internal.DefaultIdentifierGeneratorFactory.injectServices(DefaultIdentifierGeneratorFactory.java:147)
          at org.hibernate.service.internal.AbstractServiceRegistryImpl.injectDependencies(AbstractServiceRegistryImpl.java:272)
          at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:231)
          at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:206)
          at org.hibernate.metamodel.source.internal.MetadataImpl.(MetadataImpl.java:120)
          at org.hibernate.metamodel.source.internal.MetadataBuilderImpl.build(MetadataBuilderImpl.java:123)
          at org.hibernate.metamodel.MetadataSources.buildMetadata(MetadataSources.java:151)
          at com.hibernate.demo.Application.buildSesssionFactory(Application.java:46)
          at com.hibernate.demo.Application.(Application.java:17)
        Caused by: org.h2.jdbc.JdbcSQLException: Database may be already in use: null. Possible solutions: close all other connection(s); use
    the server mode [90020-192]
          at org.h2.message.DbException.getJdbcSQLException(DbException.java:345)
          at org.h2.message.DbException.get(DbException.java:168)
          at org.h2.mvstore.db.MVTableEngine$Store.convertIllegalStateException(MVTableEngine.java:187)
          at org.h2.mvstore.db.MVTableEngine$Store.open(MVTableEngine.java:167)
          at org.h2.mvstore.db.MVTableEngine.init(MVTableEngine.java:99)
          at org.h2.engine.Database.getPageStore(Database.java:2463)
          at org.h2.engine.Database.open(Database.java:692)
          at org.h2.engine.Database.openDatabase(Database.java:270)
          at org.h2.engine.Database.(Database.java:264)
          at org.h2.engine.Engine.openSession(Engine.java:64)
          at org.h2.engine.Engine.openSession(Engine.java:176)
          at org.h2.engine.Engine.createSessionAndValidate(Engine.java:154)
          at org.h2.engine.Engine.createSession(Engine.java:137)
          at org.h2.engine.Engine.createSession(Engine.java:27)
          at org.h2.engine.SessionRemote.connectEmbeddedOrServer(SessionRemote.java:349)
          at org.h2.jdbc.JdbcConnection.(JdbcConnection.java:118)
          at org.h2.jdbc.JdbcConnection.(JdbcConnection.java:102)
          at org.h2.Driver.connect(Driver.java:72)
          at org.hibernate.engine.jdbc.connections.internal.DriverConnectionCreator.makeConnection(DriverConnectionCreator.java:55)
          ... 19 more
        Caused by: java.lang.IllegalStateException: The file is locked: nio:/Development/hiber/data/contactmgr.mv.db [1.4.192/7]
          at org.h2.mvstore.DataUtils.newIllegalStateException(DataUtils.java:773)
          at org.h2.mvstore.FileStore.open(FileStore.java:172)
          at org.h2.mvstore.MVStore.(MVStore.java:344)
          at org.h2.mvstore.MVStore$Builder.open(MVStore.java:2893)
          at org.h2.mvstore.db.MVTableEngine$Store.open(MVTableEngine.java:154)
          ... 34 more</p>
  </blockquote>
  
  <p>This is my hibernate.cfg.xml file in use :</p>
  
  <pre><code>&lt;?xml version='1.0' encoding='utf-8'?&gt;
  
  &lt;!DOCTYPE hibernate-configuration PUBLIC
          "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
          "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"&gt;
  
  &lt;hibernate-configuration&gt;
  
      &lt;session-factory&gt;
  
          &lt;!-- Database connection settings --&gt;
          &lt;property name="connection.driver_class"&gt;org.h2.Driver&lt;/property&gt;
          &lt;property name="connection.url"&gt;jdbc:h2:./data/contactmgr&lt;/property&gt;
  
  
          &lt;property name="hibernate.default_schema"&gt;PUBLIC&lt;/property&gt;
  
          &lt;!-- JDBC connection pool (use the built-in) --&gt;
          &lt;property name="connection.pool_size"&gt;1&lt;/property&gt;
  
          &lt;!-- SQL dialect --&gt;
          &lt;property name="dialect"&gt;org.hibernate.dialect.H2Dialect&lt;/property&gt;
  
          &lt;!-- Disable the second-level cache  --&gt;
          &lt;property name="cache.provider_class"&gt;org.hibernate.cache.internal.NoCacheProvider&lt;/property&gt;
  
          &lt;!-- Echo all executed SQL to stdout --&gt;
          &lt;property name="show_sql"&gt;true&lt;/property&gt;
  
          &lt;!-- Drop and re-create the database schema on startup --&gt;
          &lt;property name="hbm2ddl.auto"&gt;create&lt;/property&gt;
  
          &lt;property name="show_sql"&gt;true&lt;/property&gt;
  
          &lt;mapping class="com.hibernate.demo.model.Contact"/&gt;
  
      &lt;/session-factory&gt;
  
  &lt;/hibernate-configuration&gt;
  </code></pre>
  
  <p>and following is my Main class:</p>
  
  <pre><code>public class Application {
  
      //Session factory
      private static final SessionFactory sessionFactory = buildSesssionFactory();
  
      public static void main(String[] args) {
  
          Contact contact = new Contact.ContactBuilder("Bob", "marley").withEmail("marley.bob@gmail.com").withPhone(5859789791L).build();
  
          //Open a Session
  
          Session session = sessionFactory.openSession();
  
          //Begin a Transaction
           session.beginTransaction();
  
  
          //Use the session to save the contact
  
          session.save(contact);
          //Commit the transaction
  
          session.getTransaction().commit();
          // Close the session
          session.close();
      }
  
      private static SessionFactory buildSesssionFactory() {
          // Create a StandardServiceRegistry
          Configuration configuration = new Configuration();
          configuration.configure("hibernate.cfg.xml");
          final ServiceRegistry registry = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties()).build();
          return new MetadataSources(registry).buildMetadata().buildSessionFactory();
  </code></pre>