EJB3 Business Logic Patterns & Practices

I'm in the process of developing a multi-tiered financial processing application in Java using EJB3 (Hibernate + Glassfish for the app and web services layer, Lift on Glassfish for the web UI) and I'm struggling with the question of where to put my business logic.

When this project started, our first notion was to put the bulk of our business logic into the stateless session beans. As time has gone on, though, we've found the dependency injection provided by the EJB framework too limiting, so a lot of our business logic has ended up in POJOs that are assembled by Guice in the @PostConstruct method of the stateless session beans. This progress has led to fragmentation of our business logic between the session beans and the POJOs, and I'm trying to figure out an approach for correcting this.

Initially, we tried to have our web tier use the remote interfaces of the session beans to perform some functions that are accessible both from the UI and from the web service layer, which is provided by @WebService-annotated stateless session beans. This turned out to be a nightmare from a persistence and performance perspective, because our entity graph can grow quite large and reattaching the detached entity graph to the persistence context turned out to be highly error-prone, so our solution was to start just passing object identifiers around and looking up the entities from the database wherever they were needed.

My basic question is this: what principles and guidelines can you suggest for deciding whether business logic should go in a session bean or a POJO? When does it make sense to pass entity beans around, given a complex object graph?


Asked by: Lyndon318 | Posted: 23-01-2022






Answer 1

I struggled with this while building a webapp using JPA, EJB3, and Wicket. Since hitting my database hard with repeated queries was more scalable than holding lots of large entities in memory, I decided to only pass around their ids and never the entity itself.

Wicket and its concept of models had a lot to do with this decision. Their loadableDetachableModel cleans up entities when they are not in use, while still holding on to the id. A load() method is implemented which knows how to get the entity when it is needed again, in my case by calling a stateless session bean; and a persist() method calls the stateless bean to persist changes. This model class is what I actually pass around. Wicket only handles logic pertaining to display and input validation, and I only need to inject the ejb into the model classes. Perhaps you could create something similar in your app (I've got no idea what lift has to offer...).

This has worked out nicely for me, but I don't have particularly complex business logic and can isolate any changes which need to be persisted into small units of logic and single pages.

Answered by: Sienna124 | Posted: 24-02-2022



Answer 2

Whenever you need many "system" services (injections, etc) use stateless bean. Otherwise - POJOs. POJOs are much more flexible.

However simple (accessor?) methods (e.g. in webservices and beans) can just do some simple work and return the results.

Answered by: Arnold783 | Posted: 24-02-2022



Similar questions

java - What are the best practices for naming ant targets?

What are the best practices for naming ant targets? For example, what would you expect the target "test" to run? All unit tests? All functional tests? Both? What are the standard names used for running different types of tests (unit/functional/all)? Are there standards for target names to deploy software in J2SE? in J2EE? My project uses ant for a java project with junit, Swing applications, and...


java - LDAP Best Practices

I'm interested in the best practices of using LDAP authentication in a Java-based web application. In my app I don't want to store username\password, only some ids. But I want to retrieve addition information (Name, Last name) if any exists in an LDAP catalog.


Logging in Java and in general: Best Practices?

Sometimes when I see my logging code I wonder if I am doing it right. There might be no definitive answer to that, but I have the following concerns: Library Classes I have several library classes which might log some INFO messages. Fatal Errors are reported as exceptions. Currently I have a static logger instance in my classes with the class name as the logging name. (Log4j's:...


java - a Good Example of ant best practices

Closed. This question does not meet Stack Overflow guid...


Best practices for writing open source Java

Closed. This question needs to be more focused. It ...


security - Java Best Practices to Prevent Cross Site Scripting

Closed. This question is opinion-based. It is not c...


java - GWT panel design best practices

I am a newbie in GWT, and also to Swing/SWT style interface building. I have worked with HTML for a long time, and I know how to build my UIs in that. However, I am having trouble translating this to GWT. What I am trying to make currently, is a simple UI with a table on the left with 4 columns, heading and a caption, and a panel on the right with an input and some dropdowns (filters for the list on the left). I have made ...


maven 2 - Best practices creating Java service or daemon scripts

I'm looking for a tool to run java server processes as daemon services in Linux (and potentially on Windows and other OS's). I'm looking for the best practices when it comes to how to build production capable scripts and launch configuration. I'm familiar with best practices when it comes to a project's build, using Apache Maven, or something like


java - Best Practices ElseIf against Throw Exception


java - What are tools and best practices for testing web services?

I'm about to start work on a large project that will involve providing a significant number of web services. We'll be using the Java platform, so, of course, we'll be making heavy use of JUnit, Hudson, etc. (although I'm not sure that matters at all.) We're looking for a set of best practices and/or tools for testing the web services. We'll have several goals in mind: Obviously, we need to...






Still can't find your answer? Check out these amazing Java communities for help...



Java Reddit Community | Java Help Reddit Community | Dev.to Java Community | Java Discord | Java Programmers (Facebook) | Java developers (Facebook)



top