Java Reference
In-Depth Information
@Lock(LockType.WRITE)— Place a write lock on the bean when the method
is invoked so that only the current thread gets access.
If you don't specify either, @Lock(LockType.WRITE) is essentially assumed. These
annotations may be placed on methods either in the business interface or on the bean itself.
The XML deployment file for the bean can override the annotations placed on the code,
although it's probably not a good idea to override concurrency handling at deployment.
Because a singleton method call potentially locks out callers, a mechanism is provided
to time out the callers so that they don't block forever. If the time-out is exceeded, a
javax.ejb.ConcurrentAccessTimeoutException is thrown. This is a runtime
exception, meaning that a caller doesn't have to catch it. The @AccessTimeout annota-
tion is used to specify lock time-outs. The annotation is defined as follows:
@Target(value = {ElementType.METHOD, ElementType.TYPE})
@Retention(value = RetentionPolicy.RUNTIME)
public @interface AccessTimeout {
public long value();
public TimeUnit unit() default TimeUnit.MILLISECONDS;
}
As you can see from this definition, you specify the time duration (value) and a time unit.
The time unit defaults to milliseconds (1 second = 1,000 milliseconds). The available time
units are these:
• Nanoseconds
• Microseconds
• Milliseconds
• Seconds
• Minutes
• Hours
• Days
Most applications will probably use either milliseconds, seconds, or, at the upper extreme,
minutes. The timer on the computer system, especially Windows, may not be able to re-
solve nanoseconds, and hours and days are a little extreme. Let's update your business in-
terface for the DefaultFeaturedItem to enable concurrent access and time-out after
one minute:
public interface FeaturedItem {
@Lock(LockType.READ)
Search WWH ::




Custom Search