static void runWithDatabase(final ODatabaseDocumentInternal database, final Runnable runnable) { if (!database.isActiveOnCurrentThread()) database.activateOnCurrentThread(); try { runnable.run(); } finally { ODatabaseRecordThreadLocal.INSTANCE.remove(); } }
static <V> V supplyWithDatabase(final ODatabaseDocumentInternal database, final Supplier<V> callable) { if (!database.isActiveOnCurrentThread()) database.activateOnCurrentThread(); try { return callable.get(); } finally { ODatabaseRecordThreadLocal.INSTANCE.remove(); } }
static <E extends Throwable> void acceptWithDatabase(final ODatabaseDocumentTx database, final Acceptor<? super ODatabaseDocumentTx, E> acceptor) throws E{ if (!database.isActiveOnCurrentThread()) database.activateOnCurrentThread(); try { acceptor.accept(database); } finally { ODatabaseRecordThreadLocal.INSTANCE.remove(); } } }
@Override public void signOut() { super.signOut(); this.username=null; this.password=null; this.userModel.setObject(null); ODatabaseRecordThreadLocal.instance().remove(); }
public synchronized void initCustomStorage(String name, String path, String userName, String userPassword) { ODatabaseDocumentEmbedded embedded = null; synchronized (this) { boolean exists = OLocalPaginatedStorage.exists(Paths.get(path)); OAbstractPaginatedStorage storage = (OAbstractPaginatedStorage) disk.createStorage(path, new HashMap<>(), maxWALSegmentSize); // TODO: Add Creation settings and parameters if (!exists) { embedded = internalCreate(getConfigurations(), storage); } storages.put(name, storage); } if (embedded != null) { embedded.callOnCreateListeners(); ODatabaseRecordThreadLocal.instance().remove(); } }
private static <T> T withActiveDb(final ODatabase db, final Supplier<T> supplier) { @SuppressWarnings("resource") final ODatabaseDocumentInternal currentDb = ODatabaseRecordThreadLocal.instance().getIfDefined(); if (db.equals(currentDb) || !(db instanceof ODatabaseDocumentInternal)) { return supplier.get(); } try { ODatabaseRecordThreadLocal.instance().set((ODatabaseDocumentInternal) db); return supplier.get(); } finally { if (currentDb != null) { ODatabaseRecordThreadLocal.instance().set(currentDb); } else { ODatabaseRecordThreadLocal.instance().remove(); } } }
@Override public void drop() { checkOpenness(); internal.callOnDropListeners(); ODatabaseRecordThreadLocal.instance().remove(); factory.drop(this.getName(), null, null); this.internal = null; clearOwner(); }
protected void execInSeparateDatabase(final OCallable iCallback) { final ODatabaseDocumentInternal prevDb = ODatabaseRecordThreadLocal.instance().getIfDefined(); try { iCallback.call(null); } finally { if (prevDb != null) { ODatabaseRecordThreadLocal.instance().set(prevDb); } else { ODatabaseRecordThreadLocal.instance().remove(); } } }
protected void execInSeparateDatabase(final OCallable iCallback) { final ODatabaseDocumentInternal prevDb = ODatabaseRecordThreadLocal.instance().getIfDefined(); try { iCallback.call(null); } finally { if (prevDb != null) { ODatabaseRecordThreadLocal.instance().set(prevDb); } else { ODatabaseRecordThreadLocal.instance().remove(); } } }
public ODatabaseDocumentEmbedded(final OStorage storage) { activateOnCurrentThread(); try { status = STATUS.CLOSED; // OVERWRITE THE URL url = storage.getURL(); this.storage = storage; this.componentsFactory = storage.getComponentsFactory(); unmodifiableHooks = Collections.unmodifiableMap(hooks); localCache = new OLocalRecordCache(); init(); databaseOwner = this; } catch (Exception t) { ODatabaseRecordThreadLocal.instance().remove(); throw OException.wrapException(new ODatabaseException("Error on opening database "), t); } }
@Override public void create(String name, String user, String password, ODatabaseType type, OrientDBConfig config) { final ODatabaseDocumentEmbedded embedded; synchronized (this) { if (!exists(name, user, password)) { try { config = solveConfig(config); OAbstractPaginatedStorage storage; if (type == ODatabaseType.MEMORY) { storage = (OAbstractPaginatedStorage) memory.createStorage(name, new HashMap<>(), maxWALSegmentSize); } else { storage = (OAbstractPaginatedStorage) disk.createStorage(buildName(name), new HashMap<>(), maxWALSegmentSize); } storages.put(name, storage); embedded = internalCreate(config, storage); } catch (Exception e) { throw OException.wrapException(new ODatabaseException("Cannot create database '" + name + "'"), e); } } else throw new ODatabaseException("Cannot create new database '" + name + "' because it already exists"); } embedded.callOnCreateListeners(); ODatabaseRecordThreadLocal.instance().remove(); }
public void restore(String name, String user, String password, ODatabaseType type, String path, OrientDBConfig config) { final ODatabaseDocumentEmbedded embedded; OAbstractPaginatedStorage storage; synchronized (this) { if (!exists(name, null, null)) { try { storage = (OAbstractPaginatedStorage) disk.createStorage(buildName(name), new HashMap<>(), maxWALSegmentSize); embedded = internalCreate(config, storage); storages.put(name, storage); } catch (Exception e) { throw OException.wrapException(new ODatabaseException("Cannot restore database '" + name + "'"), e); } } else throw new ODatabaseException("Cannot create new storage '" + name + "' because it already exists"); } storage.restoreFromIncrementalBackup(path); embedded.callOnCreateListeners(); ODatabaseRecordThreadLocal.instance().remove(); }
@Override public void unSubscribe() { ODatabaseDocumentInternal prev = ODatabaseRecordThreadLocal.instance().getIfDefined(); db.activateOnCurrentThread(); OLiveQueryHookV2.unsubscribe(token, db); if (prev != null) { ODatabaseRecordThreadLocal.instance().set(prev); } else { ODatabaseRecordThreadLocal.instance().remove(); } }
@Override public void onDetach(RequestCycle cycle) { ODatabaseDocument db = ODatabaseRecordThreadLocal.instance().getIfDefined(); if(db!=null) { if(db.getTransaction().isActive()) db.commit(true); db.close(); ODatabaseRecordThreadLocal.instance().remove(); } }
public void internalOpen(final String iUserName, final String iUserPassword, boolean checkPassword) { try { OSecurity security = metadata.getSecurity(); if (user == null || user.getVersion() != security.getVersion() || !user.getName().equalsIgnoreCase(iUserName)) { final OUser usr; if (checkPassword) { usr = security.authenticate(iUserName, iUserPassword); } else { usr = security.getUser(iUserName); } if (usr != null) user = new OImmutableUser(security.getVersion(), usr); else user = null; checkSecurity(ORule.ResourceGeneric.DATABASE, ORole.PERMISSION_READ); } } catch (OException e) { ODatabaseRecordThreadLocal.instance().remove(); throw e; } catch (Exception e) { ODatabaseRecordThreadLocal.instance().remove(); throw OException.wrapException(new ODatabaseException("Cannot open database url=" + getURL()), e); } }
public void close() { ODatabaseDocumentInternal prev = ODatabaseRecordThreadLocal.instance().getIfDefined(); try { db.activateOnCurrentThread(); resultSet.close(); db.close(); ((OSharedContextEmbedded) ((ODatabaseInternal) db).getSharedContext()).getActiveDistributedQueries().remove(queryId); } finally { if (prev == null) { ODatabaseRecordThreadLocal.instance().remove(); } else { ODatabaseRecordThreadLocal.instance().set(prev); } } } }
OrientKeyValueStorage(final ODatabaseDocumentTx database, final String collectionName, final boolean forceCreate) { if (!database.isActiveOnCurrentThread()) database.activateOnCurrentThread(); this.database = database; indexName = collectionName + "Index"; //init class final OSchema schema = database.getMetadata().getSchema(); final OClass documentClass; if (schema.existsClass(collectionName)) documentClass = schema.getClass(collectionName); else if (forceCreate) { documentClass = schema.createClass(collectionName); PersistentFieldDefinition.defineFields(documentClass); PersistentFieldDefinition.createIndex(documentClass, indexName); } else documentClass = null; this.documentClass = new AtomicReference<>(documentClass); ODatabaseRecordThreadLocal.INSTANCE.remove(); }
public List<OResult> fetchNextPage() { ODatabaseDocumentInternal prev = ODatabaseRecordThreadLocal.instance().getIfDefined(); try { db.activateOnCurrentThread(); resultSet.close(); db.close(); } finally { if (prev == null) { ODatabaseRecordThreadLocal.instance().remove(); } else { ODatabaseRecordThreadLocal.instance().set(prev); } } return null; }
public void onLiveResultEnd() { if (request.getResultListener() instanceof OLiveResultListener) { ((OLiveResultListener) request.getResultListener()).onUnsubscribe(token); } if (execDb != null) { ODatabaseDocumentInternal oldThreadDB = ODatabaseRecordThreadLocal.instance().getIfDefined(); execDb.activateOnCurrentThread(); execDb.close(); if (oldThreadDB == null) { ODatabaseRecordThreadLocal.instance().remove(); } else { ODatabaseRecordThreadLocal.instance().set(oldThreadDB); } } }
@Provides public ODatabaseDocument getDatabaseRecord() { ODatabaseDocument db = DefaultODatabaseThreadLocalFactory.castToODatabaseDocument(ODatabaseRecordThreadLocal.instance().get().getDatabaseOwner()); if(db.isClosed()) { ODatabaseRecordThreadLocal.instance().remove(); db = DefaultODatabaseThreadLocalFactory.castToODatabaseDocument(ODatabaseRecordThreadLocal.instance().get().getDatabaseOwner()); } return db; }