How do the timeouts work with EJB3 Stateful Beans

EJB3 Timeouts with Stateful Session Beans

 

When using stateful session beans, you can control the timeouts for.

 

  1. How long the bean(with the state) remains in memory.  State can be passivated to disk(or cache) to free up memory for other beans or processes.

  2. How long the bean remains active before it is completely removed.  This controls the life span of the SFSB.

 

There are two timeouts that control this:

 

  1. idleTimeoutSeconds - the number of seconds that a SFSB remains active but unused until the state is passivated.

  2. removalTimeoutSeconds - this represents the end of the beans life, the bean and state are deleted when this timeout is hit but the bean was unused. 0 represents infinity. When removalTimeoutSeconds <= idleTimeoutSecond, then the bean is not passivated, it is just straightforwardly removed.

 

You can change this in two places. 

 

1.  You can change the timeouts per bean by defining a class annotation.

 

For a clustered configuration specify a simple CacheConfig with your specific timeouts.  Remember that 0 is infinite.

 

If you are NOT in a clustered app server..

 

@CacheConfig (maxSize=100000, idleTimeoutSeconds=300, removalTimeoutSeconds=0)

 

If you are IN a clustered app server..

 

@CacheConfig (name="jboss.cache:service=EJB3SFSBClusteredCache", maxSize=100000, idleTimeoutSeconds=300, removalTimeoutSeconds=0)

 

 

2.  You can change these globally if you go to the deploy/ejb3-interceptors-aop.xml file.

You will see the following defined for clustered and non clustered SFSBs.  Just edit the file and replace the timouts that you would like.  Make sure that you change the correct one if you are clustered or non-clustered.  You may want to change both for constancy.

 

 

   <annotation expr="!class(@org.jboss.annotation.ejb.cache.simple.CacheConfig) AND !class(@org.jboss.annotation.ejb.Clustered)">
         @org.jboss.annotation.ejb.cache.simple.CacheConfig (maxSize=100000, idleTimeoutSeconds=300, removalTimeoutSeconds=0)
      </annotation>

      <!-- Clustered cache configuration -->
      <annotation expr="!class(@org.jboss.annotation.ejb.cache.Cache) AND class(@org.jboss.annotation.ejb.Clustered)">
         @org.jboss.annotation.ejb.cache.Cache (org.jboss.ejb3.cache.tree.StatefulTreeCache.class)
      </annotation>
      <annotation expr="!class(@org.jboss.annotation.ejb.cache.tree.CacheConfig) AND class(@org.jboss.annotation.ejb.Clustered)">
         @org.jboss.annotation.ejb.cache.tree.CacheConfig (name="jboss.cache:service=EJB3SFSBClusteredCache", maxSize=100000, idleTimeoutSeconds=300, removalTimeoutSeconds=0)
      </annotation>

 

 

Warning:Be careful what you set the removalTimeoutSeconds to.  If you are in the middle of a session and bean is not touched, it's possible that you could end up in a condition where you are depending on the state, you haven't touched it in the given timeout and the state is suddenly gone.  You will get the following errror...

 

javax.ejb.EJBNoSuchObjectException: Could not find Stateful bean: 41z4l26-wb41k6-ev4j007b-1-ev4jirql-bw

 

ref:

(1.) timout setting for seam.  JbossTimeoutSettingForSeam