Search in sources :

Example 1 with ValueGenerationBlock

use of org.datanucleus.store.valuegenerator.ValueGenerationBlock in project datanucleus-rdbms by datanucleus.

the class MaxGenerator method reserveBlock.

/**
 * Method to reserve a block of identities.
 * Note : Only allocates a single id always.
 * @param size The block size
 * @return The reserved block
 */
public ValueGenerationBlock reserveBlock(long size) {
    try {
        // search an Id in the database
        ManagedConnection mconn = connectionProvider.retrieveConnection();
        PreparedStatement ps = null;
        ResultSet rs = null;
        SQLController sqlControl = ((RDBMSStoreManager) storeMgr).getSQLController();
        try {
            String stmt = getStatement();
            ps = sqlControl.getStatementForUpdate(mconn, stmt, false);
            rs = sqlControl.executeStatementQuery(null, mconn, stmt, ps);
            if (!rs.next()) {
                return new ValueGenerationBlock(new Object[] { Long.valueOf(1) });
            }
            return new ValueGenerationBlock(new Object[] { Long.valueOf(rs.getLong(1) + 1) });
        } catch (SQLException e) {
            NucleusLogger.VALUEGENERATION.warn("Exception thrown getting next value for MaxGenerator", e);
            throw new ValueGenerationException("Exception thrown getting next value for MaxGenerator", e);
        } finally {
            try {
                if (rs != null) {
                    rs.close();
                }
                if (ps != null) {
                    sqlControl.closeStatement(mconn, ps);
                }
            } catch (SQLException e) {
            // no recoverable error
            }
        }
    } finally {
        connectionProvider.releaseConnection();
    }
}
Also used : SQLException(java.sql.SQLException) ResultSet(java.sql.ResultSet) ValueGenerationBlock(org.datanucleus.store.valuegenerator.ValueGenerationBlock) ManagedConnection(org.datanucleus.store.connection.ManagedConnection) PreparedStatement(java.sql.PreparedStatement) ValueGenerationException(org.datanucleus.store.valuegenerator.ValueGenerationException) SQLController(org.datanucleus.store.rdbms.SQLController) RDBMSStoreManager(org.datanucleus.store.rdbms.RDBMSStoreManager)

Example 2 with ValueGenerationBlock

use of org.datanucleus.store.valuegenerator.ValueGenerationBlock in project datanucleus-rdbms by datanucleus.

the class SequenceGenerator method reserveBlock.

/**
 * Reserve a block of ids.
 * @param size Block size
 * @return The reserved block
 */
protected synchronized ValueGenerationBlock<Long> reserveBlock(long size) {
    if (size < 1) {
        return null;
    }
    PreparedStatement ps = null;
    ResultSet rs = null;
    List oid = new ArrayList();
    RDBMSStoreManager srm = (RDBMSStoreManager) storeMgr;
    SQLController sqlControl = srm.getSQLController();
    try {
        // Get next available id
        DatastoreAdapter dba = srm.getDatastoreAdapter();
        String stmt = dba.getSequenceNextStmt(sequenceName);
        ps = sqlControl.getStatementForQuery(connection, stmt);
        rs = sqlControl.executeStatementQuery(null, connection, stmt, ps);
        Long nextId = Long.valueOf(0);
        if (rs.next()) {
            nextId = Long.valueOf(rs.getLong(1));
            oid.add(nextId);
        }
        for (int i = 1; i < size; i++) {
            // size must match key-increment-by otherwise it will
            // cause duplicates keys
            nextId = Long.valueOf(nextId.longValue() + 1);
            oid.add(nextId);
        }
        if (NucleusLogger.VALUEGENERATION.isDebugEnabled()) {
            NucleusLogger.VALUEGENERATION.debug(Localiser.msg("040004", "" + size));
        }
        return new ValueGenerationBlock<>(oid);
    } catch (SQLException e) {
        throw new ValueGenerationException(Localiser.msg("061001", e.getMessage()), e);
    } finally {
        try {
            if (rs != null) {
                rs.close();
            }
            if (ps != null) {
                sqlControl.closeStatement(connection, ps);
            }
        } catch (SQLException e) {
        // non-recoverable error
        }
    }
}
Also used : SQLException(java.sql.SQLException) ArrayList(java.util.ArrayList) ValueGenerationBlock(org.datanucleus.store.valuegenerator.ValueGenerationBlock) PreparedStatement(java.sql.PreparedStatement) ValueGenerationException(org.datanucleus.store.valuegenerator.ValueGenerationException) RDBMSStoreManager(org.datanucleus.store.rdbms.RDBMSStoreManager) SQLController(org.datanucleus.store.rdbms.SQLController) ResultSet(java.sql.ResultSet) ArrayList(java.util.ArrayList) List(java.util.List) DatastoreAdapter(org.datanucleus.store.rdbms.adapter.DatastoreAdapter)

Example 3 with ValueGenerationBlock

use of org.datanucleus.store.valuegenerator.ValueGenerationBlock in project datanucleus-rdbms by datanucleus.

the class TableGenerator method reserveBlock.

/**
 * Method to reserve a block of "size" identities.
 * @param size Block size
 * @return The reserved block
 */
public ValueGenerationBlock<Long> reserveBlock(long size) {
    if (size < 1) {
        return null;
    }
    // search for an ID in the database
    List<Long> oid = new ArrayList<>();
    try {
        if (sequenceTable == null) {
            initialiseSequenceTable();
        }
        DatastoreIdentifier sourceTableIdentifier = null;
        if (properties.containsKey(ValueGenerator.PROPERTY_TABLE_NAME)) {
            sourceTableIdentifier = ((RDBMSStoreManager) storeMgr).getIdentifierFactory().newTableIdentifier(properties.getProperty(ValueGenerator.PROPERTY_TABLE_NAME));
        // TODO Apply passed in catalog/schema to this identifier rather than the default for the factory
        }
        Long nextId = sequenceTable.getNextVal(sequenceName, connection, (int) size, sourceTableIdentifier, properties.getProperty(ValueGenerator.PROPERTY_COLUMN_NAME), initialValue);
        for (int i = 0; i < size; i++) {
            oid.add(nextId);
            nextId = Long.valueOf(nextId.longValue() + 1);
        }
        if (NucleusLogger.VALUEGENERATION.isDebugEnabled()) {
            NucleusLogger.VALUEGENERATION.debug(Localiser.msg("040004", "" + size));
        }
        return new ValueGenerationBlock<>(oid);
    } catch (SQLException e) {
        throw new ValueGenerationException(Localiser.msg("061001", e.getMessage()));
    }
}
Also used : SQLException(java.sql.SQLException) DatastoreIdentifier(org.datanucleus.store.rdbms.identifier.DatastoreIdentifier) ArrayList(java.util.ArrayList) ValueGenerationBlock(org.datanucleus.store.valuegenerator.ValueGenerationBlock) ValueGenerationException(org.datanucleus.store.valuegenerator.ValueGenerationException) RDBMSStoreManager(org.datanucleus.store.rdbms.RDBMSStoreManager)

Example 4 with ValueGenerationBlock

use of org.datanucleus.store.valuegenerator.ValueGenerationBlock in project datanucleus-rdbms by datanucleus.

the class DatastoreUUIDHexGenerator method reserveBlock.

/**
 * Reserve a block of ids.
 * @param size Block size
 * @return The reserved block
 */
protected synchronized ValueGenerationBlock<String> reserveBlock(long size) {
    if (size < 1) {
        return null;
    }
    List<String> oids = new ArrayList<>();
    try {
        ManagedConnection mconn = connectionProvider.retrieveConnection();
        PreparedStatement ps = null;
        ResultSet rs = null;
        RDBMSStoreManager rdbmsMgr = (RDBMSStoreManager) storeMgr;
        SQLController sqlControl = rdbmsMgr.getSQLController();
        try {
            // Find the next ID from the database
            DatastoreAdapter dba = rdbmsMgr.getDatastoreAdapter();
            String stmt = dba.getSelectNewUUIDStmt();
            ps = sqlControl.getStatementForQuery(mconn, stmt);
            for (int i = 1; i < size; i++) {
                rs = sqlControl.executeStatementQuery(null, mconn, stmt, ps);
                if (rs.next()) {
                    oids.add(rs.getString(1));
                }
            }
        } catch (SQLException e) {
            throw new ValueGenerationException(Localiser.msg("040008", e.getMessage()));
        } finally {
            try {
                if (rs != null) {
                    rs.close();
                }
                if (ps != null) {
                    sqlControl.closeStatement(mconn, ps);
                }
            } catch (SQLException e) {
            // non-recoverable error
            }
        }
    } finally {
        connectionProvider.releaseConnection();
    }
    return new ValueGenerationBlock(oids);
}
Also used : SQLException(java.sql.SQLException) ArrayList(java.util.ArrayList) ValueGenerationBlock(org.datanucleus.store.valuegenerator.ValueGenerationBlock) PreparedStatement(java.sql.PreparedStatement) ValueGenerationException(org.datanucleus.store.valuegenerator.ValueGenerationException) RDBMSStoreManager(org.datanucleus.store.rdbms.RDBMSStoreManager) SQLController(org.datanucleus.store.rdbms.SQLController) ResultSet(java.sql.ResultSet) ManagedConnection(org.datanucleus.store.connection.ManagedConnection) DatastoreAdapter(org.datanucleus.store.rdbms.adapter.DatastoreAdapter)

Aggregations

SQLException (java.sql.SQLException)4 RDBMSStoreManager (org.datanucleus.store.rdbms.RDBMSStoreManager)4 ValueGenerationBlock (org.datanucleus.store.valuegenerator.ValueGenerationBlock)4 ValueGenerationException (org.datanucleus.store.valuegenerator.ValueGenerationException)4 PreparedStatement (java.sql.PreparedStatement)3 ResultSet (java.sql.ResultSet)3 ArrayList (java.util.ArrayList)3 SQLController (org.datanucleus.store.rdbms.SQLController)3 ManagedConnection (org.datanucleus.store.connection.ManagedConnection)2 DatastoreAdapter (org.datanucleus.store.rdbms.adapter.DatastoreAdapter)2 List (java.util.List)1 DatastoreIdentifier (org.datanucleus.store.rdbms.identifier.DatastoreIdentifier)1