|
Re: manually allocate a @GeneratedValue [message #488364 is a reply to message #488350] |
Mon, 28 September 2009 12:39 |
Tom Eugelink Messages: 825 Registered: July 2009 |
Senior Member |
|
|
> Is there a easy way to invoke the "get me the next PK for xxx"?
This does the job, but I hate having duplicate code and the allocating by Eclipselink may be done through a different connection, thus causing locking conflicts. So I still prefer using Eclipselink's code.
/**
* Allocate a block, return the highest allocated key.
* So if the block size is 3 and returned is 100, then 98, 99 and 100 are allocated.
*
* @param entityManager
* @param countColumnName
* @param nameColumnName
* @param sequenceTableName
* @param name
* @param blocksize
* @return the highest allocated value
*/
static public BigInteger allocateValueFromSequence(EntityManager entityManager, String countColumnName, String nameColumnName, String sequenceTableName, String name, int blocksize)
{
Query lQuery;
// number of attempts
for (int lAttempt = 0; lAttempt < 10; lAttempt++)
{
// get the last issued PK
lQuery = entityManager.createNativeQuery("select " + countColumnName + " from " + sequenceTableName + " where " + nameColumnName + "=?");
lQuery.setParameter(1, name);
BigInteger lLastPK = new BigInteger( JpaUtil.getSingleResultOrNull(lQuery).toString() );
// calculate the new "last issued PK"
BigInteger lAllocatedPK = lLastPK.add( BigInteger.valueOf(blocksize) );
// allocate it
lQuery = entityManager.createNativeQuery("update sequence set " + countColumnName + " = ? where " + nameColumnName + "=? and " + countColumnName + " = ?");
lQuery.setParameter(1, lAllocatedPK);
lQuery.setParameter(2, name);
lQuery.setParameter(3, lLastPK);
int lCnt = lQuery.executeUpdate();
// if we updated one record, we succeeded!
if (lCnt == 1) return lAllocatedPK; // the highest allocated key is the current value
// breeve pause (incremental)
ThreadUtil.sleep(lAttempt * 100);
}
// failed
throw new PersistenceException("Could not allocate a new value from sequence for " + name);
}
|
|
|
|
Powered by
FUDForum. Page generated in 0.02828 seconds