Java EE EJB 3.0 Glassfish
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
EJB 3.0 was the release that made Enterprise JavaBeans practical for many teams again by replacing large XML-heavy patterns with annotations and plain Java classes. GlassFish matters here because it was the Java EE reference server, so it remains a useful environment for understanding how EJB 3.0 components are packaged, deployed, and injected.
What EJB 3.0 Simplified
Older EJB versions required home interfaces, remote interfaces, deployment descriptors, and a lot of container-specific ceremony. EJB 3.0 reduced that friction with:
- annotation-based bean definitions
- dependency injection
- POJO-style programming
- container-managed transactions
- simpler local and remote access
That is why many examples from the GlassFish era look much closer to modern Java than EJB 2.x code.
A Basic Stateless Session Bean
The most common EJB 3.0 example is a stateless session bean. It represents business logic with no conversational state tied to a specific client.
GlassFish detects the annotations during deployment and registers the bean automatically.
Injecting the Bean Into Another Component
One of the main benefits of EJB 3.0 is that you usually consume beans through injection rather than manual lookups.
That injection point is resolved by the container when the application is deployed.
Packaging and Deploying on GlassFish
For a simple application, you typically package the EJB and web layer in a WAR or EAR and deploy it to GlassFish. If you are using Maven, the build often looks familiar:
The provided scope matters because GlassFish already supplies the Java EE APIs at runtime.
Once packaged, deployment can be done from the admin console or command line:
After deployment, GlassFish manages lifecycle, pooling, transactions, security integration, and injection.
Transactions and Container Services
A major reason to use EJB was never just object lookup. It was access to container services with minimal code.
For example, a method on a stateless bean can run inside a transaction automatically:
In many cases, the container default is already enough, but explicit annotations make the intent clear.
Common Pitfalls
The most common mistake is packaging the Java EE API into the application instead of marking it as provided. Doing so can create classloading conflicts on GlassFish.
Another issue is confusing local and remote interfaces. If your client is inside the same application, a local interface is usually the right choice and simpler to configure.
A third pitfall is treating EJB as just another object with new. Container-managed features such as transactions and injection only work when the bean is created and invoked through the container.
Finally, many old tutorials mix versions of Java EE, GlassFish, and EJB syntax. Keep the example internally consistent so the deployment model matches the server you are actually using.
Summary
- EJB 3.0 made enterprise components much simpler through annotations and injection.
- GlassFish is a natural server for learning EJB because it was the Java EE reference implementation.
- Stateless session beans are the standard entry point for business logic.
- Use container injection and container lifecycle instead of constructing beans manually.
- Keep dependencies and packaging aligned with the GlassFish version you deploy to.
Related reading

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.