Example usage for org.hibernate.engine.spi SharedSessionContractImplementor getEventListenerManager

List of usage examples for org.hibernate.engine.spi SharedSessionContractImplementor getEventListenerManager

Introduction

In this page you can find the example usage for org.hibernate.engine.spi SharedSessionContractImplementor getEventListenerManager.

Prototype

SessionEventListenerManager getEventListenerManager();

Source Link

Usage

From source file:net.e6tech.elements.persist.hibernate.ModifiedTableGenerator.java

License:Apache License

@Override
public Serializable generate(final SharedSessionContractImplementor session, final Object obj) {
    final SqlStatementLogger statementLogger = session.getFactory().getServiceRegistry()
            .getService(JdbcServices.class).getSqlStatementLogger();
    final SessionEventListenerManager statsCollector = session.getEventListenerManager();

    return optimizer.generate(new AccessCallback() {
        @Override//w w  w  .jav  a2 s.c  o m
        public IntegralDataTypeHolder getNextValue() {
            return session.getTransactionCoordinator().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 {
                                            LOG.tracef("binding parameter [%s] - [%s]", 1, segmentValue);
                                            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) {
                                    LOG.unableToUpdateQueryHiValue(renderedTableName, e);
                                    throw e;
                                } finally {
                                    updatePS.close();
                                }
                            } while (rows == 0);

                            accessCount++;

                            return value;
                        }
                    }, true);
        }

        @Override
        public String getTenantIdentifier() {
            return session.getTenantIdentifier();
        }
    });
}