Reputation: 93
I am developing a simple web application in JSP, which has an access to a MySQL database. I am using Hibernate libraries to connect to and use the database. So, my question is: do I need to use Enterprise Java Beans (EJB) for this matter (considering that it's a web site, which probably will be accessed by more than one user simultaneously and all of them would like to use the database at the same time)?
Upvotes: 2
Views: 1301
Reputation: 2053
If your application works under Apache Tomcat, you should use hibernate or pure JPA . I think for simple application you need not JBoss, or need?
If your application run under JBoss or Glassfish you are free to use EJB3 without remote calls. If your backend and frontend separated you can use EJB3 via remote calls.
In general if you can use EJB3/3.1/3.2 - use it!
Upvotes: 0
Reputation: 1782
I would say Yes.
EJBs are very simple to use beans that make working with JPA (Hibernate) much easier.
Without them, you need to manually keep track of transactions, and clean them up in try/catch/finally blocks. You also need to manually create an entity manager factory when your application starts up and then store it somewhere. It's not really difficult, but it's a tedious job that makes your code very verbose.
With EJB, this all goes away. All you need to do is something like this:
@Stateless
public class MyService {
@PersistenceContext
private EntityManager entityManager;
public Customer getCustomer(Integer customerId) {
return entityManager.find(Customer.class, customerId);
}
}
No manually handling transactions, and no code to store and keep the entity manager.
In addition to that, EJBs are also automatically thread-safe and instances are pooled. Which means better performance and automatic throttling that prevents your system from being overloaded.
There are a few more features in EJB, but I've personally found that in the majority of cases something like the above is what you use most.
EJBs don't need any configuration to use (see Minimal 3-tier Java EE app, without any XML config) and they are supported by a lot of servers (Resin, GlassFish, TomEE, JBoss AS). You can also add EJB to servers that don't yet support it via OpenEJB, but it's far easier to start with a server that already supports it.
Upvotes: 1
Reputation: 12538
Since Java EE 6 you must distinguish between EJB and JPA
Current Hibernate releases do implement the JPA API. So if you use Hibernate you can use its proprietary API or the JPA API. The latter provides the advantage that you can switch between different JPA implementations like OpenJPA, EclipseLink, Hibernate. The proprietary Hibernate API is more powerful in that it provides more features.
You are not required to use EJB, you can build your own business logic layer based on standard Java features or use another container like Spring.
Upvotes: 3
Reputation: 691933
The main advantage of EJBs is that it allows demarcating the transactions declaratively. Instead of writing the same boilerplate code again and again to start a transaction, commit it at the end, rollback if an exception occurs, etc., this is done for you by the EJB stack. Spring offers the same possibility, BTW.
But what's most important, reading your question and previous comments, is to architect your application correctly. Don't put any Java code in your JSPs. Only use the JSP EL, the JSTL and other custom tags in your JSPs, which are meant to generate markup.
The business logic should be placed in Java classes (EJBs or not), and these Java classes should be called from a servlet, which would then dispatch to a JSP to generate the markup. There are plenty of web frameworks to help you architect the application correctly, but also to help you with validation, i18n, templating, etc. Look at Stripes, Spring MVC, Struts, etc.
Upvotes: 3
Reputation: 780
Hibernate's ORM is plenty enough for that purpose. My understanding is that EJBs are more useful in multi-tier applications (where you have a frontend talk to a middle layer containing EJBs, which in turn talks to a database), and as a result EJBs are horribly overengineered for simpler purposes (but EJB3 is not the abomination that the previous versions were, so take that with a grain of salt).
Upvotes: 0