Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to resolve "Caused by: java.lang.IllegalStateException: The file is locked: nio:/Development/hiber/data/contactmgr.mv.db [1.4.192/7]"

I am trying to insert mock data in H2 db using hibernate, but I am facing following error

Caused by: java.lang.IllegalStateException: The file is locked: nio:/Development/hiber/data/contactmgr.mv.db [1.4.192/7]

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

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

This is my hibernate.cfg.xml file in use :

<?xml version='1.0' encoding='utf-8'?>

<!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
        "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">

<hibernate-configuration>

    <session-factory>

        <!-- Database connection settings -->
        <property name="connection.driver_class">org.h2.Driver</property>
        <property name="connection.url">jdbc:h2:./data/contactmgr</property>


        <property name="hibernate.default_schema">PUBLIC</property>

        <!-- JDBC connection pool (use the built-in) -->
        <property name="connection.pool_size">1</property>

        <!-- SQL dialect -->
        <property name="dialect">org.hibernate.dialect.H2Dialect</property>

        <!-- Disable the second-level cache  -->
        <property name="cache.provider_class">org.hibernate.cache.internal.NoCacheProvider</property>

        <!-- Echo all executed SQL to stdout -->
        <property name="show_sql">true</property>

        <!-- Drop and re-create the database schema on startup -->
        <property name="hbm2ddl.auto">create</property>

        <property name="show_sql">true</property>

        <mapping class="com.hibernate.demo.model.Contact"/>

    </session-factory>

</hibernate-configuration>

and following is my Main class:

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("[email protected]").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();
like image 461
Shek Avatar asked Jul 04 '16 21:07

Shek


2 Answers

This exception is thrown because the H2 db file is already in use. (Maybe because of some inconsistency or similar problems with you db file)

I suggest the following:

  1. Have you tried to simply remove the file and all other intermediate files which belong to it in the folder? Afterwards just restart the app freshly with a new db file.

  2. Maybe you've opened the application twice and thus causing the error? Closing all instances will get rid of the exception.

like image 100
David Artmann Avatar answered Nov 01 '22 08:11

David Artmann


If you don't want any lock on your database, you can configure your database connection URL as following :

<property name="connection.url">jdbc:h2:./data/contactmgr;DB_CLOSE_ON_EXIT=TRUE;FILE_LOCK=NO</property>

Documentation :

  • http://h2database.com/html/advanced.html
like image 41
Mickael Avatar answered Nov 01 '22 06:11

Mickael