Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7934081
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T21:32:01+00:00 2026-06-03T21:32:01+00:00

I have the following code: JSF Managed Bean: @ManagedBean(name = purchaseView) @ViewScoped public class

  • 0

I have the following code:

JSF Managed Bean:

@ManagedBean(name = "purchaseView")
@ViewScoped
public class PurchaseView implements Serializable {

 @EJB
 private PurchaseService service;
 private Order order;

 // Getter/Setters here

 public void checkoutOrder() {
   // .. some checks for null here, then call service
   service.checkout(order);
 }
}

Service:

@Stateless
public class BuyVoucherService {

 @EJB
 private OrderBean orderBean;
 @EJB
 private ProductBean productBean;

 public boolean checkout(Order order) {
  orderBean.create(order);
  for(int i=0;i<order.getQuantity();i++) {
   Product product = new Product();
   if(someCondition) {
     // don't save anything and
     return false;
   }
   // .. some setter here
   product.setOrder(order);
   productBean.create(product);
  }
  return true;
 }

The productBean and orderBean are simple JPA EJB with the EntityManager and CRUD operation (Generated by Netbeans..).
In the Service above, things are persisted in the database when the Service returns. In the case something is wrong (someCondition == TRUE above), if I return false the orderBean.save(order) will still persist the order in the database, and I don’t want that.

Is throwing an EJBException and catching it in the ManagedBean the best option?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T21:32:02+00:00Added an answer on June 3, 2026 at 9:32 pm

    As you haven’t specified any transaction attribute explicitly, it will be most probably Required, but depends on the server. Therefore both these methods will be within same transaction, so rolling back in a method will cascade the changes in another.

    You can also try using Mandatory attribute for the 2nd method, it will ensure that it requires a transaction to proceed further, else will cause runtime exception.

        @Resource
        private EJBContext context;
    
        try{
    
          if(someCondition) {
              throw SomeBusinessException("Failed, rolling back");
        } 
    
        }catch(Exception e){
           log(e.getMessage, e) 
           context.setRollbackOnly();
        }
    

    Else, you can throw system exception, that will force the container to rollback the changes being made.

        if(someCondition)
            throw SomeBusinessException("Failed, rolling back");
    
        }catch(Exception e){
            throw new EJBException (e.getMessage(), e);
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a JSF 2.0 bean: @ManagedBean @SessionScoped public class LoginBean implements Serializable {
I have following code class User attr_accessor :name end u = User.new u.name =
I'm using Glassfish 3.1.1 and JSF 2.0: I have the following code: public String
I have following line of code for jsp tag <input name=UnicodeVirtualKey class=btn type=button value=&#x0A85;
I have following code: <div class='parent'> <div class='left-child'></div> <div class=right-child></div> </div> What I want
I Have following code: Controller: public ActionResult Step1() { return View(); } [AcceptVerbs(HttpVerbs.Post)] public
I have the following code in JSF-2 Mojarra 2.0.8 running on Tomcat 7 <h:panelGrid
Im using websphere server and JSF 2.0 In my managed bean i have declared
I have the following JSF code (stripped down for the sake of sanity): <!DOCTYPE
I have the following code inside a method invoked by a jsf <h:commandButton>. It

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.