Looking at listing #1 at the following tutorial,
JAX-RS resource classes can be defined as @Stateless or @Singleton.
I have the following code in my application:
@Stateless
public class VisitDaoImpl implements VisitDao {
@PersistenceContext(name = "MysqlPU")
private EntityManager em;
@Override
public void persist(Visit vist) {
em.persist(vist);
}
}
@ApplicationPath("rest")
public class ApplicationConfig extends Application {
@Override
public Set<Class<?>> getClasses() {
Set<Class<?>> resources = new java.util.HashSet<>();
resources.add(WelcomeResource.class);
return resources;
}
}
//@Singleton
@Stateless
@Path("/Welcome")
public class WelcomeResource {
@EJB
private VisitDao visitDao;
@GET
@Produces(MediaType.TEXT_PLAIN)
public String wellcomeMessage() {
visitDao.persist(new Visit())
return "Welcome";
}
}
As you can see I do not have state in my resource class except for the Dao bean.
My questions are:
- Should I use @Stateless or @Singleton bean here?
- When to favor one of them on the other in JAX-RS resource classes?
Thank you.