I have a DBServiceBean that is responsible for most of my DB access functions. I am doing this in order to make it easy to keep track of how many times various updates are being called and how long they take. I had the code working with DataManager but wanted to test EntityManager as I have many commits which would seem to benefit from being in managed transactions.
I call the DBServiceBean from both UIs and other Services and usually return the results of a database query. It works well for retrieving all of the local variables and increases the speed of the program ~25%. However, I am having a really weird problem with retrieving associated entities using views. Below is the DBServiceBean code that is behaving weird:
public List<ActShips> getFullShipList (){
List<ActShips> ships = new ArrayList<>();
try (Transaction txShipList = persistence.createTransaction()) {
// get EntityManager for the current transaction
EntityManager em = persistence.getEntityManager();
// create and execute Query
View view = new View(ActShips.class, "ships-view");
Query query = em.createQuery(
"select e from fleetassessment$ActShips e")
.setView(view);
ships = query.getResultList();
stopClock();
return ships;
}
}
When I debug, this code returns the exact result I want, including two sets of attached entities (EventList and RequirementsList) which are included in ‘ships-view’. The problem occurs in the data received back at the callling routine. In another service bean “SimServiceBean”, I call the function with the following code:
List<ActShips> ships = dbService.getFullShipList();
The return value from getFullShipList includes the local view of ActShips consistently but only sporadically gets the EventList and RequirementsList. When it works, all of the downstream code works great. When it fails, those fields show “Unable to evaluate the expression Method threw ‘java.lang.IllegalStateException’ exception.” and the code throws an exception the first time EventList or RequirementsList is called. When I manually step through the code in the IDE, it works more frequently and when I don’t put stop points, it never works. Sometimes 2 or 3 of the ships will include the attached entities and the other ships will have errors.
In a related question, is it possible to share a transaction between two services? I think I would like to keep the transactions managed as they are manipulated rather than have them detach and then merged.