Search in sources :

Example 6 with FileLockMethod

use of org.h2.store.FileLockMethod in project h2database by h2database.

the class Database method close.

/**
 * Close the database.
 *
 * @param fromShutdownHook true if this method is called from the shutdown
 *            hook
 */
void close(boolean fromShutdownHook) {
    try {
        synchronized (this) {
            if (closing) {
                return;
            }
            throwLastBackgroundException();
            if (fileLockMethod == FileLockMethod.SERIALIZED && !reconnectChangePending) {
                // another connection may have written something - don't write
                try {
                    closeOpenFilesAndUnlock(false);
                } catch (DbException e) {
                // ignore
                }
                traceSystem.close();
                return;
            }
            closing = true;
            stopServer();
            if (!userSessions.isEmpty()) {
                if (!fromShutdownHook) {
                    return;
                }
                trace.info("closing {0} from shutdown hook", databaseName);
                closeAllSessionsException(null);
            }
            trace.info("closing {0}", databaseName);
            if (eventListener != null) {
                // allow the event listener to connect to the database
                closing = false;
                DatabaseEventListener e = eventListener;
                // set it to null, to make sure it's called only once
                eventListener = null;
                e.closingDatabase();
                if (!userSessions.isEmpty()) {
                    // if a connection was opened, we can't close the database
                    return;
                }
                closing = true;
            }
        }
        if (!this.isReadOnly()) {
            removeOrphanedLobs();
        }
        try {
            if (systemSession != null) {
                if (powerOffCount != -1) {
                    for (Table table : getAllTablesAndViews(false)) {
                        if (table.isGlobalTemporary()) {
                            table.removeChildrenAndResources(systemSession);
                        } else {
                            table.close(systemSession);
                        }
                    }
                    for (SchemaObject obj : getAllSchemaObjects(DbObject.SEQUENCE)) {
                        Sequence sequence = (Sequence) obj;
                        sequence.close();
                    }
                }
                for (SchemaObject obj : getAllSchemaObjects(DbObject.TRIGGER)) {
                    TriggerObject trigger = (TriggerObject) obj;
                    try {
                        trigger.close();
                    } catch (SQLException e) {
                        trace.error(e, "close");
                    }
                }
                if (powerOffCount != -1) {
                    meta.close(systemSession);
                    systemSession.commit(true);
                }
            }
        } catch (DbException e) {
            trace.error(e, "close");
        }
        tempFileDeleter.deleteAll();
        try {
            closeOpenFilesAndUnlock(true);
        } catch (DbException e) {
            trace.error(e, "close");
        }
        trace.info("closed");
        traceSystem.close();
        if (closeOnExit != null) {
            closeOnExit.reset();
            try {
                Runtime.getRuntime().removeShutdownHook(closeOnExit);
            } catch (IllegalStateException e) {
            // ignore
            } catch (SecurityException e) {
            // applets may not do that - ignore
            }
            closeOnExit = null;
        }
        if (deleteFilesOnDisconnect && persistent) {
            deleteFilesOnDisconnect = false;
            try {
                String directory = FileUtils.getParent(databaseName);
                String name = FileUtils.getName(databaseName);
                DeleteDbFiles.execute(directory, name, true);
            } catch (Exception e) {
            // ignore (the trace is closed already)
            }
        }
    } finally {
        Engine.getInstance().close(databaseName);
    }
}
Also used : SchemaObject(org.h2.schema.SchemaObject) DatabaseEventListener(org.h2.api.DatabaseEventListener) MetaTable(org.h2.table.MetaTable) Table(org.h2.table.Table) SQLException(java.sql.SQLException) TriggerObject(org.h2.schema.TriggerObject) Sequence(org.h2.schema.Sequence) DbException(org.h2.message.DbException) SQLException(java.sql.SQLException) IOException(java.io.IOException) DbException(org.h2.message.DbException)

Aggregations

DbException (org.h2.message.DbException)5 IOException (java.io.IOException)3 SQLException (java.sql.SQLException)3 Properties (java.util.Properties)2 FileLock (org.h2.store.FileLock)2 BitSet (java.util.BitSet)1 DatabaseEventListener (org.h2.api.DatabaseEventListener)1 CreateTableData (org.h2.command.ddl.CreateTableData)1 Constraint (org.h2.constraint.Constraint)1 Cursor (org.h2.index.Cursor)1 TraceSystem (org.h2.message.TraceSystem)1 Schema (org.h2.schema.Schema)1 SchemaObject (org.h2.schema.SchemaObject)1 Sequence (org.h2.schema.Sequence)1 TriggerObject (org.h2.schema.TriggerObject)1 FileLockMethod (org.h2.store.FileLockMethod)1 PageStore (org.h2.store.PageStore)1 Column (org.h2.table.Column)1 IndexColumn (org.h2.table.IndexColumn)1 MetaTable (org.h2.table.MetaTable)1