protected void applyQuerySettingsAndHints(Query query) { if ( lockOptions.getLockMode() != LockMode.NONE ) { query.setLockMode( getLockMode( lockOptions.getLockMode() ) ); } Object queryTimeout; if ( (queryTimeout = getProperties().get( QueryHints.SPEC_HINT_TIMEOUT ) ) != null ) { query.setHint( QueryHints.SPEC_HINT_TIMEOUT, queryTimeout ); } Object lockTimeout; if( (lockTimeout = getProperties().get( JPA_LOCK_TIMEOUT ))!=null){ query.setHint( JPA_LOCK_TIMEOUT, lockTimeout ); } }
@Override protected void initQueryFromNamedDefinition(Query query, NamedQueryDefinition namedQueryDefinition) { super.initQueryFromNamedDefinition( query, namedQueryDefinition ); if ( namedQueryDefinition.getLockOptions() != null ) { if ( namedQueryDefinition.getLockOptions().getLockMode() != null ) { query.setLockMode( LockModeTypeHelper.getLockModeType( namedQueryDefinition.getLockOptions().getLockMode() ) ); } } }
@Test @TestForIssue(jiraKey = "HHH-2735") public void testQueryLockModePessimisticWriteWithAlias() { doInHibernate( this::sessionFactory, session -> { // shouldn't throw an exception session.createQuery( "SELECT MAX(a.id)+1 FROM A a where a.value = :value" ) .setLockMode( "a", LockMode.PESSIMISTIC_WRITE ) .setParameter( "value", "it" ) .list(); } ); }
@Test @TestForIssue(jiraKey = "HHH-2735") public void testQueryLockModeNoneWithAlias() { doInHibernate( this::sessionFactory, session -> { // shouldn't throw an exception session.createQuery( "SELECT a.value FROM A a where a.id = :id" ) .setLockMode( "a", LockMode.NONE ) .setParameter( "id", id ) .list(); } ); }
@Test public void testQuery() { // open a session, begin a transaction and lock row doInHibernate( this::sessionFactory, session -> { A it = (A) session.createQuery( "from A a" ) .setLockMode( "a", LockMode.PESSIMISTIC_WRITE ) .uniqueResult(); // make sure we got it assertNotNull( it ); // that initial transaction is still active and so the lock should still be held. // Lets open another session/transaction and verify that we cannot update the row nowAttemptToUpdateRow(); } ); }
@Test @BMRules(rules = { @BMRule(targetClass = "org.hibernate.dialect.Dialect", targetMethod = "useFollowOnLocking", action = "return true", name = "H2DialectUseFollowOnLocking") }) public void testQuerySetLockModeNONEDoNotLogAWarnMessageWhenTheDialectUseFollowOnLockingIsTrue() { try (Session s = openSession();) { final Query query = s.createQuery( "from Item i join i.bids b where name = :name" ); query.setParameter( "name", "ZZZZ" ); query.setLockMode( "i", LockMode.NONE ); query.setLockMode( "b", LockMode.NONE ); query.list(); assertFalse( "Log message was not triggered", triggerable.wasTriggered() ); } }
protected void setQueryProperties(Query query) { if ( maxResults != null ) { query.setMaxResults( maxResults ); } if ( firstResult != null ) { query.setFirstResult( firstResult ); } if ( cacheable != null ) { query.setCacheable( cacheable ); } if ( cacheRegion != null ) { query.setCacheRegion( cacheRegion ); } if ( comment != null ) { query.setComment( comment ); } if ( flushMode != null ) { query.setFlushMode( flushMode ); } if ( cacheMode != null ) { query.setCacheMode( cacheMode ); } if ( timeout != null ) { query.setTimeout( timeout ); } if ( lockOptions != null && lockOptions.getLockMode() != LockMode.NONE ) { query.setLockMode( REFERENCED_ENTITY_ALIAS, lockOptions.getLockMode() ); } }
@Override public void setLockMode(final String alias, final LockMode lockMode) { if (queryV2ForCompare != null) { queryV2ForCompare.setLockMode(alias, lockMode); } query.setLockMode(alias, LockModeAdapter.adapt(lockMode)); }
protected void applyQuerySettingsAndHints(Query query) { if ( lockOptions.getLockMode() != LockMode.NONE ) { query.setLockMode( getLockMode( lockOptions.getLockMode() ) ); } Object queryTimeout; if ( (queryTimeout = getProperties().get( QueryHints.SPEC_HINT_TIMEOUT ) ) != null ) { query.setHint( QueryHints.SPEC_HINT_TIMEOUT, queryTimeout ); } Object lockTimeout; if( (lockTimeout = getProperties().get( JPA_LOCK_TIMEOUT ))!=null){ query.setHint( JPA_LOCK_TIMEOUT, lockTimeout ); } }
.setLockMode( LockModeType.NONE ) .setHibernateFlushMode( FlushMode.MANUAL ) .setCacheMode( cacheMode )
"where c.post.id = :id", PostComment.class) .setParameter("id", 1L) .setLockMode(LockModeType.PESSIMISTIC_WRITE) .getResultList();
"where c.post.id = :id", PostComment.class) .setParameter("id", 1L) .setLockMode(LockModeType.PESSIMISTIC_WRITE) .getResultList();
query.setLockMode( REFERENCED_ENTITY_ALIAS, lockOptions.getLockMode() );