尝试使用@CacheConfig停用有状态会话bean的钝化

时间:2012-04-19 22:21:08

标签: java-ee-6 jboss6.x jboss-weld

我尝试使用

停用有状态会话bean的钝化
  

@CacheConfig(maxSize = 0,idleTimeoutSeconds = 0)

http://docs.jboss.org/ejb3/docs/reference/1.0.7/html/SessionBean_and_MDB_configuration.html

我向我的有状态会话bean和它注入的托管bean添加了几个print语句,以便更好地了解正在发生的事情。看来有状态bean确实注入了托管bean(因为它不是null),但是调用它的方法没有效果(如果你看下面托管bean的loadData()方法,它会调用test()例程)有状态会话bean,但输出“test”永远不会出现。)

  

15:06:07,861 INFO [STDOUT] loadData programSlug = some-program-name

     

15:06:07,876 INFO [STDOUT] programServiceBean =端点的无接口视图[jboss.j2ee:jar = TEI.war,name = ProgramServiceBean,service = EJB3]和session 43h1h2v-j35uon-h18czgvf-1-h18d0nzj -df

     

15:06:07,908 ERROR [org.apache.catalina.core.ContainerBase。[jboss.web]。[localhost]。[/ TEI]。[Faces Servlet]] servlet的Servlet.service()Faces Servlet抛出异常:java.lang.IllegalStateException:    无法找到成员[com.ray.TEI.model.Program#urlSlug]           在org.hibernate.ejb.metamodel.AbstractAttribute.readObject(AbstractAttribute.java:122)[:3.6.6.Final]           at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)[:1.6.0_30]           at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)[:1.6.0_30]

这是我的有状态会话bean ...

package com.ray.TEI.ejb;

import com.ray.TEI.model.Program;

import javax.ejb.Stateful;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.PersistenceContextType;

import org.jboss.ejb3.annotation.CacheConfig;

@Stateful
@CacheConfig(maxSize = 0, idleTimeoutSeconds = 0)
public class ProgramServiceBean {
  @PersistenceContext(unitName="TEI", type=PersistenceContextType.EXTENDED)
  protected EntityManager em;

  public void test() {
    System.out.println("test");
  }

  public Program findBySlug(String urlSlug) {
    System.out.println("findBySlug " + urlSlug);
    return em.createNamedQuery("Program.findBySlug", Program.class)
        .setParameter("urlSlug", urlSlug)
        .getSingleResult();
  }
}

这是托管bean ...

package com.ray.TEI.controller;

import javax.enterprise.context.RequestScoped;
import javax.inject.Inject;
import javax.inject.Named;

import com.ocpsoft.pretty.faces.annotation.URLAction;
import com.ocpsoft.pretty.faces.annotation.URLMapping;
import com.ocpsoft.pretty.faces.annotation.URLQueryParameter;
import com.ray.TEI.ejb.ProgramServiceBean;
import com.ray.TEI.model.Program;

@Named
@RequestScoped
@URLMapping(
  id="uuts",
  pattern="/#{programSlug:uutsController.programSlug}/uuts",
  viewId="/uuts.xhtml"
)
public class UutsController {
  @Inject private ProgramServiceBean programServiceBean;
  private String programSlug;
  private Program program;

  @URLQueryParameter("new")
  private Boolean displayAddForm = false;

  @URLAction
  public String loadData() {
    System.out.println("loadData programSlug = " + programSlug);
    if (programSlug != null) {
      System.out.println("programServiceBean = " + programServiceBean);
      programServiceBean.test();
      program = programServiceBean.findBySlug(programSlug);
      System.out.println("loadData program = " + program.getName());
    }
    if (program == null) {
      return "pretty:error";
    }
    return null;
  }

  public String getProgramSlug() {
    return programSlug;
  }
  public void setProgramSlug(String programSlug) {
    this.programSlug = programSlug;
  }

  //other getters/setters
}

知道问题是什么?

谢谢, 杰森

(顺便说一下,我正在使用JBoss 6.1 Final)

1 个答案:

答案 0 :(得分:2)

通过将bean删除超时设置为小于空闲超时来修复我的问题,如https://community.jboss.org/wiki/Ejb3DisableSfsbPassivation所述(选项2,方法2)

import org.jboss.ejb3.annotation.CacheConfig;

@Stateful
@CacheConfig (maxSize=100000, idleTimeoutSeconds=600, removalTimeoutSeconds=300)
public class ProgramServiceBean {
  //...
}