Note that there are some explanatory texts on larger screens.

plurals
  1. POHow does Java EE container control transactions?
    primarykey
    data
    text
    <p>I have a question about how transactions are controlled by an EE container. This is pseudo code to give some context to my question. This is not how I code, so please stay on the question and do not evolve the topic into other stuff. </p> <p>Consider the following two services and related controller. Both services have injected EntityManager and both have methods that need to run within a transaction. One service has a method that does not need any transaction support. </p> <pre><code>@Stateless class UserService { @PersistenceContext private EntityManager em; public void saveUser(User user) { em.merge(user); } public String getFullName(User user) { return user.getFirstName() + " " + user.getLastName(); } } @Stateless class LogService { @PersistenceContext private EntityManager em; public void logEvent(String eventText) { Event event=new Event(); event.setText(eventText); event.setTime(new Date()); em.persist(event); } } @Named class UserController { User user; @Inject UserService userService; @Inject LogService logService; public void updateUser(user) { // button posts to this method String fullName=userService.getFullName(user); // 1 if(fullName.startsWith("X")) return; // 2 userService.saveUser(user); // 3 logService.logEvent("Saved user " + fullName); // 4 } } </code></pre> <p>Now, imagine there's a button that posts a form to userController.updateUser. </p> <p>My assumption is that the <code>UserController.updateUser()</code> will execute <code>userService.saveUser(user);</code> and <code>logService.logEvent("Saved user " + fullName);</code> within the same transaction. So if the call to <code>logService.logEvent()</code> fails with an SQL Exception, the user entity will not be updated. Also, my assumption is that call to <code>userService.getFullName(user)</code> is not run within any transaction and if we prematurely exit the method when user's name starts with an X, then no transaction is created. But clearly, these are just guesses. </p> <p>Can someone explain what the Java EE container will do to support <code>UserController.updateUser()</code> method with transaction and what actually triggers the transaction? Also, any further reading you can point me to, would be much appreciated. I've seen some material online but still I'm missing something here and didn't get any help asking around at work, either. So I'm certainly not the only one who's got a gap on this.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload