Ejemplo n.º 1
0
 @Override
 public Repository createRepository(RepositoryDescriptor descriptor) throws Exception {
   log.info("Creating pooling repository: " + descriptor.getName());
   ManagedConnectionFactory managedConnectionFactory =
       new ManagedConnectionFactoryImpl(SQLRepository.getDescriptor(descriptor));
   return (Repository) managedConnectionFactory.createConnectionFactory(lookupConnectionManager());
 }
  /** Match a set of connections from the pool */
  public ManagedConnection matchManagedConnections(
      Set connectionSet, Subject subject, ConnectionRequestInfo info) throws ResourceException {
    boolean trace = log.isTraceEnabled();

    // Get cred
    info = getInfo(info);
    JmsCred cred = JmsCred.getJmsCred(this, subject, info);

    if (trace) log.trace("Looking for connection matching credentials: " + cred);

    // Traverse the pooled connections and look for a match, return first
    // found
    Iterator connections = connectionSet.iterator();

    while (connections.hasNext()) {
      Object obj = connections.next();

      // We only care for connections of our own type
      if (obj instanceof JmsManagedConnection) {
        // This is one from the pool
        JmsManagedConnection mc = (JmsManagedConnection) obj;

        // Check if we even created this on
        ManagedConnectionFactory mcf = mc.getManagedConnectionFactory();

        // Only admit a connection if it has the same username as our
        // asked for creds

        // FIXME, Here we have a problem, jms connection
        // may be anonymous, have a user name

        if ((mc.getUserName() == null
                || (mc.getUserName() != null && mc.getUserName().equals(cred.name)))
            && mcf.equals(this)) {
          // Now check if ConnectionInfo equals
          if (info.equals(mc.getInfo())) {

            if (trace) log.trace("Found matching connection: " + mc);

            return mc;
          }
        }
      }
    }

    if (trace) log.trace("No matching connection was found");

    return null;
  }
  /** Initialize the connection manager. */
  public Object init(ManagedConnectionFactory mcf) throws ConfigException, ResourceException {
    if (!_lifecycle.toInit()) return null;

    _mcf = mcf;

    if (_name == null) {
      int v = _idGen.incrementAndGet();

      _name = mcf.getClass().getSimpleName() + "-" + v;
    }

    if (_tm == null)
      throw new ConfigException(L.l("the connection manager needs a transaction manager."));

    _idlePool = new IdlePoolSet(_maxIdleCount);

    _connectionTime = MeterService.createActiveTimeMeter("Resin|Database|Connection");
    _idleTime = MeterService.createActiveTimeMeter("Resin|Database|Idle");
    _queryTime = MeterService.createActiveTimeMeter("Resin|Database|Query");

    registerSelf();

    _alarm = new WeakAlarm(this);

    if (!(mcf instanceof ValidatingManagedConnectionFactory)) {
      // never check
      _lastValidCheckTime = Long.MAX_VALUE / 2;
    }

    // recover any resources on startup
    if (_isEnableXA) {
      Subject subject = null;
      ManagedConnection mConn = mcf.createManagedConnection(subject, null);

      try {
        XAResource xa = mConn.getXAResource();

        _tm.recover(xa);
      } catch (NotSupportedException e) {
        log.finer(e.toString());
      } catch (Throwable e) {
        log.log(Level.FINER, e.toString(), e);
      } finally {
        mConn.destroy();
      }
    }

    return mcf.createConnectionFactory(this);
  }
  public Object allocateConnection(
      ManagedConnectionFactory paramManagedConnectionFactory,
      ConnectionRequestInfo paramConnectionRequestInfo)
      throws ResourceException {
    /* 71 */ ManagedConnection localManagedConnection =
        paramManagedConnectionFactory.createManagedConnection(null, paramConnectionRequestInfo);

    /* 73 */ return localManagedConnection.getConnection(null, paramConnectionRequestInfo);
  }
  /** Creates a new connection. */
  private UserPoolItem createConnection(
      ManagedConnectionFactory mcf,
      Subject subject,
      ConnectionRequestInfo info,
      UserPoolItem oldPoolItem)
      throws ResourceException {
    boolean isValid = false;
    ManagedPoolItem poolItem = null;

    try {
      ManagedConnection mConn = mcf.createManagedConnection(subject, info);

      if (mConn == null)
        throw new ResourceException(
            L.l("'{0}' did not return a connection from createManagedConnection", mcf));

      poolItem = new ManagedPoolItem(this, mcf, mConn);

      UserPoolItem userPoolItem;

      // Ensure the connection is still valid
      userPoolItem = poolItem.toActive(subject, info, oldPoolItem);

      if (userPoolItem == null) {
        throw new ResourceException(L.l("Connection '{0}' was not valid on creation", poolItem));
      }

      _connectionCreateCountTotal.incrementAndGet();

      synchronized (_connectionPool) {
        _connectionPool.add(poolItem);
      }

      poolItem = null;
      isValid = true;

      return userPoolItem;
    } finally {
      if (!isValid) {
        _connectionFailCountTotal.incrementAndGet();
        _lastFailTime = CurrentTime.getCurrentTime();
      }

      // server/308b - connection removed on rollback-only, when it's
      // theoretically possible to reuse it
      if (poolItem != null) poolItem.destroy();
    }
  }
  /*
   * @see com.atomikos.datasource.xa.XATransactionalResource#refreshXAConnection()
   */
  protected XAResource refreshXAConnection() throws ResourceException {
    if (LOGGER.isInfoEnabled()) {
      LOGGER.logInfo("refreshXAConnection() for resource: " + getName());
    }

    XAResource ret = null;
    if (connection != null) {
      try {
        connection.destroy();
      } catch (Exception normal) {
        // this can be expected, since this method is only called
        // if there is a connection problem
      }
    }

    try {

      LOGGER.logInfo("about to block for new connection...");
      // System.out.println ( "ABOUT TO BLOCK FOR NEW CONNECTION");
      connection = mcf.createManagedConnection(null, null);

    } catch (javax.resource.ResourceException e) {
      // ignore and return null: happens if resource is down
      // at this moment
      connection = null;
    } finally {
      // System.out.println ( "BLOCKING DONE");
      if (LOGGER.isInfoEnabled()) {
        LOGGER.logInfo("blocking done.");
      }
    }

    try {
      if (connection != null) ret = connection.getXAResource();
    } catch (javax.resource.ResourceException e) {
      LOGGER.logWarning("error getting XAResource: " + e.getMessage());

      throw new ResourceException("Error in getting XA resource", e);
    }

    LOGGER.logInfo("refreshXAConnection() done.");

    // System.out.println ( "DONE REFRESHXACONNECTION FOR RESOURCE: " +
    // getName() );

    return ret;
  }
  /** Allocates a connection from the idle pool. */
  private UserPoolItem allocateIdleConnection(
      ManagedConnectionFactory mcf,
      Subject subject,
      ConnectionRequestInfo info,
      UserPoolItem oldPoolItem)
      throws ResourceException {
    while (_lifecycle.isActive()) {
      ManagedConnection mConn;

      long now = CurrentTime.getCurrentTime();

      if (_lastValidCheckTime + 15000L < now) {
        _lastValidCheckTime = now;

        if (mcf instanceof ValidatingManagedConnectionFactory) {
          ValidatingManagedConnectionFactory vmcf;
          vmcf = (ValidatingManagedConnectionFactory) mcf;

          validate(vmcf);
        }
      }

      ManagedPoolItem poolItem = null;

      while (true) {
        // asks the Driver's ManagedConnectionFactory to match an
        // idle connection
        synchronized (_connectionPool) {
          mConn = mcf.matchManagedConnections(_idlePool, subject, info);

          // If there are no more idle connections, return null
          if (mConn == null) return null;

          // remove can fail for threading reasons, so only succeed if it works.
          if (!_idlePool.remove(mConn)) {
            mConn = null;
          }
        }

        if (mConn != null) {
          poolItem = findPoolItem(mConn);

          if (poolItem == null)
            throw new IllegalStateException(
                L.l("Unexpected non-matching PoolItem found for {0}", mConn));

          break;
        }
      }

      try {
        // Ensure the connection is still valid
        UserPoolItem userPoolItem;
        userPoolItem = poolItem.toActive(subject, info, oldPoolItem);

        if (userPoolItem != null) {
          poolItem = null;
          return userPoolItem;
        }
      } finally {
        if (poolItem != null) poolItem.destroy();
      }
    }

    return null;
  }