I would like to have a static read only collection in a stateless session bean. Ideally it will only be initialized once and available to bean instances as long as the application is running.
Let's assume that this application is deployed in a clustered environment with multiple servers/JVMs.
As I understand it, in the first case, the static variable barList gets initialized at the same time when the bean is created by the container and lives as long as the bean (or maybe even longer?) and there's no danger of it being garbage collected while the bean instance is alive.
In the second case, barList gets initialized when the Foo class gets loaded which is when the bean's getBarList() method gets executed. What happens when it's returned though? Will it be destroyed after the bean method is done executing?
Case 1:
@Stateless
public class MrBean implements BeanInterface{
private static final List<Bar> barList;
static{
barList = new ArrayList<Bar>();
//create some bars, add them to the list
}
public List<Bar> getBarList(){
return barList;
}
}
Case 2:
@Stateless
public class MrBean implements BeanInterface{
public List<Bar> getBarList (){
return Foo.barList;
}
}
public class Foo {
public static final List<Bar> barList;
static{
barList = new ArrayList<Bar>();
// create bars, add them to barList
}
}