Example usage for org.hibernate.id.enhanced Optimizer generate

List of usage examples for org.hibernate.id.enhanced Optimizer generate

Introduction

In this page you can find the example usage for org.hibernate.id.enhanced Optimizer generate.

Prototype

public Serializable generate(AccessCallback callback);

Source Link

Document

Generate an identifier value accounting for this specific optimization.

Usage

From source file:org.apereo.portal.utils.HibernateStyleCounterStore.java

License:Apache License

private int getNextIdInternal(final Optimizer optimizer, final String counterName) {

    return (Integer) optimizer.generate(new AccessCallback() {
        @Override//w ww  .  j av  a 2 s . c  om
        public IntegralDataTypeHolder getNextValue() {

            IntegralDataTypeHolder rslt = null;
            for (int i = 0; rslt == null && i < MAX_ATTEMPTS; i++) {

                rslt = transactionOperations.execute(new TransactionCallback<IntegralDataTypeHolder>() {
                    @Override
                    public IntegralDataTypeHolder doInTransaction(TransactionStatus status) {
                        final IntegralDataTypeHolder value = IdentifierGeneratorHelper
                                .getIntegralDataTypeHolder(identifierType.getReturnedClass());

                        //Try and load the current value, returns true if the expected 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
                        int rowsAltered = 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);
                            }
                        });

                        return rowsAltered > 0 ? value // Success
                                : null; // Failed;  try again...
                    }
                });
            } // End for loop

            if (rslt == null) {
                throw new RuntimeException(
                        "Failed to fetch a new batch of sequence values after " + MAX_ATTEMPTS + " tries");
            }

            return rslt;
        }
    });
}

From source file:org.jasig.portal.utils.HibernateStyleCounterStore.java

License:Apache License

private int getNextIdInternal(final Optimizer optimizer, final String counterName) {
    return (Integer) optimizer.generate(new AccessCallback() {
        @Override//w ww .  j ava 2  s. c  o m
        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);

                    return value;
                }
            });
        }
    });
}