Sample EJB Configs for Spring

For legacy reasons, mandated technology reasons, or the need to do clustered remote access, a number of people do need to use EJBs with Spring, whether they want to or not.

The existing ejbtest integration test app under the autobuilds directory in the Spring source tree was originally created to be used for integration level testing of Spring code which couldn’t be properly tested with only a unit test, since it needed an EJB (or alternately J2EE Application, i.e. what an EAR file represents) environment to run in.

Since Spring currently doesn’t ship with any EJB based sample apps, and a sample goes a long way towards making explanations in the manual clearer, I spent a few hours the other day making the ejbtest app also usable as an example of how you can configure and access EJBs in a Spring environment, with the following variations (all running with JTA coordinating transactions):

- EJB is set to use CMT (Container Managed Transactions). It delegates to a POJO service object which is itself wrapped with Spring transactions, and ultimately performs some Hibernate based operations.
- EJB is set to NOT use CMT (it is non-transactional). It delegates to a POJO service object which is itself wrapped with Spring transactions, and ultimately performs some Hibernate based operations.
- EJB is set to use CMT. It delegates to a POJO service object which is NOT wrapped by Spring transactions, and ultimately performs some Hibernate based operations. What is interesting about this case is that as long as Hibernate is configured (via the SessionFactory) to be able to do a lookup of the JTA TransactionManager in the appserver environment, Spring is also able to get at this TM, and create and bind Hibernate Sessions to the JTA/CMT transaction, even in the absence of a Spring transaction manager.

One caveat to anybody looking at the config: the bean names in there are choosen to make it clear what is being tested, and as such include things like the transaction strategy. Obviously in a real-life app, you would not put something like this in the name of a bean, since the whole point is that this sort of thing should be swappable without affecting other beans or clients.

Anybody wanting to look at the config should check out the Spring source, and look under ‘/autobuilds/apps/ejbtest’. Look for the SimpleEJB variations. Alternately, you can browse the CVS tree here.

I’ve been thinking we should just ship an EJB-based sample app with Spring, but I don’t think there’s enough time for this to happen in the 1.1 timeframe.

 

8 responses

  1. Atoy says:


    I know you will have saved me hours of time, so I’m thanking you in advance.


  2. Nice job Colin, but I’d like to note that there are some problems with Spring’s TX wrappers and CMT (at least in WLS 7) as outlined in SPR-251 JIRA. Essentialy if transaction wrapper used with RequiresNew and CMT transaction started from session bean but marked for rollback before invocation of POJO you’ll get nasty ‘Cannot resume transaction’ exception after call returns from POJO to session bean. This essentialy prevented us from using Spring’s transactional proxies in our current project unfortunately. :-(


  3. Yes, it’s a bummer that it doesn’t work in WLS 7, but at least it seems to work fine in 8. You can of course go to just using CMT alone, or just Spring Tx alone, which I think should both be ok…


  4. I’m not sure that using ‘Spring Tx alone’ would work say for MDB if you need message to be taken from queue and update in database to happen in one XA transaction. I think that there is no other choice but to use CMT in this case (it’s too late to start tx whem message already taken out of the queue), which kind of pushes Spring tx out of the picture for such scenarious.
    You see, the point is that it doesn’t work in WLS 7 makes me wonder that it won’t break say in WLS 9 or WebSphere X or whatever else. I would like tx framework be more reliable that that. Just my 2c. ;-)


  5. I would worry about WebLogic 9 when WebLogic 9 comes out, personally. The fact that there are issues with WLS 7 sems to be because they are not spec compliant, whereas they are more spec compliant with WLS 8; so I am not going to start worrying that an unreleased version may become less spec compliant again. Ultimately, there are any number of issues people can and do run into just switching a straight EJB-based (for tx) app from container to container. From experience, any guarantee of compatibility you have when you get problems with those kinds of moves is typically the possibility that it will be fixed in a future service pack. In the past, when I was working heavily with WebLogic (4.5, 5, 6), I had any number of critital issues/breakage, that took many months to be fixed, if they were fixed at all. In that respect even something like this issue which remains with CMT+Spring Tx in WLS 7 is almost certainly fixable, if somebody wants to spend a bit of time on it, and it’s something that’s probably in anybody’s (not just the vendor’s) power to fix.


  6. Since you have some error in TrackBack request handler here is an url to my blog entry related to this http://jroller.com/page/eu/20040826#using_spring_jta_interfaces_from


  7. Here is error I’ve got:

    Database error: [You have an error in your SQL syntax. Check the manual that corresponds to your MySQL server version for the right syntax to use near ’s blog . That is basically a fundamental question for J2EE appl]
    INSERT INTO colins_comments
    (comment_post_ID, comment_author, comment_author_email, comment_author_url, comment_author_IP, comment_date, comment_date_gmt, comment_content, comment_approved)
    VALUES
    ('9', 'Euxx', '', 'http://jroller.com/page/eu/20040826#using_spring_jta_interfaces_from', '64.246.48.53', '2004-08-26 12:23:59', '2004-08-26 17:23:59', '<trackback /><strong>Using Spring JTA interfaces from EJB.</strong>
    We had a long discussion in Spring list, in Spring JIRA and it is now continues in Colin's blog . That is basically a fundamental question for J2EE application design. It is clearly stated in J2EE specification that Web Components can use JTA interfa', '1')
    </trackback>

    error with the database, it can’t store your comment…
    Please contact the webmaster.

  8. sheik says:


    The repository location has been changed in CVS. The new link is

    http://springframework.cvs.sourceforge.net/springframework/spring/autobuilds/apps/ejbtest/

Leave a Reply