org.hibernate.exception.SQLGrammarException: could not lock: [persistent.jpa.AssociationsClassPeer#1]

Hibernate JIRA | Guenther Demetz | 8 years ago
tip
Click on the to mark the solution that helps you, Samebug will learn from it.
As a community member, you’ll be rewarded for you help.
  1. 0

    The method SQLServerDialect#appendLockHint currently ignores the LockMode.UPGRADE_NOWAIT returning the same string as for LockMode.UPGRADE. public String appendLockHint(LockMode mode, String tableName) { if ( mode.greaterThan( LockMode.READ ) ) { // does this need holdlock also? : return tableName + " with (updlock, rowlock, holdlock)"; return tableName + " with (updlock, rowlock)"; } else { return tableName; } } As SQLServer supports the nowait option I propose the following improvement: public String appendLockHint(LockMode mode, String tableName) { if ( mode == LockMode.UPGRADE_NOWAIT) { return tableName + " with (updlock, rowlock, nowait)"; } else if ( mode.greaterThan( LockMode.READ ) ) { // does this need holdlock also? : return tableName + " with (updlock, rowlock, holdlock)"; return tableName + " with (updlock, rowlock)"; } else { return tableName; } } A test gave me a correct behaviour: if the concerning row is already locked, then after Lock request time out period exceeded following exception is thrown: org.hibernate.exception.SQLGrammarException: could not lock: [persistent.jpa.AssociationsClassPeer#1] at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:90) at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:66) at org.hibernate.dialect.lock.SelectLockingStrategy.lock(SelectLockingStrategy.java:115) at org.hibernate.persister.entity.AbstractEntityPersister.lock(AbstractEntityPersister.java:1361) at org.hibernate.event.def.AbstractLockUpgradeEventListener.upgradeLock(AbstractLockUpgradeEventListener.java:108) at org.hibernate.event.def.DefaultLockEventListener.onLock(DefaultLockEventListener.java:87) at org.hibernate.impl.SessionImpl.fireLock(SessionImpl.java:611) at org.hibernate.impl.SessionImpl.lock(SessionImpl.java:603) ... Caused by: com.microsoft.sqlserver.jdbc.SQLServerException: Lock request time out period exceeded. at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDatabaseError(SQLServerException.java:196) at com.microsoft.sqlserver.jdbc.SQLServerResultSet$FetchBuffer.nextRow(SQLServerResultSet.java:4700) at com.microsoft.sqlserver.jdbc.SQLServerResultSet.fetchBufferNext(SQLServerResultSet.java:1683) at com.microsoft.sqlserver.jdbc.SQLServerResultSet.next(SQLServerResultSet.java:956) at com.p6spy.engine.spy.P6ResultSet.next(P6ResultSet.java:156) at com.p6spy.engine.logging.P6LogResultSet.next(P6LogResultSet.java:124) at com.mchange.v2.c3p0.impl.NewProxyResultSet.next(NewProxyResultSet.java:2859) at org.hibernate.dialect.lock.SelectLockingStrategy.lock(SelectLockingStrategy.java:97) ... 24 more

    Hibernate JIRA | 8 years ago | Guenther Demetz
    org.hibernate.exception.SQLGrammarException: could not lock: [persistent.jpa.AssociationsClassPeer#1]
  2. 0

    The method SQLServerDialect#appendLockHint currently ignores the LockMode.UPGRADE_NOWAIT returning the same string as for LockMode.UPGRADE. public String appendLockHint(LockMode mode, String tableName) { if ( mode.greaterThan( LockMode.READ ) ) { // does this need holdlock also? : return tableName + " with (updlock, rowlock, holdlock)"; return tableName + " with (updlock, rowlock)"; } else { return tableName; } } As SQLServer supports the nowait option I propose the following improvement: public String appendLockHint(LockMode mode, String tableName) { if ( mode == LockMode.UPGRADE_NOWAIT) { return tableName + " with (updlock, rowlock, nowait)"; } else if ( mode.greaterThan( LockMode.READ ) ) { // does this need holdlock also? : return tableName + " with (updlock, rowlock, holdlock)"; return tableName + " with (updlock, rowlock)"; } else { return tableName; } } A test gave me a correct behaviour: if the concerning row is already locked, then after Lock request time out period exceeded following exception is thrown: org.hibernate.exception.SQLGrammarException: could not lock: [persistent.jpa.AssociationsClassPeer#1] at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:90) at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:66) at org.hibernate.dialect.lock.SelectLockingStrategy.lock(SelectLockingStrategy.java:115) at org.hibernate.persister.entity.AbstractEntityPersister.lock(AbstractEntityPersister.java:1361) at org.hibernate.event.def.AbstractLockUpgradeEventListener.upgradeLock(AbstractLockUpgradeEventListener.java:108) at org.hibernate.event.def.DefaultLockEventListener.onLock(DefaultLockEventListener.java:87) at org.hibernate.impl.SessionImpl.fireLock(SessionImpl.java:611) at org.hibernate.impl.SessionImpl.lock(SessionImpl.java:603) ... Caused by: com.microsoft.sqlserver.jdbc.SQLServerException: Lock request time out period exceeded. at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDatabaseError(SQLServerException.java:196) at com.microsoft.sqlserver.jdbc.SQLServerResultSet$FetchBuffer.nextRow(SQLServerResultSet.java:4700) at com.microsoft.sqlserver.jdbc.SQLServerResultSet.fetchBufferNext(SQLServerResultSet.java:1683) at com.microsoft.sqlserver.jdbc.SQLServerResultSet.next(SQLServerResultSet.java:956) at com.p6spy.engine.spy.P6ResultSet.next(P6ResultSet.java:156) at com.p6spy.engine.logging.P6LogResultSet.next(P6LogResultSet.java:124) at com.mchange.v2.c3p0.impl.NewProxyResultSet.next(NewProxyResultSet.java:2859) at org.hibernate.dialect.lock.SelectLockingStrategy.lock(SelectLockingStrategy.java:97) ... 24 more

    Hibernate JIRA | 8 years ago | Guenther Demetz
    org.hibernate.exception.SQLGrammarException: could not lock: [persistent.jpa.AssociationsClassPeer#1]

    Root Cause Analysis

    1. org.hibernate.exception.SQLGrammarException

      could not lock: [persistent.jpa.AssociationsClassPeer#1]

      at org.hibernate.exception.SQLStateConverter.convert()
    2. Hibernate
      AbstractEntityPersister.lock
      1. org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:90)
      2. org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:66)
      3. org.hibernate.dialect.lock.SelectLockingStrategy.lock(SelectLockingStrategy.java:115)
      4. org.hibernate.persister.entity.AbstractEntityPersister.lock(AbstractEntityPersister.java:1361)
      4 frames
    3. Hibernate
      SessionImpl.lock
      1. org.hibernate.event.def.AbstractLockUpgradeEventListener.upgradeLock(AbstractLockUpgradeEventListener.java:108)
      2. org.hibernate.event.def.DefaultLockEventListener.onLock(DefaultLockEventListener.java:87)
      3. org.hibernate.impl.SessionImpl.fireLock(SessionImpl.java:611)
      4. org.hibernate.impl.SessionImpl.lock(SessionImpl.java:603)
      4 frames