public Object invoke(MethodInvocation methodInvocation) throws Throwable {
   boolean existingTransaction = false;
   PersistenceManager pm =
       PersistenceManagerFactoryUtils.getPersistenceManager(getPersistenceManagerFactory(), true);
   if (TransactionSynchronizationManager.hasResource(getPersistenceManagerFactory())) {
     logger.debug("Found thread-bound PersistenceManager for JDO interceptor");
     existingTransaction = true;
   } else {
     logger.debug("Using new PersistenceManager for JDO interceptor");
     TransactionSynchronizationManager.bindResource(
         getPersistenceManagerFactory(), new PersistenceManagerHolder(pm));
   }
   try {
     Object retVal = methodInvocation.proceed();
     flushIfNecessary(pm, existingTransaction);
     return retVal;
   } finally {
     if (existingTransaction) {
       logger.debug("Not closing pre-bound JDO PersistenceManager after interceptor");
     } else {
       TransactionSynchronizationManager.unbindResource(getPersistenceManagerFactory());
       PersistenceManagerFactoryUtils.releasePersistenceManager(
           pm, getPersistenceManagerFactory());
     }
   }
 }
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
      // Invocation on PersistenceManager interface coming in...

      if (method.getName().equals("equals")) {
        // Only consider equal when proxies are identical.
        return (proxy == args[0] ? Boolean.TRUE : Boolean.FALSE);
      } else if (method.getName().equals("hashCode")) {
        // Use hashCode of PersistenceManager proxy.
        return new Integer(System.identityHashCode(proxy));
      } else if (method.getName().equals("close")) {
        // Handle close method: only close if not within a transaction.
        if (this.persistenceManagerFactory != null) {
          PersistenceManagerFactoryUtils.doReleasePersistenceManager(
              this.target, this.persistenceManagerFactory);
        }
        return null;
      }

      // Invoke method on target PersistenceManager.
      try {
        return method.invoke(this.target, args);
      } catch (InvocationTargetException ex) {
        throw ex.getTargetException();
      }
    }
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
      // Invocation on PersistenceManagerFactory interface coming in...

      if (method.getName().equals("equals")) {
        // Only consider equal when proxies are identical.
        return (proxy == args[0] ? Boolean.TRUE : Boolean.FALSE);
      } else if (method.getName().equals("hashCode")) {
        // Use hashCode of PersistenceManagerFactory proxy.
        return new Integer(System.identityHashCode(proxy));
      } else if (method.getName().equals("getPersistenceManager")) {
        PersistenceManagerFactory target = getTargetPersistenceManagerFactory();
        PersistenceManager pm =
            PersistenceManagerFactoryUtils.doGetPersistenceManager(target, isAllowCreate());
        Class[] ifcs =
            ClassUtils.getAllInterfacesForClass(pm.getClass(), getClass().getClassLoader());
        return (PersistenceManager)
            Proxy.newProxyInstance(
                pm.getClass().getClassLoader(),
                ifcs,
                new TransactionAwareInvocationHandler(pm, target));
      }

      // Invoke method on target PersistenceManagerFactory.
      try {
        return method.invoke(getTargetPersistenceManagerFactory(), args);
      } catch (InvocationTargetException ex) {
        throw ex.getTargetException();
      }
    }
 /**
  * Implementation of the PersistenceExceptionTranslator interface, as autodetected by Spring's
  * PersistenceExceptionTranslationPostProcessor.
  *
  * <p>Converts the exception if it is a JDOException, preferably using a specified JdoDialect.
  * Else returns {@code null} to indicate an unknown exception.
  *
  * @see org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor
  * @see JdoDialect#translateException
  * @see PersistenceManagerFactoryUtils#convertJdoAccessException
  */
 public DataAccessException translateExceptionIfPossible(RuntimeException ex) {
   if (ex instanceof JDOException) {
     if (this.jdoDialect != null) {
       return this.jdoDialect.translateException((JDOException) ex);
     } else {
       return PersistenceManagerFactoryUtils.convertJdoAccessException((JDOException) ex);
     }
   }
   return null;
 }