EJB3 with Spring
Asked Answered
S

4

2

I have understood that if I use EJB in Spring context, I get all the same benefits as if I was using it in "pure" EJB3 environment, is this true? I have googled but can't find a definitive, clear answer.

For example, let's say I have a session bean that updates some tables in the database and it throws a System Exception. In "pure" EJB3 environment the transaction is rolled back. What if I for example @Autowire this bean using Spring, does Spring take care of the transaction handling same way as does the EJB3 container? Or what? Does it maybe require some specific configuration or is it fully "automatic"?

Schumann answered 1/6, 2010 at 13:21 Comment(2)
It seems like you've answered your own question. I suggest you post your "update" as an answer. This way you can accept it a few days later.Basis
@ewernli: Ok, did as you suggestedSchumann
S
2

I got very good answers with links to articles, and from those I compiled my understanding that yes, the session beans work the same way regardless of if they are used with Spring or without, as long as the beans are defined in the Spring context with <jee:jndi-lookup>. Also found a good, simple article about this: http://java.dzone.com/articles/ejb-30-and-spring-25

However I can't accept just one answer because to me they are all equally good, but none of them exactly in point :) Could be that my question was not clear enough to start with...

(It was suggested that I post this as an answer to my own question)

Schumann answered 8/6, 2010 at 8:42 Comment(0)
B
6

I have understood that if I use EJB in Spring context, I get all the same benefits as if I was using it in "pure" EJB3 environment, is this true?

You usually use either POJO + Spring or EJB3. I'm a bit confused by what you mean by "EJB in Spring"...

POJO + Spring and EJB3 are quite close now, and have the same facilities when it comes to declarative transaction management.

I don't know all the details about security, but I would say that both technologies are also very similar.

Actually both Spring and EJB3 rely on other specifications. The important ones are: JPA (persistence), JTA (distributed transaction), JMS (messaging), JDBC (data sources). Good support for that exist in the two technology stacks.

Both technologies have become very flexible and you can choose what to use or not. So you can have EJB3 in an app. server and be very light. Or you can use Spring with all modules which is almost as heavy as a full-fledged app. server.

I think the EJB3 model is still a bit richer, with things like remoting, stateful session beans (SFSB), container-managed transactions, and extended persistence context. Plus the possible support of clustering depending on the app. server. But these are advanced features which are use seldom (and IMO require expertise).

See EJB3 vs Spring

Basis answered 1/6, 2010 at 18:17 Comment(2)
Well it is possible to wire session beans using Spring. It's a good question if there's any point in doing it like this, but so far I have two reasons: 1) the "rules" that we have require that backend services are EJB. 2) With Spring context files it is easy to mock certain services that are used by the EJB. What I don't understand is how does the Spring make use of the EJB and do I lose some of the "automatic" features of the container.Schumann
@Schumann Ah! So your question was really about how to integrate EJB with Spring. I got it wrong, my bad. Looking at static.springsource.org/spring/docs/3.0.x/…, my take is that Spring starts the JTA transaction, and the EJB just enrolls into it as long as they are marked with REQUIRED, which is the default. So that should work transparently. But if I were you I would make a few test to be sure anyway.Basis
C
3

spring has many features, one of which is transaction management, which uses a common abstraction accross all different orm implementations (jpa, raw hibernate, jdbc, jdo etc). The default behavior is that in a transactional method, a runtime exception causes a rollback (which is probably what you want), but you can also fine-tune the rollback rules.

However, none of this requires EJB. If you don't use EJBs (stateless, stateful, mdbs), JPA will be enough for that, and the spring jpa support is excellent. In 90% of the cases spring will provide everything you need without EJBs.


EDIT:

read this about Spring EJB integration

Citrin answered 1/6, 2010 at 13:35 Comment(0)
H
2

You can have Spring handle transactions and roll back accordingly. You have to configure it to do so, but that's also true of EJBs.

Nothing in life is truly "automatic". You have to tell the code what you want sometime.

The real question is: Why do you think you need both EJBs and Spring? Anything you can do with EJBs can be done using POJOs with Spring. What are EJBs buying you here?

Haldi answered 1/6, 2010 at 13:33 Comment(5)
'You have to configure it to do so': to be fair: there are sensible defaults in both. in spring: define a platformtransactionmanager, use @transactional annotations and you're up and running, that's not a lot of configurationCitrin
Can you use remoting with POJOs in Spring?Schumann
yes: static.springsource.org/spring/docs/3.0.x/…Citrin
@seanizer - "not a lot of configuration" is still not "zero configuration." My statement needs no correction.Haldi
not correction, but clarification. nothing can be done in spring without configuration, but in spring 3, configuration is minimal for standard cases (as in EJB 3).Citrin
S
2

I got very good answers with links to articles, and from those I compiled my understanding that yes, the session beans work the same way regardless of if they are used with Spring or without, as long as the beans are defined in the Spring context with <jee:jndi-lookup>. Also found a good, simple article about this: http://java.dzone.com/articles/ejb-30-and-spring-25

However I can't accept just one answer because to me they are all equally good, but none of them exactly in point :) Could be that my question was not clear enough to start with...

(It was suggested that I post this as an answer to my own question)

Schumann answered 8/6, 2010 at 8:42 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.