コード例 #1
0
 /**
  * Load database configuration from the specified input source. <tt>source</tt> must point to a
  * JDO configuration file describing the database* name, connection factory and mappings.
  * <tt>resolver</tt> can be used to resolve cached entities, e.g. for external mapping documents.
  * <tt>loader</tt> is optional, if null the default class loader is used.
  *
  * @param source The JDO configuration file
  * @param resolve An optional entity resolver
  * @param loader The class loader to use, null for the default
  * @throw MappingException The mapping file is invalid, or any error occured trying to load the
  *     JDO configuration/mapping
  */
 public static void loadConfiguration(
     InputSource source, EntityResolver resolver, ClassLoader loader) throws MappingException {
   DatabaseRegistry.loadDatabase(source, resolver, null, loader);
 }
コード例 #2
0
  /**
   * Opens and returns a connection to the database. Throws an {@link DatabaseNotFoundException} if
   * the database named was not set in the constructor or with a call to {@link #setDatabaseName},
   * or if no database configuration exists for the named database.
   *
   * @return An open connection to the database
   * @throws DatabaseNotFoundException Attempted to open a database that does not exist
   * @throws PersistenceException Database access failed
   */
  public Database getDatabase() throws DatabaseNotFoundException, PersistenceException {
    InputStream is;

    if (_dbName == null)
      throw new IllegalStateException("Called 'getDatabase' without first setting database name");
    if (DatabaseRegistry.getDatabaseRegistry(_dbName) == null) {
      if (_dbConf == null)
        throw new DatabaseNotFoundException(Messages.format("jdo.dbNoMapping", _dbName));
      try {
        DatabaseRegistry.loadDatabase(
            new InputSource(_dbConf), _entityResolver, _logInterceptor, _classLoader);
      } catch (MappingException except) {
        throw new DatabaseNotFoundException(except);
      }
    }

    if (_tmName != null) {
      InitialContext ctx;
      Transaction tx;
      DatabaseImpl dbImpl;

      try {
        if (tm == null) {
          ctx = new InitialContext();
          tm = (TransactionManager) ctx.lookup(_tmName);
        }
        tx = tm.getTransaction();
        if (_txDbPool != null && _txDbPool.containsTx(tx)) return _txDbPool.get(tx);

        if (tx != null && tx.getStatus() == Status.STATUS_ACTIVE) {
          dbImpl =
              new DatabaseImpl(
                  _dbName,
                  _lockTimeout,
                  _logInterceptor,
                  _callback,
                  _instanceFactory,
                  tx,
                  _classLoader,
                  _autoStore);

          if (_txDbPool != null) _txDbPool.put(tx, dbImpl);

          tx.registerSynchronization(dbImpl);
          return dbImpl;
        }
      } catch (NoInitialContextException except) {
        // No initial context. Just ignore.
      } catch (NameNotFoundException except) {
        // No TransactionManager object. Just ignore.
      } catch (Exception except) {
        // NamingException, SystemException, RollbackException
        if (_logInterceptor != null) _logInterceptor.exception(except);
      }
    }
    return new DatabaseImpl(
        _dbName,
        _lockTimeout,
        _logInterceptor,
        _callback,
        _instanceFactory,
        null,
        _classLoader,
        _autoStore);
  }
コード例 #3
0
 /**
  * Load database configuration from the specified URL. <tt>url</tt> must point to a JDO
  * configuration file describing the database name, connection factory and mappings.
  * <tt>loader</tt> is optional, if null the default class loader is used.
  *
  * @param url The JDO configuration file
  * @param loader The class loader to use, null for the default
  * @throw MappingException The mapping file is invalid, or any error occured trying to load the
  *     JDO configuration/mapping
  */
 public static void loadConfiguration(String url, ClassLoader loader) throws MappingException {
   DatabaseRegistry.loadDatabase(new InputSource(url), null, null, loader);
 }
コード例 #4
0
  /**
   * Opens and returns a connection to the database. Throws an {@link DatabaseNotFoundException} if
   * the database named was not set in the constructor or with a call to {@link #setDatabaseName},
   * or if no database configuration exists for the named database.
   *
   * @return An open connection to the database
   * @throws DatabaseNotFoundException Attempted to open a database that does not exist
   * @throws PersistenceException Database access failed
   */
  public Database getDatabase() throws DatabaseNotFoundException, PersistenceException {
    if (_dbName == null)
      throw new IllegalStateException("Called 'getDatabase' without first setting database name");

    if (DatabaseRegistry.getDatabaseRegistry(_dbName) == null) {
      // use _jdoConfURI to load the JDO configuration
      if (_jdoConfURI != null)
        try {
          DatabaseRegistry.loadDatabase(
              new InputSource(_jdoConfURI), _entityResolver, _classLoader);
        } catch (MappingException except) {
          throw new DatabaseNotFoundException(except);
        }
      // alternatively, use a JdoConf instance to load the JDO configuration
      else if (_jdoConf != null) {
        try {
          DatabaseRegistry.loadDatabase(_jdoConf, _entityResolver, _classLoader);
        } catch (MappingException except) {
          throw new DatabaseNotFoundException(except);
        }
      } else throw new DatabaseNotFoundException(Messages.format("jdo.dbNoMapping", _dbName));
    }

    // load transaction manager factory registry configuration
    try {
      TransactionManagerFactoryRegistry.load(new InputSource(_jdoConfURI), _entityResolver);
    } catch (TransactionManagerAcquireException e) {
      throw new PersistenceException(
          Messages.message("jdo.transaction.problemToInitializeTransactionManagerFactory"), e);
    }

    if (_transactionManagerFactory == null) {

      String transactionMode = null;
      try {
        TransactionDemarcation demarcation =
            JDOConfLoader.getTransactionDemarcation(new InputSource(_jdoConfURI), _entityResolver);

        String demarcationMode = demarcation.getMode();

        org.exolab.castor.jdo.conf.TransactionManager transactionManager =
            demarcation.getTransactionManager();

        if (transactionManager != null) transactionMode = transactionManager.getName();
        else {

          if (demarcationMode.equals(LocalTransactionManagerFactory.NAME))
            transactionMode = LocalTransactionManagerFactory.NAME;
          else
            throw new PersistenceException(
                Messages.message("jdo.transaction.missingTransactionManagerConfiguration"));
        }

      } catch (MappingException e) {
        throw new PersistenceException("jdo.transaction.noValidTransactionMode", e);
      }

      /*
       * Try to obtain the specified<code>TransactionManagerFactory</code> instance from the
       * registry.
       *
       * If the returned TransactionManagerFactory instance is null,
       * return an exception to indicate that we cannot live without a
       * valid TransactionManagerFactory instance and that the user should
       * change her configuration.
       */
      _transactionManagerFactory =
          TransactionManagerFactoryRegistry.getTransactionManagerFactory(transactionMode);

      if (_transactionManagerFactory == null) {
        throw new DatabaseNotFoundException(
            Messages.format("jdo.transaction.missingTransactionManagerFactory", transactionMode));
      }

      /*
       *  Try to obtain a <code>javax.jta.TransactionManager>/code> from the factory.
       */
      try {
        _transactionManager = _transactionManagerFactory.getTransactionManager();
      } catch (TransactionManagerAcquireException e) {
        throw new DatabaseNotFoundException(
            Messages.format(
                "jdo.transaction.unableToAcquireTransactionManager",
                _transactionManagerFactory.getName(),
                e));
      }
    }

    /* At this point, we MUST have a valid instance of TransactionManagerFactory, and
     * dependent on its type (LOCAL or not), we MIGHT have a
     * valid<code>javax.jta.TransactionManager</code> instance.
     *
     * Scenario 1: If the user uses Castor in standalone mode (outside of an
     * J2EE container), we have a TransactionManagerFactory instance only,
     * but no TransactionManager instance.
     *
     * Scenario 2: If the user uses Castor in J2EE mode (inside of an J2EE
     * container), and wants the container to control transaction
     * demarcation, we have both a TransactionManagerFactory (different from
     * LOCAL) and a TransactionManager instance.
     */

    if ((!_transactionManagerFactory.getName().equals(LocalTransactionManagerFactory.NAME))
        && (_transactionManager != null)) {
      Transaction transaction;
      DatabaseImpl dbImpl;

      try {
        transaction = _transactionManager.getTransaction();
        if (_txDbPool != null && _txDbPool.containsTx(transaction))
          return _txDbPool.get(transaction);

        if (transaction != null && transaction.getStatus() == Status.STATUS_ACTIVE) {
          dbImpl =
              new DatabaseImpl(
                  _dbName,
                  _lockTimeout,
                  _callback,
                  _instanceFactory,
                  transaction,
                  _classLoader,
                  _autoStore);

          if (_txDbPool != null) _txDbPool.put(transaction, dbImpl);

          transaction.registerSynchronization(dbImpl);
          return dbImpl;
        }
      } catch (Exception except) {
        // NamingException, SystemException, RollbackException
        if (_logInterceptor != null) _logInterceptor.exception(except);
      }
    }

    return new DatabaseImpl(
        _dbName, _lockTimeout, _callback, _instanceFactory, null, _classLoader, _autoStore);
  }