-
1. Re: Any plans for built-in optimistic locking for future ver
darryl_staflund Apr 17, 2003 4:06 PM (in response to mboulatian)Hi there,
Optimistic Locking is built into JBoss 3.2 as far as I can tell. I've used it a bit and it works fairly well although it still seems buggy to me in some places. For example, if I have two entity beans A and B such that Entity Bean A contains an aggregation of Entity Bean B's and Entity Bean B references an Entity Bean A (but in such a way that no circles are formed, i.e. the Entity Bean A referenced by Entity Bean B does not itself contain Entity Bean B in its aggregation of Entity Bean B's) then the counts involved in the locking get out of synch.
So optimistic locking is there but it's a bit buggy -- or at least I think it is in some cases because I can find no docs to check my problem against :-)
Darryl
PS: I will attach a code sample in a subsequent post to show you how I implemented it. -
2. Re: Any plans for built-in optimistic locking for future ver
darryl_staflund Apr 17, 2003 4:28 PM (in response to mboulatian)Hi again,
Okay, attached is a zip file that demonstrates how I implemented optimistic locking in my code. There are four files:
security.sql
========
This is the PostgreSQL database schema of a table named 'group'. It defines a field called 'versionCount' that JBoss will use to implement optimistic locking via versioning.
jbosscmp_jdbc.xml
=============
This contains the CMP definition for the 'group' bean. It shows how to mapping the optimistic locking field to the database.
jboss.xml
=======
This defines a new container configuration that uses optimistic locking and also specifies that my 'group' bean should use it. I created this file a long time before 3.2 went gold so JBoss 3.2's standard-jboss.xml file might contain optimistic locking container configurations. I haven't checked.
TALEntityBean
==========
This just shows that I defined the versionCount fields in my entity bean as I do other CMP fields even though the versionCount field is defined differently in the jbosscmp-jdbc.xml file.
Hope this helps. Locking works for the most part except in the situation I mentioned in my previous post. Having said this. Without actually seeing any official JBoss documentation on optimistic locking (which, I hope, will be soon), I could be totally out to left field :-)
Good luck.
Darryl
Hmmmm.....it doesn't look like it's gonna let me attach my files. I'll paste them below. Sorry for the long post...
/* *"******************************************************************** */
-- table: securitygroup
create table security.securitygroup
(
grpid int4 primary key,
name varchar (60) unique,
description varchar (255),
-- Meta-data.
creationDate timestamp not null default current_timestamp,
modificationDate timestamp not null default current_timestamp,
createdBy text not null default current_user,
modifiedBy text not null default current_user,
versionCount int4 not null default 1
)
with oids;
comment on table security.securitygroup is 'table used to store information on security groups created by the user.';
<!-- SecurityGroup -->
<ejb-name>SecurityGroupEJB</ejb-name>
<table-name>security.view_securitygroup</table-name>
<cmp-field>
<field-name>groupId</field-name>
<column-name>grpId</column-name>
<not-null/>
</cmp-field>
<cmp-field>
<field-name>name</field-name>
<column-name>name</column-name>
<not-null/>
</cmp-field>
<cmp-field>
<field-name>description</field-name>
<column-name>description</column-name>
</cmp-field>
<cmp-field>
<field-name>creationDate</field-name>
<column-name>creationDate</column-name>
</cmp-field>
<cmp-field>
<field-name>modificationDate</field-name>
<column-name>modificationDate</column-name>
</cmp-field>
<cmp-field>
<field-name>createdBy</field-name>
<column-name>createdBy</column-name>
</cmp-field>
<cmp-field>
<field-name>modifiedBy</field-name>
<column-name>modifiedBy</column-name>
</cmp-field>
<optimistic-locking>
<version-column/>
<field-name>versionCount</field-name>
<column-name>versionCount</column-name>
<jdbc-type>INTEGER</jdbc-type>
<sql-type>INT4</sql-type>
</optimistic-locking>
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE jboss PUBLIC
"-//JBoss//DTD JBOSS 3.2//EN"
"http://www.jboss.org/j2ee/dtd/jboss_3_2.dtd">
<enterprise-beans>
<ejb-name>SecurityGroupEJB</ejb-name>
<configuration-name>TalAdmin CMP 2.x EntityBean</configuration-name>
</enterprise-beans>
<container-configurations>
<!-- CMP 2.x EntityBean -->
<container-configuration extends="Standard CMP 2.x EntityBean">
<container-name>TalAdmin CMP 2.x EntityBean</container-name>
<locking-policy>org.jboss.ejb.plugins.lock.JDBCOptimisticLock</locking-policy>
<commit-option>A</commit-option>
</container-configuration>
</container-configurations>
package ca.talonline.business.ejb;
// Sun Java classes.
import java.io.*;
import java.util.*;
import javax.ejb.*;
import javax.naming.*;
/**
* Implements the methods used by most entity beans used by TAL Online. Methods
* include lifecycle methods, etc. Also implements the meta-data methods shared by
* all entity beans, such as version counts (for optimistic locking), timestamps
* (for auditing), etc.
*
* @author Darryl A. J. Staflund
*/
public abstract class TALEntityBean extends TALEnterpriseBean implements EntityBean
{
/**
* This entity bean's EntityContext.
*/
protected EntityContext entityContext;
/**
* Lifecycle method called by the EJB container when the entity bean is activated.
*/
public void ejbActivate ()
{
}
/**
* Lifecycle method called by the EJB container when the entity bean is loaded.
*/
public void ejbLoad ()
{
}
/**
* Lifecycle method called by the EJB container when the entity bean is passivated.
*/
public void ejbPassivate ()
{
}
/**
* Lifecycle method called by the EJB container when the entity bean is removed.
*/
public void ejbRemove ()
{
}
/**
* Lifecycle method called by the EJB container when the entity bean is stored.
*/
public void ejbStore ()
{
}
/**
* Called by the EJB container in order to pass the entity context to this entity bean.
*
* @param ctx The entity context of this entity bean.
*/
public void setEntityContext (EntityContext ctx)
{
this.entityContext = ctx;
}
/**
* Called by the EJB container in order to unset the entity context before the bean is removed.
*/
public void unsetEntityContext ()
{
this.entityContext = null;
}
/**
* Called when the entity bean wants the instance of the entity context.
*
* @return EntityContext The entity context of this entity bean.
*/
protected EntityContext getEntityContext ()
{
return (this.entityContext);
}
/**
* Increments the number of times this record has been modified. Is used to
* implement optimistic locking by JBoss.
*
* @param versionCount The new version count to assign to this bean.
*/
// public abstract void setVersionCount (Integer versionCount);
/**
* Returns the number of times this record has been modified. It is used to
* implement optimistic locking by JBoss.
*
* @return Integer The version count currently assigned to this bean.
*/
// public abstract Integer getVersionCount ();
/**
* Associates a creation date with the given entity bean.
*
* @param timestamp The creation date to associate with this entity bean.
*/
public abstract void setCreationDate (Date timestamp);
/**
* Returns the creation date of this entity bean.
*
* @return Date The creation date of this entity bean.
*/
public abstract Date getCreationDate ();
/**
* Associates a modification date with the given entity bean.
*
* @param timestamp The modification date to associate with this entity bean.
*/
public abstract void setModificationDate (Date timestamp);
/**
* Returns the modification date of this entity bean.
*
* @return Date The modification date of this entity bean.
*/
public abstract Date getModificationDate ();
/**
* Stores the username of the individual who first created this entity.
*
* @param username The name of the user who first created this entity.
*/
public abstract void setCreatedBy (String username);
/**
* Returns the username of the individual who first created this entity.
*
* @return String The name of the user who first created this entity.
*/
public abstract String getCreatedBy ();
/**
* Stores the username of the individual who last modified this entity.
*
* @param username The name of the user who last modified this entity.
*/
public abstract void setModifiedBy (String username);
/**
* Returns the username of the individual who last modified this entity.
*
* @return String The name of the user who last modified this entity.
*/
public abstract String getModifiedBy ();
}