3 Replies Latest reply on Feb 4, 2005 8:22 AM by tpaterson

    Optimistic Locking using Modified Strategy JBoss 3.2.3

    jeeads

      I am using 3.2.3 and I am configuring optimistic locking in the following manner:

      jboss.xml

      
      <entity>
       <ejb-name>MSSQLServer2000Sequencher_01REF_Users</ejb-name>
       <local-jndi-name>MSSQLServer2000Sequencher_01REF_Users</local-jndi-name>
       <configuration-name>Non-nullable foreignKey ejbPostCreate and Optimistic Container</configuration-name>
      <entity>
      
      <container-configurations>
      <!-- <Extends Per Transaction for the purpose of inserting record after post create> -->
       <container-configuration extends="Instance Per Transaction CMP 2.x EntityBean">
       <container-name>Non-nullable foreignKey ejbPostCreate Container</container-name>
       <insert-after-ejb-post-create>true</insert-after-ejb-post-create>
       </container-configuration>
      
      <!-- <Extends Per Transaction for the purpose of Optimistic Locking and inserting record after post create> -->
       <container-configuration extends="Instance Per Transaction CMP 2.x EntityBean">
       <container-name>Non-nullable foreignKey ejbPostCreate and Optimistic Container</container-name>
       <insert-after-ejb-post-create>true</insert-after-ejb-post-create>
       <!-- <overrides: org.jboss.ejb.plugins.lock.noLock> -->
       <locking-policy>org.jboss.ejb.plugins.lock.JDBCOptimisticLock</locking-policy>
       </container-configuration>
      
      <!-- <Extends Per Transaction for the purpose of Optimistic Locking JE 8/18/2003> -->
       <container-configuration extends="Instance Per Transaction CMP 2.x EntityBean">
       <container-name>Optimistic Container</container-name>
       <!-- <overrides: org.jboss.ejb.plugins.lock.noLock> -->
       <locking-policy>org.jboss.ejb.plugins.lock.JDBCOptimisticLock</locking-policy>
       </container-configuration>
      </container-configurations>






      jbosscmp-jdbc.xml


      <defaults>
       <datasource>java:/MSSQLServer2000Sequencher_01REF</datasource>
       <datasource-mapping>MS SQLSERVER2000</datasource-mapping>
       <pk-constraint>true</pk-constraint>
       <fk-constraint>false</fk-constraint>
       <preferred-relation-mapping>foreign-key</preferred-relation-mapping>
       <read-ahead>
       <strategy>on-load</strategy>
       <page-size>1000</page-size>
       <eager-load-group>*</eager-load-group>
       </read-ahead>
       <list-cache-max>1000</list-cache-max>
      </defaults>
      <enterprise-beans>
       <entity>
       <ejb-name>MSSQLServer2000Sequencher_01REF_Users</ejb-name>
       <create-table>false</create-table>
       <remove-table>false</remove-table>
       <table-name>Users</table-name>
       <cmp-field>
       <field-name>user</field-name>
       <column-name>UserID</column-name>
       </cmp-field>
       <cmp-field>
       <field-name>employeeId</field-name>
       <column-name>EmployeeID</column-name>
       </cmp-field>
       <cmp-field>
       <field-name>firstName</field-name>
       <column-name>FirstName</column-name>
       </cmp-field>
       <cmp-field>
       <field-name>lastName</field-name>
       <column-name>LastName</column-name>
       </cmp-field>
       <cmp-field>
       <field-name>initials</field-name>
       <column-name>Initials</column-name>
       </cmp-field>
       <cmp-field>
       <field-name>eMail</field-name>
       <column-name>EMail</column-name>
       </cmp-field>
       <cmp-field>
       <field-name>locked</field-name>
       <column-name>Locked</column-name>
       </cmp-field>
       <cmp-field>
       <field-name>active</field-name>
       <column-name>Active</column-name>
       </cmp-field>
       <cmp-field>
       <field-name>changePasswordOnNextLogin</field-name>
       <column-name>ChangePasswordOnNextLogin</column-name>
       </cmp-field>
       <cmp-field>
       <field-name>dateCreated</field-name>
       <column-name>DateCreated</column-name>
       </cmp-field>
       <cmp-field>
       <field-name>createdBy</field-name>
       <column-name>CreatedBy</column-name>
       </cmp-field>
       <cmp-field>
       <field-name>dateModified</field-name>
       <column-name>DateModified</column-name>
       </cmp-field>
       <cmp-field>
       <field-name>modifiedBy</field-name>
       <column-name>ModifiedBy</column-name>
       </cmp-field>
       <optimistic-locking>
       <modified-strategy/>
       </optimistic-locking>
       </entity>
      </enterprise-beans>
      


      When two users open the same users record and do updates on different fields they wind up with a lost update. The first update is lost and the second update does not throw an exception as it should with the modified strategy. What have I setup wrong? Is it true that after 3.2.2 that org.jboss.ejb.plugins.lock.JDBCOptimisticLock is deprecated? If so how should the container configuration look?

      Thanks
      Jerry

        • 1. Re: Optimistic Locking using Modified Strategy JBoss 3.2.3
          ppetrou

          Hi,

          I am not an expert in the area but I had the same problem a few
          days ago. Your XML configuration seems ok to me.

          What you need to make sure for optimistic locking to work
          is to include the read of the record, the user edit time, and the update
          of the record into one single transaction.

          The only way I found to do this is with a client initiated transaction.

          You can have a look in my posting below for code snippets.
          http://www.jboss.org/index.html?module=bb&op=viewtopic&t=57794

          Petros

          • 2. Re: Optimistic Locking using Modified Strategy JBoss 3.2.3
            jeeads

            Thanks,
            I had looked at your post and you are right the transaction must be atomized for JBoss optimistic locking to work. My application writers are adverse to handling their own transactions. I am going to have to declaratively setup which field in a table is to be checked for concurrency so I can roll my own optimistic locking for the clients. I will use the JBoss mechanism for middle tier transactions and use my own for remote invokations.

            Jerry

            • 3. Re: Optimistic Locking using Modified Strategy JBoss 3.2.3

              Just a ME-TOO

              We do exactly the same -- use JBoss/J2EE transactions within the middle-tier, but also have a lockVersion field in the VO sent to the client -- so when the client hits save -- we lookup the entity - compare the lockVersion with that in the VO and if they don't match we throw a LockException..