For some reason the EntityManager isn't being injected into a separate thread that I want to keep running in the background. Other classes can access the entity manager without a problem, but those aren't running in a separate thread.
What am I doing wrong here?
@Stateful
public class DatabaseManager extends Thread{
@PersistenceContext(unitName="imas-unit")
private EntityManager em;
private int timeBetweenRefresh;
public void run(){
loadProperties();
retrieveDBContent();
long timerTicks = System.currentTimeMillis();
while(running){
if(System.currentTimeMillis() > timerTicks + timeBetweenRefresh){
timerTicks = System.currentTimeMillis();
deleteAllRecords();
retrieveDBContent();
}
Thread.sleep(1000);
}
}
private void deleteAllRecords(){
//Deletes all the records
}
private void loadProperties(){
//Loads properties for methods
}
private void retrieveDBContent(){
List<Cel> celList = methodThatGetsCells();
System.out.println("Saving to database");
for(Cel cell : celList){
try{
em.persist(cell); //Null pointer here
em.flush();
}
catch(Exception e){
System.out.println(e.getMessage());
e.printStackTrace();
}
}
} catch (InterruptedException e1) {
// TODO Auto-generated catch block
System.out.println("Retrieval failed. Please check error log for further information.");
e1.printStackTrace();
}
}
}
I can post more information if needed.