Example usage for org.hibernate.engine.jdbc.internal FormatStyle BASIC

List of usage examples for org.hibernate.engine.jdbc.internal FormatStyle BASIC

Introduction

In this page you can find the example usage for org.hibernate.engine.jdbc.internal FormatStyle BASIC.

Prototype

FormatStyle BASIC

To view the source code for org.hibernate.engine.jdbc.internal FormatStyle BASIC.

Click Source Link

Document

Formatting for SELECT, INSERT, UPDATE and DELETE statements

Usage

From source file:com.literatejava.hibernate.allocator.LinearBlockAllocator.java

License:Open Source License

protected long allocateBlock(final SessionImplementor session) {
    AbstractReturningWork<Long> work = new AbstractReturningWork<Long>() {
        @Override//from  w  w  w .  j  a v  a  2  s.  co  m
        public Long execute(Connection conn) throws SQLException {
            final SqlStatementLogger statementLogger = session.getFactory().getServiceRegistry()
                    .getService(JdbcServices.class).getSqlStatementLogger();

            long result;
            int rows;
            do {
                // The loop ensures atomicity of the
                // select + update even for no transaction
                // or read committed isolation level

                statementLogger.logStatement(query, FormatStyle.BASIC.getFormatter());
                PreparedStatement qps = conn.prepareStatement(query);
                try {
                    qps.setString(1, sequenceName);
                    ResultSet rs = qps.executeQuery();
                    if (!rs.next()) {
                        String err = "could not read a hi value - you need to populate the table: " + tableName
                                + ", " + sequenceName;
                        log.error(err);
                        throw new IdentifierGenerationException(err);
                    }
                    result = rs.getLong(1);
                    rs.close();
                } catch (SQLException sqle) {
                    log.error("could not read a hi value", sqle);
                    throw sqle;
                } finally {
                    qps.close();
                }

                statementLogger.logStatement(update, FormatStyle.BASIC.getFormatter());
                PreparedStatement ups = conn.prepareStatement(update);
                try {
                    ups.setLong(1, result + blockSize);
                    ups.setString(2, sequenceName);
                    ups.setLong(3, result);
                    rows = ups.executeUpdate();
                } catch (SQLException sqle) {
                    log.error("could not update hi value in: " + tableName, sqle);
                    throw sqle;
                } finally {
                    ups.close();
                }
            } while (rows == 0);

            // success;
            //      -- allocated a Block.
            //
            statisticsTableAccessCount++;
            return new Long(result);
        }
    };

    // perform in an isolated Transaction.
    long allocated = session.getTransactionCoordinator().getTransaction().createIsolationDelegate()
            .delegateWork(work, true);
    return allocated;
}

From source file:de.innovationgate.webgate.api.mysql.GaleraClusterTableGenerator.java

License:Open Source License

@Override
public synchronized Serializable generate(final SessionImplementor session, Object obj) {
    final SqlStatementLogger statementLogger = session.getFactory().getServiceRegistry()
            .getService(JdbcServices.class).getSqlStatementLogger();
    return optimizer.generate(new AccessCallback() {
        @Override//ww  w  .  j  a  va  2 s . co m
        public IntegralDataTypeHolder getNextValue() {
            return session.getTransactionCoordinator().getTransaction().createIsolationDelegate()
                    .delegateWork(new AbstractReturningWork<IntegralDataTypeHolder>() {
                        @Override
                        public IntegralDataTypeHolder execute(Connection connection) throws SQLException {
                            IntegralDataTypeHolder value = IdentifierGeneratorHelper
                                    .getIntegralDataTypeHolder(identifierType.getReturnedClass());
                            int rows = 0;
                            do {
                                statementLogger.logStatement(selectQuery, FormatStyle.BASIC.getFormatter());
                                PreparedStatement selectPS = connection.prepareStatement(selectQuery);
                                try {
                                    selectPS.setString(1, segmentValue);
                                    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());
                                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) {
                                    // Another cluster node concurrently fetched and changed the ID. Just retry. 
                                    if (e.getErrorCode() == 1213) {
                                    } else {
                                        LOG.unableToUpdateQueryHiValue(tableName, e);
                                        throw e;
                                    }
                                } finally {
                                    updatePS.close();
                                }
                            } while (rows == 0);

                            accessCount++;

                            return value;
                        }
                    }, true);
        }
    });
}

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

License:Apache License

private PreparedStatement prepareStatement(Connection connection, String sql,
        SqlStatementLogger statementLogger, SessionEventListenerManager statsCollector) throws SQLException {
    statementLogger.logStatement(sql, FormatStyle.BASIC.getFormatter());
    try {// w  w w . j  av a2  s  .  c  om
        statsCollector.jdbcPrepareStatementStart();
        return connection.prepareStatement(sql);
    } finally {
        statsCollector.jdbcPrepareStatementEnd();
    }
}

From source file:org.activityinfo.server.database.hibernate.HibernateExecutor.java

License:Open Source License

private String format(final String statement) {
    return FormatStyle.BASIC.getFormatter().format(statement);
}