use of org.hibernate.LockOptions in project hibernate-orm by hibernate.
the class SQLServer2005DialectTestCase method testAppendLockHintUpgrade.
@Test
@TestForIssue(jiraKey = "HHH-9635")
public void testAppendLockHintUpgrade() {
final String expectedLockHint = "tab1 with (updlock, rowlock)";
LockOptions lockOptions = new LockOptions(LockMode.UPGRADE);
String lockHint = dialect.appendLockHint(lockOptions, "tab1");
assertEquals(expectedLockHint, lockHint);
}
use of org.hibernate.LockOptions in project hibernate-orm by hibernate.
the class SQLServer2005DialectTestCase method testAppendLockHintPessimisticWriteNoTimeOut.
@Test
@TestForIssue(jiraKey = "HHH-9635")
public void testAppendLockHintPessimisticWriteNoTimeOut() {
final String expectedLockHint = "tab1 with (updlock, rowlock, nowait)";
LockOptions lockOptions = new LockOptions(LockMode.UPGRADE);
lockOptions.setTimeOut(LockOptions.NO_WAIT);
String lockHint = dialect.appendLockHint(lockOptions, "tab1");
assertEquals(expectedLockHint, lockHint);
}
use of org.hibernate.LockOptions in project hibernate-orm by hibernate.
the class ParentChildTest method testLocking.
@Test
public void testLocking() throws Exception {
Session s = openSession();
Transaction tx = s.beginTransaction();
Simple s1 = new Simple(Long.valueOf(1));
s1.setCount(1);
Simple s2 = new Simple(Long.valueOf(2));
s2.setCount(2);
Simple s3 = new Simple(Long.valueOf(3));
s3.setCount(3);
Simple s4 = new Simple(Long.valueOf(4));
s4.setCount(4);
Simple s5 = new Simple(Long.valueOf(5));
s5.setCount(5);
s.save(s1);
s.save(s2);
s.save(s3);
s.save(s4);
s.save(s5);
assertTrue(s.getCurrentLockMode(s1) == LockMode.WRITE);
tx.commit();
s.close();
s = openSession();
tx = s.beginTransaction();
s1 = (Simple) s.load(Simple.class, new Long(1), LockMode.NONE);
//depends if cache is enabled
assertTrue(s.getCurrentLockMode(s1) == LockMode.READ || s.getCurrentLockMode(s1) == LockMode.NONE);
s2 = (Simple) s.load(Simple.class, new Long(2), LockMode.READ);
assertTrue(s.getCurrentLockMode(s2) == LockMode.READ);
s3 = (Simple) s.load(Simple.class, new Long(3), LockMode.UPGRADE);
assertTrue(s.getCurrentLockMode(s3) == LockMode.UPGRADE);
s4 = (Simple) s.byId(Simple.class).with(new LockOptions(LockMode.UPGRADE_NOWAIT)).load(4L);
assertTrue(s.getCurrentLockMode(s4) == LockMode.UPGRADE_NOWAIT);
s5 = (Simple) s.byId(Simple.class).with(new LockOptions(LockMode.UPGRADE_SKIPLOCKED)).load(5L);
assertTrue(s.getCurrentLockMode(s5) == LockMode.UPGRADE_SKIPLOCKED);
//upgrade
s1 = (Simple) s.load(Simple.class, new Long(1), LockMode.UPGRADE);
assertTrue(s.getCurrentLockMode(s1) == LockMode.UPGRADE);
s2 = (Simple) s.load(Simple.class, new Long(2), LockMode.NONE);
assertTrue(s.getCurrentLockMode(s2) == LockMode.READ);
s3 = (Simple) s.load(Simple.class, new Long(3), LockMode.READ);
assertTrue(s.getCurrentLockMode(s3) == LockMode.UPGRADE);
s4 = (Simple) s.load(Simple.class, new Long(4), LockMode.UPGRADE);
assertTrue(s.getCurrentLockMode(s4) == LockMode.UPGRADE_NOWAIT);
s5 = (Simple) s.load(Simple.class, new Long(5), LockMode.UPGRADE);
assertTrue(s.getCurrentLockMode(s5) == LockMode.UPGRADE_SKIPLOCKED);
//upgrade
s.lock(s2, LockMode.UPGRADE);
assertTrue(s.getCurrentLockMode(s2) == LockMode.UPGRADE);
s.lock(s3, LockMode.UPGRADE);
assertTrue(s.getCurrentLockMode(s3) == LockMode.UPGRADE);
s.lock(s1, LockMode.UPGRADE_NOWAIT);
s.lock(s4, LockMode.NONE);
s.lock(s5, LockMode.UPGRADE_SKIPLOCKED);
assertTrue(s.getCurrentLockMode(s4) == LockMode.UPGRADE_NOWAIT);
assertTrue(s.getCurrentLockMode(s5) == LockMode.UPGRADE_SKIPLOCKED);
tx.commit();
tx = s.beginTransaction();
assertTrue(s.getCurrentLockMode(s3) == LockMode.NONE);
assertTrue(s.getCurrentLockMode(s1) == LockMode.NONE);
assertTrue(s.getCurrentLockMode(s2) == LockMode.NONE);
assertTrue(s.getCurrentLockMode(s4) == LockMode.NONE);
assertTrue(s.getCurrentLockMode(s5) == LockMode.NONE);
//upgrade
s.lock(s1, LockMode.READ);
assertTrue(s.getCurrentLockMode(s1) == LockMode.READ);
//upgrade
s.lock(s2, LockMode.UPGRADE);
assertTrue(s.getCurrentLockMode(s2) == LockMode.UPGRADE);
//upgrade
s.lock(s3, LockMode.UPGRADE_NOWAIT);
assertTrue(s.getCurrentLockMode(s3) == LockMode.UPGRADE_NOWAIT);
s.lock(s4, LockMode.NONE);
assertTrue(s.getCurrentLockMode(s4) == LockMode.NONE);
s4.setName("s4");
s.flush();
assertTrue(s.getCurrentLockMode(s4) == LockMode.WRITE);
tx.commit();
tx = s.beginTransaction();
assertTrue(s.getCurrentLockMode(s3) == LockMode.NONE);
assertTrue(s.getCurrentLockMode(s1) == LockMode.NONE);
assertTrue(s.getCurrentLockMode(s2) == LockMode.NONE);
assertTrue(s.getCurrentLockMode(s4) == LockMode.NONE);
assertTrue(s.getCurrentLockMode(s5) == LockMode.NONE);
s.delete(s1);
s.delete(s2);
s.delete(s3);
s.delete(s4);
s.delete(s5);
tx.commit();
s.close();
}
use of org.hibernate.LockOptions in project hibernate-orm by hibernate.
the class SelectLockingStrategy method generateLockString.
protected String generateLockString(int timeout) {
final SessionFactoryImplementor factory = getLockable().getFactory();
final LockOptions lockOptions = new LockOptions(getLockMode());
lockOptions.setTimeOut(timeout);
final SimpleSelect select = new SimpleSelect(factory.getDialect()).setLockOptions(lockOptions).setTableName(getLockable().getRootTableName()).addColumn(getLockable().getRootTableIdentifierColumnNames()[0]).addCondition(getLockable().getRootTableIdentifierColumnNames(), "=?");
if (getLockable().isVersioned()) {
select.addCondition(getLockable().getVersionColumnName(), "=?");
}
if (factory.getSessionFactoryOptions().isCommentsEnabled()) {
select.setComment(getLockMode() + " lock " + getLockable().getEntityName());
}
return select.toStatementString();
}
use of org.hibernate.LockOptions in project hibernate-orm by hibernate.
the class TableGenerator method buildSelectQuery.
@SuppressWarnings("unchecked")
protected String buildSelectQuery(Dialect dialect) {
final String alias = "tbl";
final String query = "select " + StringHelper.qualify(alias, valueColumnName) + " from " + renderedTableName + ' ' + alias + " where " + StringHelper.qualify(alias, segmentColumnName) + "=?";
final LockOptions lockOptions = new LockOptions(LockMode.PESSIMISTIC_WRITE);
lockOptions.setAliasSpecificLockMode(alias, LockMode.PESSIMISTIC_WRITE);
final Map updateTargetColumnsMap = Collections.singletonMap(alias, new String[] { valueColumnName });
return dialect.applyLocksToSql(query, lockOptions, updateTargetColumnsMap);
}
Aggregations