Package org.hibernate.id

Examples of org.hibernate.id.IntegralDataTypeHolder


  @Override
  public Serializable generate(AccessCallback callback) {
    // IMPL NOTE : it is incredibly important that the method-local variable be used here to
    //    avoid concurrency issues.
    IntegralDataTypeHolder value = null;
    while ( value == null || value.lt( 1 ) ) {
      value = callback.getNextValue();
    }
    lastSourceValue = value;
    return value.makeValue();
  }
View Full Code Here


          public IntegralDataTypeHolder getNextValue() {
            return session.getTransactionCoordinator().getTransaction().createIsolationDelegate().delegateWork(
                new AbstractReturningWork<IntegralDataTypeHolder>() {
                  @Override
                  public IntegralDataTypeHolder execute(Connection connection) throws SQLException {
                    final IntegralDataTypeHolder value = IdentifierGeneratorHelper.getIntegralDataTypeHolder(
                        identifierType.getReturnedClass()
                    );
                    int rows;
                    do {
                      statementLogger.logStatement(
                          selectQuery,
                          FormatStyle.BASIC.getFormatter()
                      );
                      PreparedStatement selectPS = connection.prepareStatement( selectQuery );
                      try {
                        selectPS.setString( 1, segmentValue );
                        final ResultSet selectRS = selectPS.executeQuery();
                        if ( !selectRS.next() ) {
                          value.initialize( initialValue );
                          PreparedStatement insertPS = null;
                          try {
                            statementLogger.logStatement(
                                insertQuery,
                                FormatStyle.BASIC.getFormatter()
                            );
                            insertPS = connection.prepareStatement( insertQuery );
                            insertPS.setString( 1, segmentValue );
                            value.bind( insertPS, 2 );
                            insertPS.execute();
                          }
                          finally {
                            if ( insertPS != null ) {
                              insertPS.close();
                            }
                          }
                        }
                        else {
                          value.initialize( selectRS, 1 );
                        }
                        selectRS.close();
                      }
                      catch (SQLException e) {
                        LOG.unableToReadOrInitHiValue( e );
                        throw e;
                      }
                      finally {
                        selectPS.close();
                      }

                      statementLogger.logStatement(
                          updateQuery,
                          FormatStyle.BASIC.getFormatter()
                      );
                      final PreparedStatement updatePS = connection.prepareStatement( updateQuery );
                      try {
                        final IntegralDataTypeHolder updateValue = value.copy();
                        if ( optimizer.applyIncrementSizeToSourceValues() ) {
                          updateValue.add( incrementSize );
                        }
                        else {
                          updateValue.increment();
                        }
                        updateValue.bind( updatePS, 1 );
                        value.bind( updatePS, 2 );
                        updatePS.setString( 3, segmentValue );
                        rows = updatePS.executeUpdate();
                      }
                      catch (SQLException e) {
View Full Code Here

          final PreparedStatement st = session.getTransactionCoordinator().getJdbcCoordinator().getStatementPreparer().prepareStatement( sql );
          try {
            final ResultSet rs = session.getTransactionCoordinator().getJdbcCoordinator().getResultSetReturn().extract( st );
            try {
              rs.next();
              final IntegralDataTypeHolder value = IdentifierGeneratorHelper.getIntegralDataTypeHolder( numberType );
              value.initialize( rs, 1 );
              if ( LOG.isDebugEnabled() ) {
                LOG.debugf( "Sequence value obtained: %s", value.makeValue() );
              }
              return value;
            }
            finally {
              try {
View Full Code Here

    @Override
    public synchronized Serializable generate(AccessCallback callback) {
      // IMPL NOTE : it is incredibly important that the method-local variable be used here to
      //    avoid concurrency issues.
      IntegralDataTypeHolder value = null;
      while ( value == null || value.lt( 1 ) ) {
        value = callback.getNextValue();
      }
      lastSourceValue = value;
      return value.makeValue();
    }
View Full Code Here

          public IntegralDataTypeHolder getNextValue() {
            return session.getTransactionCoordinator().getTransaction().createIsolationDelegate().delegateWork(
                new AbstractReturningWork<IntegralDataTypeHolder>() {
                  @Override
                  public IntegralDataTypeHolder execute(Connection connection) throws SQLException {
                    final IntegralDataTypeHolder value = makeValue();
                    int rows;
                    do {
                      final PreparedStatement selectPS = prepareStatement( connection, selectQuery, statementLogger, statsCollector );

                      try {
                        selectPS.setString( 1, segmentValue );
                        final ResultSet selectRS = executeQuery( selectPS, statsCollector );
                        if ( !selectRS.next() ) {
                          value.initialize( initialValue );

                          final PreparedStatement insertPS = prepareStatement( connection, insertQuery, statementLogger, statsCollector );
                          try {
                            insertPS.setString( 1, segmentValue );
                            value.bind( insertPS, 2 );
                            executeUpdate( insertPS, statsCollector );
                          }
                          finally {
                            insertPS.close();
                          }
                        }
                        else {
                          value.initialize( selectRS, 1 );
                        }
                        selectRS.close();
                      }
                      catch ( SQLException e ) {
                          LOG.unableToReadOrInitHiValue(e);
                        throw e;
                      }
                      finally {
                        selectPS.close();
                      }


                      final PreparedStatement updatePS = prepareStatement( connection, updateQuery, statementLogger, statsCollector );
                      try {
                        final IntegralDataTypeHolder updateValue = value.copy();
                        if ( optimizer.applyIncrementSizeToSourceValues() ) {
                          updateValue.add( incrementSize );
                        }
                        else {
                          updateValue.increment();
                        }
                        updateValue.bind( updatePS, 1 );
                        value.bind( updatePS, 2 );
                        updatePS.setString( 3, segmentValue );
                        rows = executeUpdate( updatePS, statsCollector );
                      }
                      catch ( SQLException e ) {
View Full Code Here

    RowKey key = new RowKey(
        tableName,
        new String[] { segmentColumnName },
        new Object[] { segmentColumnValue }
    );
    IntegralDataTypeHolder value = IdentifierGeneratorHelper.getIntegralDataTypeHolder( identifierType.getReturnedClass() );

    boolean done = false;
    do {
      //read value
      //skip locking proposed by Sanne
      Object valueFromDb = identifierCache.withFlags( Flag.SKIP_LOCKING ).get( key );
      if ( valueFromDb == null ) {
        //if not there, insert initial value
        value.initialize( initialValue );
        valueFromDb = nullSafeSet( identifierValueGridType, value.makeValue().longValue(), valueColumnName, session, tuple );
        final Object oldValue = identifierCache.putIfAbsent( key, valueFromDb );
        //check in case somebody has inserted it behind our back
        if ( oldValue != null ) {
          value.initialize( ( (Number) oldValue ).longValue() );
          valueFromDb = oldValue;
        }
      }
      else {
        //read the value from the table
        value.initialize( ( ( Number ) valueFromDb ).longValue() );
      }

      //update value
      final IntegralDataTypeHolder updateValue = value.copy();
      //increment value
      if ( optimizer.applyIncrementSizeToSourceValues() ) {
        updateValue.add( incrementSize );
      }
      else {
        updateValue.increment();
      }
      final Object newValueFromDb = nullSafeSet(
          identifierValueGridType, updateValue.makeValue().longValue(), valueColumnName, session, tuple
      );
      done = identifierCache.replace( key, valueFromDb, newValueFromDb );
    }
    while ( !done );
View Full Code Here

        //read the value from the table
        value.initialize( ( (Number) valueFromDb ).longValue() );
      }

      //update value
      final IntegralDataTypeHolder updateValue = value.copy();
      //increment value
      updateValue.add( increment );
      //TODO should we use GridTypes here?
      final Object newValueFromDb = updateValue.makeValue();
      done = identifierCache.replace( key, valueFromDb, newValueFromDb );
    }
    while ( !done );
  }
View Full Code Here

            @Override
            public IntegralDataTypeHolder getNextValue() {
                return transactionOperations.execute(new TransactionCallback<IntegralDataTypeHolder>() {
                    @Override
                    public IntegralDataTypeHolder doInTransaction(TransactionStatus status) {
                        final IntegralDataTypeHolder value = IdentifierGeneratorHelper.getIntegralDataTypeHolder(identifierType.getReturnedClass());
                        int rows;
                        do {
                            //Try and load the current value, returns true if the exepected row exists, null otherwise
                            final boolean selected = jdbcOperations.query(SELECT_QUERY, new ResultSetExtractor<Boolean>() {
                                        @Override
                                        public Boolean extractData(ResultSet rs) throws SQLException, DataAccessException {
                                            if (rs.next()) {
                                                value.initialize(rs, 1);
                                                return true;
                                            }
                                            return false;
                                        }
                                }, counterName);
                           
                            //No row exists for the counter, insert it
                            if (!selected) {
                                value.initialize(initialValue);
                               
                                jdbcOperations.update(INSERT_QUERY, new PreparedStatementSetter() {
                                    @Override
                                    public void setValues(PreparedStatement ps) throws SQLException {
                                        ps.setString(1, counterName);
                                        value.bind(ps, 2);
                                    }
                                });
                            }
                           
                            //Increment the counter row value
                            final IntegralDataTypeHolder updateValue = value.copy();
                            if (optimizer.applyIncrementSizeToSourceValues()) {
                                updateValue.add(incrementSize);
                            }
                            else {
                                updateValue.increment();
                            }
                           
                            //Update the counter row, if rows returns 0 the update failed due to a race condition, it will be retried
                            rows = jdbcOperations.update(UPDATE_QUERY, new PreparedStatementSetter() {
                                @Override
                                public void setValues(PreparedStatement ps) throws SQLException {
                                    updateValue.bind(ps, 1);
                                    ps.setString(2, counterName);
                                    value.bind(ps, 3);
                                }
                            });
                        } while (rows == 0);
View Full Code Here

     * {@inheritDoc}
     */
    public Serializable generate(AccessCallback callback) {
      // IMPL NOTE : it is incredibly important that the method-local variable be used here to
      //    avoid concurrency issues.
      IntegralDataTypeHolder value = null;
      while ( value == null || value.lt( 1 ) ) {
        value = callback.getNextValue();
      }
      lastSourceValue = value;
      return value.makeValue();
    }
View Full Code Here

  /**
   * {@inheritDoc}
   */
  public Serializable doWorkInCurrentTransaction(Connection conn, String sql) throws SQLException {
    IntegralDataTypeHolder value = IdentifierGeneratorHelper.getIntegralDataTypeHolder( identifierType.getReturnedClass() );
    int rows;
    do {
      SQL_STATEMENT_LOGGER.logStatement( selectQuery, FormatStyle.BASIC );
      PreparedStatement selectPS = conn.prepareStatement( selectQuery );
      try {
        selectPS.setString( 1, segmentValue );
        ResultSet selectRS = selectPS.executeQuery();
        if ( !selectRS.next() ) {
          value.initialize( initialValue );
          PreparedStatement insertPS = null;
          try {
            SQL_STATEMENT_LOGGER.logStatement( insertQuery, FormatStyle.BASIC );
            insertPS = conn.prepareStatement( insertQuery );
            insertPS.setString( 1, segmentValue );
            value.bind( insertPS, 2 );
            insertPS.execute();
          }
          finally {
            if ( insertPS != null ) {
              insertPS.close();
            }
          }
        }
        else {
          value.initialize( selectRS, 1 );
        }
        selectRS.close();
      }
      catch ( SQLException sqle ) {
        log.error( "could not read or init a hi value", sqle );
        throw sqle;
      }
      finally {
        selectPS.close();
      }

      SQL_STATEMENT_LOGGER.logStatement( updateQuery, FormatStyle.BASIC );
      PreparedStatement updatePS = conn.prepareStatement( updateQuery );
      try {
        final IntegralDataTypeHolder updateValue = value.copy();
        if ( optimizer.applyIncrementSizeToSourceValues() ) {
          updateValue.add( incrementSize );
        }
        else {
          updateValue.increment();
        }
        updateValue.bind( updatePS, 1 );
        value.bind( updatePS, 2 );
        updatePS.setString( 3, segmentValue );
        rows = updatePS.executeUpdate();
      }
      catch ( SQLException sqle ) {
View Full Code Here

TOP

Related Classes of org.hibernate.id.IntegralDataTypeHolder

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.