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();
}
}
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
}
}
}
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()));
}
}
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);
}
Aggregations