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 6629021
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T22:12:04+00:00 2026-05-25T22:12:04+00:00

Just a background since I am used to using JDBC since I worked on

  • 0

Just a background since I am used to using JDBC since I worked on an old project.
When saving into database, I always set the value to -1 for any unsuccessful insert

public class StudentDao{
    //This dao method returns -1 if unsuccessful in saving
    public int save(Student stud){
        Statement st = con.createStatement();
        try{
            int val = st.executeUpdate("INSERT student VALUES(.......)");
        }catch(Exception e){
            return -1;
        }
    }
}

Based on the return I could could tell if the insert is successful so that I could do the exact logic.
(Tell the user that the transaction is incomplete…)

Now, I used EJB in persisting entity. Most of the tutorials that I am seeing only have this construct.
Netbeans is generating this code also with a ‘void’ return.

@Stateless
public class StudentFacade{
    @PersistenceContext(unitName = "MyDBPU")
    private EntityManager em;

    public void save(Student student){
        em.persist(student);
    }
}

When saving entity on a servlet, it just call the method like this code.

@WebServlet(name = "StudentServlet",
loadOnStartup = 1,
urlPatterns = {
    "/addStudent",})
public class StudentServlet extends HttpServlet {
    @EJB
    private StudentFacade studentFacade;

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        //extract HTTP form request parameters then set 
        Student stud = Util.getStudent(request);

        studentFacade.save(stud);

    }
}

But how will I know if the insert is successful? (Dont catch the exception and then just let it propagate.
I have configured my error page so obviously this would catch the error???)

Sorry I am getting confused on integrating my EJB components but I am seeing its benefits.
I just need some advise on some items. Thanks.

  • 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-05-25T22:12:04+00:00Added an answer on May 25, 2026 at 10:12 pm

    The container will propagate the exception to the caller (if you don’t do anything with it inside the EJB). That would be probably the SQLException I guess. You can catch it on the servlet and do whatever you want with it. If you use Container Managed Transactions (CMT) the transaction will be rolled back for you automatically by the container and the student object won’t be added. As you said, you can of course leave the exception on the web layer as well and then prepare a special error page for it. All depends on your usage scenario.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Just some background, sorry so long winded. I'm using the System.Data.SQLite ADO.net adapter to
I come from a PHP background and I'm just getting my teeth into some
Does anyone know how to set the background colour of just a section of
I tried something like this but it just makes the background of the image
Just started coding in AS3 with FlashDevelop and coming from a C# background, I
NOTE : I mention the next couple of paragraphs as background. If you just
This is pretty simple, I come from a swing/awt background. I'm just wondering what
A little background: I'm using Spring and Hibernate to create a really simple domain/dao/service
Background: Filemaker Pro 10+ have gained functionality to send emails using SendMail via SMTP.
I'm just getting into CSS3 transitions (about time!) and I witness some strange behavior

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.