amberconnection.java

来自「RESIN 3.2 最新源码」· Java 代码 · 共 2,682 行 · 第 1/5 页

JAVA
2,682
字号
    } catch (RuntimeException e) {      throw e;    } catch (Exception e) {      throw new EJBExceptionWrapper(e);    }  }  /**   * Saves the object.   *   * @param obj the object to create   */  public void create(AmberEntityHome home, Object obj)    throws SQLException  {    // ejb/0g22 exception handling    try {      createInternal(home, obj);    } catch (RuntimeException e) {      throw e;    } catch (Exception e) {      throw new EJBExceptionWrapper(e);    }  }  /**   * Updates the object.   */  public void update(Entity entity)  {    if (entity == null)      return;    // jpa/0g0i    if (entity.__caucho_getEntityType() == null)      return;    // XXX: also needs to check PersistenceContextType.TRANSACTION/EXTENDED.    // jpa/0k10    if (! isActiveTransaction())      return;    AmberTable table = entity.__caucho_getEntityType().getTable();    Object key = entity.__caucho_getPrimaryKey();    addCompletion(new RowInvalidateCompletion(table.getName(), key));    // jpa/0ga8, jpa/0s2d if (! _txEntities.contains(entity)) {    Entity oldEntity = getTransactionEntity(entity.getClass(), key);    if (oldEntity == null) {      addTxEntity(entity);    }    else {      // XXX:      /*      // jpa/0s2d      Entity oldEntity = _txEntities.get(index);      _txEntities.set(index, entity);      */    }  }  /**   * Deletes the object.   *   * @param obj the object to delete   */  public void delete(Entity entity)    throws SQLException  {    Entity oldEntity = getEntity(entity.getClass(),                                 entity.__caucho_getPrimaryKey());    if (oldEntity == null) {      throw new IllegalStateException(L.l("AmberEntity[{0}:{1}] cannot be deleted since it is not managed",                                          entity.getClass().getName(),                                          entity.__caucho_getPrimaryKey()));      /*        EntityType entityType = entity.__caucho_getEntityType();        if (entityType == null)          return;        // throw new AmberException(L.l("entity has no entityType"));        AmberEntityHome entityHome = entityType.getHome();        //entityHome = _persistenceUnit.getEntityHome(entity.getClass().getName());        if (entityHome == null)          throw new AmberException(L.l("entity has no matching home"));        // XXX: this makes no sense        entityHome.makePersistent(entity, this, true);        addEntity(entity);      */    }    else {      // XXX: jpa/0k12      oldEntity.__caucho_setConnection(this);      entity = oldEntity;    }    entity.__caucho_delete();  }  /**   * Creates a query object from a query string.   *   * @param query a Hibernate query   */  public AmberQuery prepareQuery(String queryString)    throws AmberException  {    return prepareQuery(queryString, false);  }  /**   * Creates a query object from a query string.   *   * @param query a Hibernate query   */  public AmberQuery prepareLazyQuery(String queryString)    throws AmberException  {    return prepareQuery(queryString, true);  }  /**   * Creates a query object from a query string.   *   * @param query a Hibernate query   */  public AmberQuery prepareUpdate(String queryString)    throws AmberException  {    return prepareQuery(queryString, true);  }  /**   * Creates a query object from a query string.   *   * @param query a Hibernate query   */  private AmberQuery prepareQuery(String queryString, boolean isLazy)    throws AmberException  {    AbstractQuery queryProgram = parseQuery(queryString, isLazy);    UserQuery query = new UserQuery(queryProgram);    query.setSession(this);    return query;  }  /**   * Creates a query object from a query string.   *   * @param query a Hibernate query   */  public AbstractQuery parseQuery(String sql, boolean isLazy)    throws AmberException  {    try {      _persistenceUnit.initEntityHomes();    } catch (Exception e) {      throw AmberRuntimeException.create(e);    }    AbstractQuery query = _persistenceUnit.getQueryParseCache(sql);    if (query == null) {      QueryParser parser = new QueryParser(sql);      parser.setPersistenceUnit(_persistenceUnit);      parser.setLazyResult(isLazy);      query = parser.parse();      _persistenceUnit.putQueryParseCache(sql, query);    }    return query;  }  /**   * Select a list of objects with a Hibernate query.   *   * @param query the hibernate query   *   * @return the query results.   */  public ResultSet query(String hsql)    throws SQLException  {    AmberQuery query = prepareQuery(hsql);    return query.executeQuery();  }  /**   * Returns the cache chunk.   *   * @param sql the SQL for the cache chunk   * @param args the filled parameters for the cache chunk   * @param startRow the starting row for the cache chunk   */  public ResultSetCacheChunk getQueryCacheChunk(String sql,                                                Object []args,                                                int startRow)  {    _queryKey.init(sql, args, startRow);    return _persistenceUnit.getQueryChunk(_queryKey);  }  /**   * Returns the result set meta data from cache.   */  public ResultSetMetaData getQueryMetaData()  {    return _persistenceUnit.getQueryMetaData(_queryKey);  }  /**   * Sets the cache chunk.   *   * @param sql the SQL for the cache chunk   * @param args the filled parameters for the cache chunk   * @param startRow the starting row for the cache chunk   * @param cacheChunk the new value of the cache chunk   */  public void putQueryCacheChunk(String sql,                                 Object []args,                                 int startRow,                                 ResultSetCacheChunk cacheChunk,                                 ResultSetMetaData cacheMetaData)  {    QueryCacheKey key = new QueryCacheKey();    Object []newArgs = new Object[args.length];    System.arraycopy(args, 0, newArgs, 0, args.length);    key.init(sql, newArgs, startRow);    _persistenceUnit.putQueryChunk(key, cacheChunk);    _persistenceUnit.putQueryMetaData(key, cacheMetaData);  }  /**   * Updates the database with a query   *   * @param query the hibernate query   *   * @return the query results.   */  public int update(String hsql)    throws SQLException  {    AmberQuery query = prepareUpdate(hsql);    return query.executeUpdate();  }  /**   * Select a list of objects with a Hibernate query.   *   * @param query the hibernate query   *   * @return the query results.   */  public List find(String hsql)    throws SQLException  {    AmberQuery query = prepareQuery(hsql);    return query.list();  }  /**   * Cleans up the connection.   */  public void cleanup()  {    if (log.isLoggable(Level.FINER))      log.log(Level.FINER, "AmberConnection.cleanup");    try {      // XXX: also needs to check PersistenceContextType.TRANSACTION/EXTENDED.      // jpa/0g04      if (isActiveTransaction()) {        flushInternal();      }    }    catch (RuntimeException e) {      throw e;    }    catch (SQLException e) {      throw new IllegalStateException(e);    }    catch (Exception e) {      throw new EJBExceptionWrapper(e);    }    finally {      _depth = 0;      for (int i = _entitiesTop - 1; i >= 0; i--) {        _entities[i].__caucho_detach();      }      _entitiesTop = 0;      _txEntitiesTop = 0;      _completionList.clear();      freeConnection();    }  }  /**   * Pushes the depth.   */  public void pushDepth()  {    // these aren't necessary because the AmberConnection is added as    // a close callback to the UserTransaction  }  /**   * Pops the depth.   */  public void popDepth()  {  }  /**   * Frees the connection.   */  public void freeConnection()  {    closeConnectionImpl();  }  /**   * Frees the connection.   */  private void closeConnectionImpl()  {    Connection conn = _conn;    _conn = null;    Connection readConn = _readConn;    _readConn = null;    boolean isAutoCommit = _isAutoCommit;    _isAutoCommit = true;    try {      if (conn != null && ! isAutoCommit)        conn.setAutoCommit(true);    } catch (SQLException e) {    }    for (Statement stmt : _statements) {      try {        stmt.close();      } catch (Exception e) {        log.log(Level.WARNING, e.toString(), e);      }    }    try {      _preparedStatementMap.clear();      _statements.clear();      if (conn != null)        conn.close();      if (readConn != null)        readConn.close();    } catch (Exception e) {      log.log(Level.WARNING, e.toString(), e);    }  }  @Override  public String toString()  {    if (_persistenceUnit != null)      return "AmberConnection[" + _persistenceUnit.getName() + "]";    else      return "AmberConnection[closed]";  }  /**   * Finalizer.   */  @Override  public void finalize()  {    cleanup();  }  /**   * Returns true when cache items can be used.   */  public boolean shouldRetrieveFromCache()  {    // ejb/0d01    return (! isActiveTransaction());  }  public void setTransactionalState(Entity entity)  {    if (isActiveTransaction()) {      // jpa/0ga8      entity.__caucho_setConnection(this);      // jpa/0j5f      EntityState state = entity.__caucho_getEntityState();      //if (state.ordinal() < EntityState.P_DELETING.ordinal())      if (state == EntityState.P_NON_TRANSACTIONAL)        entity.__caucho_setEntityState(EntityState.P_TRANSACTIONAL);    }  }  public boolean isCacheEntity(Entity entity)  {    return entity == getCacheEntity(entity, true);  }  public Entity getCacheEntity(Entity entity)  {    return getCacheEntity(entity, false);  }  public Entity getCacheEntity(Entity entity,                               boolean isDebug)  {    // jpa/0h0a    if (entity == null)      return null;    // XXX: jpa/0h20, the cache entity is only available after commit.    Entity cacheEntity = entity.__caucho_getCacheEntity();    if (cacheEntity != null)      return cacheEntity;    return getCacheEntity(entity.getClass(),                          entity.__caucho_getPrimaryKey(),                          isDebug);  }  public Entity getCacheEntity(Class cl, Object pk)  {    return getCacheEntity(cl, pk, false);  }  // jpa/0h20  public Entity getCacheEntity(Class cl, Object pk, boolean isDebug)  {    if (pk == null)      return null;    String className = cl.getName();    AmberEntityHome entityHome = _persistenceUnit.getEntityHome(className);    if (entityHome == null) {      if (log.isLoggable(Level.FINER))        log.log(Level.FINER, L.l("Home not found for entity (class: '{0}' PK: '{1}')",                                 className, pk));      return null;    }    EntityType rootType = entityHome.getRootType();    EntityItem item = _persistenceUnit.getEntity(rootType, pk);    if (item == null)      return null;    // jpa/0o0b    if (isDebug)      return item.getEntity();    // XXX: jpa/0h31, expires the child cache entity.    if (isActiveTransaction()) {      Entity txEntity = getTransactionEntity(cl, pk);      if (txEntity != null)        txEntity.__caucho_getEntityState();      else // jpa/0o0b || ! state.isManaged()) {        item.getEntity().__caucho_expire();      return null;    }    return item.getEntity();  }  //  // private  //  // throws Exception (for jpa)  //  // ejb/0g22 (cmp) expects exception handling in  // the public methods. See public void create(Object) above.  /**   * Adds an entity to the context, assuming it has not been added yet.   * Also, if there is a transaction, adds the entity to the list of   * transactional entities.   */  private void addInternalEntity(Entity entity)  {    if (log.isLoggable(Level.FINEST)) {      log.log(Level.FINEST, L.l("amber {0}[{1}] addInternalEntity",                                entity.getClass().getName(),                                entity.__caucho_getPrimaryKey()));    }    addEntity(entity);    // jpa/0g06    if (isActiveTra

⌨️ 快捷键说明

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