How can I pessimistically lock an Entity in JBoss and Hibernate?

Solution Verified - Updated -

Issue

  • I am trying to find a way to synchronize access to some POJO entities in a EAP 5.1 environment.
  • I need is to have a transaction hold a lock on and entity and release the lock at the end while other transactions "wait" for the lock to become available.
  • When using em.lock(Object, LockModeType.WRITE) and another transaction is trying to put a lock on an already locked object, I'm receiving the following error:

    Caused by: org.hibernate.exception.LockAcquisitionException: could not lock: [com.company.entity.Order#39578404]
    at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:110)
    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:1352)
    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:613)
    at org.hibernate.impl.SessionImpl.lock(SessionImpl.java:605)
          at org.hibernate.ejb.AbstractEntityManagerImpl.lock(AbstractEntityManagerImpl.java:383)
          ... 135 more
     Caused by: java.sql.SQLException: ORA-00054: resource busy and acquire with NOWAIT specified
          at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:112)
          at oracle.jdbc.driver.T4CTTIoer.processError(T4CTTIoer.java:331)
          at oracle.jdbc.driver.T4CTTIoer.processError(T4CTTIoer.java:288)
          at oracle.jdbc.driver.T4C8Oall.receive(T4C8Oall.java:745)
          at oracle.jdbc.driver.T4CPreparedStatement.doOall8(T4CPreparedStatement.java:219)
          at oracle.jdbc.driver.T4CPreparedStatement.executeForDescribe(T4CPreparedStatement.java:813)
          at oracle.jdbc.driver.OracleStatement.executeMaybeDescribe(OracleStatement.java:1049)
          at oracle.jdbc.driver.T4CPreparedStatement.executeMaybeDescribe(T4CPreparedStatement.java:854)
          at oracle.jdbc.driver.OracleStatement.doExecuteWithTimeout(OracleStatement.java:1154)
          at oracle.jdbc.driver.OraclePreparedStatement.executeInternal(OraclePreparedStatement.java:3370)
          at oracle.jdbc.driver.OraclePreparedStatement.executeQuery(OraclePreparedStatement.java:3415)
          at org.jboss.resource.adapter.jdbc.WrappedPreparedStatement.executeQuery(WrappedPreparedStatement.java:342)
          at org.hibernate.dialect.lock.SelectLockingStrategy.lock(SelectLockingStrategy.java:95)
    

Environment

  • JBoss Enterprise Application Platform (EAP) 5.1.0

Subscriber exclusive content

A Red Hat subscription provides unlimited access to our knowledgebase, tools, and much more.

Current Customers and Partners

Log in for full access

Log In

New to Red Hat?

Learn more about Red Hat subscriptions

Using a Red Hat product through a public cloud?

How to access this content