⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 transaction.java

📁 Java的面向对象数据库系统的源代码
💻 JAVA
📖 第 1 页 / 共 4 页
字号:
            if (className != null) {
                cl = env.classManager.classForName(className);
            }

            Permissions perms = new Permissions(owner, access);

            if (id == null) {
                id = new ObjectID(env.keyGenerator.nextID());
            }

            ObjectContainer container = env.storeManager.newContainerAndLock(this, null, id, perms, Lock.LEVEL_WRITE);

            boolean alright = false;
            boolean containerAcquired = false;

            try {
                container = acquireContainer(container, Lock.LEVEL_WRITE);
                containerAcquired = true;

                if (cl != null) {
                    container.setShouldCallOnActivate(false);
                    container.createTarget(env, cl, sig, args);
                    container.target().onCreate();
                }

                if (name != null) {
                    nameObject(container.id(), name);
                }

                alright = true;

                return container;
            } finally {
                if (!containerAcquired) {
                    /*
                        We have had acquired a lock but somehow did not manage to acquire the corresponding container.
                        Thus, the cluster would not be added to this transaction and thus it would not be unlocked
                        when the transaction is finished. Thus we have to unlock it now.
                    */
                    container./*getCluster().getLock().*/lock().release(this);
                    /*
                        But is this the right behaviour? What if acquireContainer() fails for some reason (e.g. PermissionError)
                        after it locked the cluster itself. Then we would unlock it while it should remain locked? Should it
                        if it fails with an exception?
                    */
                }
                if (!alright) {
                    container.unpin();
                }
            }
        } catch (InvocationTargetException e) {
            Throwable ee = e.getTargetException();

            if (ee instanceof org.ozoneDB.core.TransactionError) {
                throw (org.ozoneDB.core.TransactionError) ee;
            }

            if (ee instanceof OzoneObjectException)
                throw (OzoneObjectException) ee;

            throw new org.ozoneDB.OzoneObjectException("caught", ee);
            /*
            if (ee instanceof RuntimeException) {
                throw (RuntimeException)ee;
            } else if (ee instanceof Exception) {
                throw (Exception)ee;
            } else if (ee instanceof Error) {
                throw (Error)ee;
            } else {
                throw new Exception( "Unknown exception type " + ee.getClass().getName() );
            }
            */
        } catch (OzoneObjectException e) {
            throw e;
        } catch (OzoneRemoteException e) {
            throw e;
        } catch (Exception e) {
            env.logWriter.newEntry(this, "createObject()", e, LogWriter.WARN);
            throw new OzoneInternalException(e.toString(), e);
        }
    }


    public ObjectContainer copyObject(ObjectID id) throws Exception {
        if (env.logWriter.hasTarget(LogWriter.DEBUG3)) {
            env.logWriter.newEntry(this, "copyObject() ", LogWriter.DEBUG3);
        }

        // check (and wait) if a thread runs exclusively;
        env.transactionManager.checkExclusion();

        try {
            if (env.logWriter.hasTarget(LogWriter.DEBUG3)) {
                env.logWriter.newEntry(this, "copyObject(): " + id.toString(), LogWriter.DEBUG3);
            }

            ObjectContainer container = acquireObject(id, Lock.LEVEL_WRITE);

            try {
                Object target = container.targetClone();
                Permissions perms = (Permissions) container.permissions().clone();
                ObjectContainer copyContainer = env.storeManager.newContainerAndLock(this, (OzoneCompatible) target, new ObjectID(env.keyGenerator.nextID()), perms, Lock.LEVEL_WRITE);
                boolean alright = false;

                try {
                    copyContainer = acquireContainer(copyContainer, Lock.LEVEL_WRITE);
                    alright = true;
                    return copyContainer;
                } finally {
                    if (!alright) {
                        copyContainer.unpin();
                    }
                }
            } finally {
                releaseObject(container);
            }
        } catch (OzoneRemoteException e) {
            throw e;
        } catch (Exception e) {
            env.logWriter.newEntry(this, "copyObject()", e, LogWriter.WARN);
            throw new OzoneInternalException(e.toString(), e);
        }
    }


    public void deleteObject(ObjectID id) throws ObjectNotFoundException, IOException, ClassNotFoundException, TransactionException, TransactionError, OzoneRemoteException, OzoneInternalException, OzoneObjectException {
        if (env.logWriter.hasTarget(LogWriter.DEBUG3)) {
            env.logWriter.newEntry(this, "deleteObject(): " + id.toString(), LogWriter.DEBUG3);
        }

        try {

            ObjectContainer container = acquireObject(id, Lock.LEVEL_WRITE);
            try {
                try {
                    container.target().onDelete();
                } catch (OzoneRemoteException e) {
                    throw e;
                } catch (OzoneObjectException e) {
                    throw e;
                } catch (Throwable e) {
                    env.logWriter.newEntry(this, "deleteObject()", e, LogWriter.WARN);
//                  throw new OzoneInternalException( e.toString() );
                    throw new OzoneObjectException("caught onDelete()", e);
                }
                // avoid onPassivate() being called
                container.setShouldCallOnPassivate(false);
                container.deleteTarget();
            } finally {
                releaseObject(container);
            }
        } catch (OzoneRemoteException e) {
            throw e;
        } catch (Exception e) {
            env.logWriter.newEntry(this, "deleteObject()", e, LogWriter.WARN);
            throw new OzoneInternalException(e.toString(), e);
        }
    }


    /**
     * @param id
     * @param methodName
     * @param sig
     * @param lockLevel
     * @return the result of the invocation
     */
    public Object invokeObject(ObjectID id, String methodName, String sig, Object[] args, int lockLevel) throws Exception, org.ozoneDB.OzoneObjectException {
        if (env.logWriter.hasTarget(LogWriter.DEBUG3)) {
            env.logWriter.newEntry(this, "invokeObject(): " + methodName, LogWriter.DEBUG3);
        }

        try {
            ObjectContainer container = acquireObject(id, lockLevel);

            try {
                env.getGarbageCollector().interceptInvocationPre(this, container, args);

                Object result = null;

                try {
                    result = container.invokeTarget(env, methodName, sig, args);
                } finally {
                    env.getGarbageCollector().interceptInvocationPost(this, container, result);
                }

                return result;
            } finally {
                releaseObject(container);
            }
            // using the container after the invoke is dangerous because
            // it's possibly deactivated meanwhile
        } catch (InvocationTargetException e) {
            Throwable ee = e.getTargetException();

            if (ee instanceof org.ozoneDB.core.TransactionError) {
                throw (org.ozoneDB.core.TransactionError) ee;
            }

            if (ee instanceof OzoneObjectException)
                throw (OzoneObjectException) ee;

            throw new org.ozoneDB.OzoneObjectException("caught", ee);

            /*
            if (ee instanceof RuntimeException) {
                throw (RuntimeException)ee;
            } else if (ee instanceof Exception) {
                throw (Exception)ee;
            } else if (ee instanceof Error) {
                throw (Error)ee;
            } else {
                throw new Exception( "Unknown exception type " + ee.getClass().getName() );
            }
            */
        } catch (OzoneObjectException e) {
            throw e;
        } catch (OzoneRemoteException e) {
            env.logWriter.newEntry(this, "invokeObject()", e, LogWriter.WARN);
            throw e;
        } catch (Exception e) {
            // since we throw away stack trace of the original exception we
            // create a new log message here
            env.logWriter.newEntry(this, "invokeObject()", e, LogWriter.WARN);
            throw new OzoneInternalException(e.toString(), e);
        }
    }


    public Object invokeObject(ObjectID id, int methodIndex, Object[] args, int lockLevel) throws Exception, org.ozoneDB.OzoneObjectException {
        if (env.logWriter.hasTarget(LogWriter.DEBUG3)) {
            env.logWriter.newEntry(this, "invokeObject(," + methodIndex + "): start.", LogWriter.DEBUG3);
        }

        try {
            try {
                ObjectContainer container = acquireObject(id, lockLevel);

                if (container == null) {
                    env.logWriter.newEntry(this, "container is null!.", LogWriter.WARN);
                }

                try {
                    env.getGarbageCollector().interceptInvocationPre(this, container, args);

                    Object result = null;

                    try {
                        result = container.invokeTarget(env, methodIndex, args);
                    } finally {
                        env.getGarbageCollector().interceptInvocationPost(this, container, result);
                    }

                    return result;
                } finally {
                    releaseObject(container);
                }

                // using the container after the invoke is dangerous because
                // it's possibly deactivated meanwhile
            } catch (InvocationTargetException e) {
                Throwable ee = e.getTargetException();

                if (ee instanceof org.ozoneDB.core.TransactionError) {
                    throw (org.ozoneDB.core.TransactionError) ee;
                }

                if (ee instanceof OzoneObjectException)
                    throw (OzoneObjectException) ee;

                throw new OzoneObjectException("caught", ee);
                /*
                if (ee instanceof RuntimeException) {
                    throw (RuntimeException)ee;
                }
                else if (ee instanceof Exception) {
                    throw (Exception)ee;
                }
                else if (ee instanceof Error) {
                    throw (Error)ee;
                }
                else {
                    throw new Exception( "Unknown exception type " + ee.getClass().getName() );
                }
                */
            } catch (OzoneObjectException e) {
                throw e;
            } catch (OzoneRemoteException e) {
                env.logWriter.newEntry(this, "invokeObject()", e, LogWriter.WARN);
                throw e;
            } catch (Exception e) {
                // since we throw away stack trace of the original exception we
                // create a new log message here
                env.logWriter.newEntry(this, "invokeObject()", e, LogWriter.WARN);
                throw new OzoneInternalException(e.toString(), e);
            }
        } finally {
            if (env.logWriter.hasTarget(LogWriter.DEBUG3)) {
                env.logWriter.newEntry(this, "invokeObject(," + methodIndex + "): end.", LogWriter.DEBUG3);
            }
        }
    }


    public void nameObject(ObjectID id, String name) throws Exception {
        if (env.logWriter.hasTarget(LogWriter.DEBUG3)) {
            env.logWriter.newEntry(this, "nameObject()", LogWriter.DEBUG3);
        }

        try {
            ObjectContainer container = acquireObject(id, Lock.LEVEL_WRITE);

            try {

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -