public long getNextSequence(TypeDescriptor typeDescriptor, Session session) {
    // check if there is an id in the cache
    ConcurrentLinkedQueue cachedIds =
        (ConcurrentLinkedQueue) this.typeDescriptorToIdCache.get(typeDescriptor);

    if (null == cachedIds) {
      typeDescriptorToIdCache.putIfAbsent(typeDescriptor, new ConcurrentLinkedQueue());

      cachedIds = (ConcurrentLinkedQueue) this.typeDescriptorToIdCache.get(typeDescriptor);
    }

    Number cachedId = (Number) cachedIds.poll();
    if (cachedId == null) {
      synchronized (cachedIds) {
        cachedId = (Number) cachedIds.poll();
        if (cachedId == null) {
          List newIds = this.getNextSequenceImpl(typeDescriptor, session);
          Assert.condition(0 < newIds.size());

          // reserve first for own use
          cachedId = (Number) newIds.remove(0);
          cachedIds.addAll(newIds);
        }
      }
    }

    if (trace.isDebugEnabled()) {
      trace.debug("returning unique ID: " + cachedId.longValue());
    }

    return cachedId.longValue();
  }
예제 #2
0
 /**
  * 执行统计查询语句,语句的执行结果必须只返回一个数值
  *
  * @param sql
  * @param params
  * @return
  * @throws DBException
  */
 public long stat(String sql, Object... params) throws SQLException {
   printIn(sql);
   printIn("params length:" + params.length);
   for (int i = 0; i < params.length; i++) {
     printIn("params" + i + ":" + params[i]);
   }
   Number num = (Number) queryRunner.query(getConnection(), sql, scalarHandler, params);
   return (num != null) ? num.longValue() : -1;
 }