This may be a silly question, due to missing some understanding of java but I have this code:
@Stateless
@WebService
public class MLHRequesterBean implements MLHRequesterBeanRemote {
private final static String sel = "MLHRequesterPU" + (isProduction()? " " : "-dev");
public static boolean isProduction(){
try {
if (Inet4Address.getLocalHost().getHostName().equalsIgnoreCase("ironman")) {
return true;
}
} catch (UnknownHostException ex) {}
return false;
}
@PersistenceContext(unitName=sel)
...
Why is sel not considered a constant? We have our test server and a production server and each one should write to a different DB. How can I overcome this issue?
This is the error:
C:\projects\workspace\MLHRequester\MLHRequester-ejb\src\java\mlh\MLHRequesterBean.java:33: attribute value must be constant
@PersistenceContext(unitName=sel)
1 error
sel is a final static, but its value is evaluated the first time this class is loaded. The
@annotationsare evaluated at compile time, hence the error.You are better off doing something like a macro/substitution pre-processing step during build to generate the right value (may be base it off a .properties file).