@SuppressWarnings({"unchecked"})
  public static void close(Statement statement) {
    log.tracef("Closing prepared statement [%s]", statement);

    try {
      // if we are unable to "clean" the prepared statement,
      // we do not close it
      try {
        if (statement.getMaxRows() != 0) {
          statement.setMaxRows(0);
        }
        if (statement.getQueryTimeout() != 0) {
          statement.setQueryTimeout(0);
        }
      } catch (SQLException sqle) {
        // there was a problem "cleaning" the prepared statement
        if (log.isDebugEnabled()) {
          log.debugf("Exception clearing maxRows/queryTimeout [%s]", sqle.getMessage());
        }
        // EARLY EXIT!!!
        return;
      }
      statement.close();
    } catch (SQLException e) {
      log.debugf("Unable to release JDBC statement [%s]", e.getMessage());
    } catch (Exception e) {
      // try to handle general errors more elegantly
      log.debugf("Unable to release JDBC statement [%s]", e.getMessage());
    }
  }
  @Override
  public void release(ResultSet resultSet, Statement statement) {
    log.tracef("Releasing result set [%s]", resultSet);

    if (statement == null) {
      try {
        statement = resultSet.getStatement();
      } catch (SQLException e) {
        throw convert(e, "unable to access Statement from ResultSet");
      }
    }
    if (statement != null) {
      final Set<ResultSet> resultSets = xref.get(statement);
      if (resultSets == null) {
        log.unregisteredStatement();
      } else {
        resultSets.remove(resultSet);
        if (resultSets.isEmpty()) {
          xref.remove(statement);
        }
      }
    } else {
      final boolean removed = unassociatedResultSets.remove(resultSet);
      if (!removed) {
        log.unregisteredResultSetWithoutStatement();
      }
    }
    close(resultSet);
  }
  @Override
  public void register(ResultSet resultSet, Statement statement) {
    log.tracef("Registering result set [%s]", resultSet);

    if (statement == null) {
      try {
        statement = resultSet.getStatement();
      } catch (SQLException e) {
        throw convert(e, "unable to access Statement from ResultSet");
      }
    }
    if (statement != null) {
      // Keep this at DEBUG level, rather than warn.  Numerous connection pool implementations can
      // return a
      // proxy/wrapper around the JDBC Statement, causing excessive logging here.  See HHH-8210.
      if (log.isDebugEnabled() && !xref.containsKey(statement)) {
        log.debug("ResultSet statement was not registered (on register)");
      }
      Set<ResultSet> resultSets = xref.get(statement);
      if (resultSets == null) {
        resultSets = new HashSet<ResultSet>();
        xref.put(statement, resultSets);
      }
      resultSets.add(resultSet);
    } else {
      unassociatedResultSets.add(resultSet);
    }
  }
Exemplo n.º 4
0
 public void processMappingDocument(MappingDocument mappingDocument) {
   this.currentMappingDocument = mappingDocument;
   try {
     LOG.tracef("Processing mapping document: %s ", mappingDocument.getOrigin());
     processCurrentMappingDocument();
   } finally {
     this.currentMappingDocument = null;
   }
 }
  @Override
  public void register(Statement statement, boolean cancelable) {
    log.tracef("Registering statement [%s]", statement);
    if (xref.containsKey(statement)) {
      throw new HibernateException("JDBC Statement already registered");
    }
    xref.put(statement, null);

    if (cancelable) {
      lastQuery = statement;
    }
  }
Exemplo n.º 6
0
 /**
  * Generate the next increment in the optimistic locking value according the {@link VersionType}
  * contract for the version property.
  *
  * @param version The current version
  * @param versionType The version type
  * @param session The originating session
  * @return The incremented optimistic locking value.
  */
 @SuppressWarnings("unchecked")
 public static Object increment(
     Object version, VersionType versionType, SharedSessionContractImplementor session) {
   final Object next = versionType.next(version, session);
   if (LOG.isTraceEnabled()) {
     LOG.tracef(
         "Incrementing: %s to %s",
         versionType.toLoggableString(version, session.getFactory()),
         versionType.toLoggableString(next, session.getFactory()));
   }
   return next;
 }
  @SuppressWarnings({"unchecked"})
  public static void close(ResultSet resultSet) {
    log.tracef("Closing result set [%s]", resultSet);

    try {
      resultSet.close();
    } catch (SQLException e) {
      log.debugf("Unable to release JDBC result set [%s]", e.getMessage());
    } catch (Exception e) {
      // try to handle general errors more elegantly
      log.debugf("Unable to release JDBC result set [%s]", e.getMessage());
    }
  }
Exemplo n.º 8
0
  private void bind(Name name, Object value, Context context) {
    try {
      LOG.tracef("Binding : %s", name);
      context.rebind(name, value);
    } catch (Exception initialException) {
      // We had problems doing a simple bind operation.
      if (name.size() == 1) {
        // if the jndi name had only 1 component there is nothing more we can do...
        throw new JndiException("Error performing bind [" + name + "]", initialException);
      }

      // Otherwise, there is a good chance this may have been caused by missing intermediate
      // contexts.  So we
      // attempt to create those missing intermediate contexts and bind again
      Context intermediateContextBase = context;
      while (name.size() > 1) {
        final String intermediateContextName = name.get(0);

        Context intermediateContext = null;
        try {
          LOG.tracev("Intermediate lookup: {0}", intermediateContextName);
          intermediateContext = (Context) intermediateContextBase.lookup(intermediateContextName);
        } catch (NameNotFoundException handledBelow) {
          // ok as we will create it below if not found
        } catch (NamingException e) {
          throw new JndiException("Unanticipated error doing intermediate lookup", e);
        }

        if (intermediateContext != null) {
          LOG.tracev("Found intermediate context: {0}", intermediateContextName);
        } else {
          LOG.tracev("Creating sub-context: {0}", intermediateContextName);
          try {
            intermediateContext = intermediateContextBase.createSubcontext(intermediateContextName);
          } catch (NamingException e) {
            throw new JndiException(
                "Error creating intermediate context [" + intermediateContextName + "]", e);
          }
        }
        intermediateContextBase = intermediateContext;
        name = name.getSuffix(1);
      }
      LOG.tracev("Binding : {0}", name);
      try {
        intermediateContextBase.rebind(name, value);
      } catch (NamingException e) {
        throw new JndiException("Error performing intermediate bind [" + name + "]", e);
      }
    }
    LOG.debugf("Bound name: %s", name);
  }
Exemplo n.º 9
0
 /**
  * Create the subselect fetch query fragment for the provided {@link QueryParameters} with SELECT
  * and ORDER BY clauses removed.
  *
  * @param queryParameters -the query parameters.
  * @return the subselect fetch query fragment.
  */
 public static String createSubselectFetchQueryFragment(QueryParameters queryParameters) {
   // TODO: ugly here:
   final String queryString = queryParameters.getFilteredSQL();
   final int fromIndex = getFromIndex(queryString);
   final int orderByIndex = queryString.lastIndexOf("order by");
   final String subselectQueryFragment =
       orderByIndex > 0
           ? queryString.substring(fromIndex, orderByIndex)
           : queryString.substring(fromIndex);
   if (LOG.isTraceEnabled()) {
     LOG.tracef("SubselectFetch query fragment: %s", subselectQueryFragment);
   }
   return subselectQueryFragment;
 }
  /**
   * remove the specified entity manager factory from the EntityManagerFactoryRegistry
   *
   * @param name
   * @param entityManagerFactory
   * @throws HibernateException if the specified entity manager factory could not be found in the
   *     registry
   */
  public void removeEntityManagerFactory(String name, EntityManagerFactory entityManagerFactory)
      throws HibernateException {
    LOG.debugf("Remove: name=%s", name);

    if (name == null) { // allow unit tests that don't specify the pu name to succeed
      LOG.tracef("not removing EntityManagerFactory from registry because name is null");
      return;
    }

    Set<EntityManagerFactory> entityManagerFactorySet = entityManagerFactoryMap.get(name);
    if (entityManagerFactorySet == null) {
      throw new HibernateException("registry does not contain entity manager factory: " + name);
    }
    synchronized (entityManagerFactorySet) {
      boolean removed = entityManagerFactorySet.remove(entityManagerFactory);

      if (entityManagerFactorySet.size() == 0) {
        entityManagerFactoryMap.remove(name);
      }
    }
  }
  /**
   * register the specified entity manager factory
   *
   * @param name to register the passed entity manager factory
   * @param entityManagerFactory
   */
  public void addEntityManagerFactory(String name, EntityManagerFactory entityManagerFactory) {
    LOG.debugf("Registering EntityManagerFactory: %s ", name);
    if (name == null) { // allow unit tests that don't specify the pu name to succeed
      LOG.tracef("not registering EntityManagerFactory because name is null");
      return;
    }
    Set<EntityManagerFactory> entityManagerFactorySet = new HashSet<EntityManagerFactory>();
    entityManagerFactorySet.add(entityManagerFactory);
    Set<EntityManagerFactory> previous =
        entityManagerFactoryMap.putIfAbsent(name, entityManagerFactorySet);

    // if already added under 'name'.  Where 'name' could be session factory name, pu name or uuid
    // (previous
    // will be null).  We will give a warning that an EntityManagerFactory is created with the same
    // name
    // as is already used for a different EMF.  The best way to avoid the warning is to specify the
    // AvailableSettings.SESSION_FACTORY_NAME
    // with a unique name.
    if (previous != null) {
      LOG.entityManagerFactoryAlreadyRegistered(
          name, AvailableSettings.ENTITY_MANAGER_FACTORY_NAME);
      boolean done = false;
      while (!done) {
        synchronized (previous) {
          if (entityManagerFactoryMap.get(name) == previous) { // compare and set EMF if same
            previous.add(entityManagerFactory);
            done = true;
          } else { // else it was removed or a new set added
            previous = entityManagerFactoryMap.get(name); // get the set added by another thread
            if (null == previous) { // or add it here if not
              entityManagerFactoryMap.putIfAbsent(name, new HashSet<EntityManagerFactory>());
              previous = entityManagerFactoryMap.get(name); // use the current set
            }
          }
        }
      }
    }
  }
Exemplo n.º 12
0
 /**
  * Create an initial optimistic locking value according the {@link VersionType} contract for the
  * version property.
  *
  * @param versionType The version type.
  * @param session The originating session
  * @return The initial optimistic locking value
  */
 private static Object seed(VersionType versionType, SharedSessionContractImplementor session) {
   final Object seed = versionType.seed(session);
   LOG.tracef("Seeding: %s", seed);
   return seed;
 }