Life Codecs @

Ruminations. Reflections. Refractions. Code.

Apr 25, 2009 - software dev

Spring 2.5 + EJB 3 Integration

Yes, yet another post on the topic so talked about on the Web (like a year ago or so anyway!). This is mostly for my benefit and summary though – haven’t even written code to test it myself, but here goes. Spent a few hours exploring integration alternatives just now, basically EJB 3 DI is great, but a glaring thing that’s missing is injecting POJOs into it, which Spring does quite well – sure in both cases the “POJOs” are managed by containers, but hey, nothing’s perfect, yet. To summarise, here are the 2 major integration scenarios and solutions:

  1. To inject Spring Beans into EJBs (and bootstrap Spring app context(s) from within EJBs!) is simple enough, we use:

    Spring Docs, Chapter 18, EJB 3 Integration

    This actually handles bootstrapping (read up the doc on how it does it), which is tricky in EJB modules, since there’s no standard EJB context listener mechanism as there is for web containers. Many app server vendors do have EJB module lifecycle hooks/interfaces you can implement – but obviously they’re proprietary and make your app less portable, so I’d rather avoid them if I can, I still don’t see why this feature isn’t part of the spec… clearly there are use cases, and clearly since there are non-standard implementations, it’s feasible to do and define behavior for.

    • The other way around is a bit tricky, i.e. we now need EJB container-managed objects (e.g. entity managers, EBJs, resources, etc.) injected into Spring Beans in the XML file. In this case those resources need to be registered in the JNDI ENC (still figuring out EJB context vs. global) and in the Spring XML a JNDI lookup is done for the name it was registered with, and then injected to the relevant bean via a bean-ref. A ‘lookup-then-inject’ Spring XML config example can be seen here (note dataSource JNDI look-up and inject):

      Spring and JNDI (Tomcat or Jetty)

      Registering the container-managed objects in the JNDI can be done via EJB 3 interceptors, for example, a use case is having Spring-managed DAO beans, but EJB-managed Entities/Persistence Units: let the interceptor have an annotation

      @PersistenceUnit(unitName="bar", name=”barEntityManagerFactory”)

      – which receives the container-injected persistence unit and then registers it into the JNDI registry keyed by the name “barEntityManagerFactory”. The Spring XML config will need a bean created through a JNDI look-up of “barEntityManager”, and inject that in to the DAO Spring-managed bean.

      Lots of repetition up there! (Note I said it was for my benefit :P).

Now I can finally watch some Naruto!

Apr 24, 2009 - software dev

JBoss 5.0.x (and 5.1 Beta?) and JPA Persistence Units

After much messing around, I finally have my sample EJB 3 app working, but on Glassfish!! I started off with JBoss, and may still go back to it once they decide on better handling of persistence units, and not reload/load it regardless! Too complicated for me to explain, check out these links which summarise it in depth (they speak about Seam, but refer to the same issue I’m babbling about):

Basically I have a common domain-model jar which I wish to treat as a persistence unit used by both my EJB and WAR in the same EAR.

And after hours of tweaking and messing, this is my successful test output:

Data Value=JustCreated!!

Data Value=JustCreated!!

Updated Data Value=JustUpdated!!


Ah, the joys of development. Actually that’s a pretty decent test case, it’s a servlet that’s got an EJB3 instance injected, which in turn has an EntityManager instance obtained via the JNDI name of a configured JTA XA MySQL datasource injected. What a mouthful!

Apr 23, 2009 - general software dev

A Failed Interview!

I had an interview yesterday – which I thought went quite well, except for 2 questions below (interview was around 30 minutes I think), which it seems were significant to the interviewer.

Disclaimer: I didn’t get the job – so there’s obviously some bias in what I have to say. If anything, just add these questions to your list of things to look out for when looking for a Java WebApp job. I did not have a full job description when I went in (it was not provided), all I knew was it involved Hibernate, and that they were a team following an Agile methodology.

The Questions of Doom were:

  1. What’s a Hibernate Session?
  2. In a WSDL, what’s the difference between port and binding?

Question 1, something I probably should’ve been able to give a quick answer for. I started with the Session being a non-threadsafe object, which allows you to do CRUD operations with entities. The interviewer brought up the relationship between a Session and a database connection, and this is where I fumbled – eventually saying that a session uses one connection. It’s tricky because a Hibernate Session performs many functions (the docs say so too – “The Session is also many other things”!! I wonder if that violates the SRP 😛 – though it’s probably more a logical Facade). When you learn it mostly on project where most things are set up when you get there, you tend to skip exact details, so reading about it formally: it’s a unit of work, it also maintains the persistence cache – a map of objects in the persistence context to ensure identity, it is a handle to load and store objects, it’s a list of SQL statements to be executed, and finally it obtains a connection from a connection pool. Many things eh. I knew all of this from experience, I just could not put it elegantly into words. I also obviously knew having too many sessions open at the same time could exhaust your connections!

I also now know that you can reconnect a session – but the API deprecates the no-param reconnect() method, and you have to supply a connection reconnect(Connection)… any ideas on what the use case for this generally is? Extended sessions? Hmm…

Question 2, now I have done a fair bit of Web Services work, debugged quite a few issues too – but I’ve generally been a downstream client, so we get WSDLs from the upstream services, run a tool like Axis over it, voila a nicer Java view of the whole thing, so I didn’t have to tweak or look at the WSDL much – hence my knowledge of WSDLs themselves is fairly informal – I do know it’s basically a contract/webservice interface document and defines types, messages, and operations. When I did have to mess with WSDLs – it had to do with the operations and I/O parameters – the actual messages being passed around. So this question took me under 10 minutes to find resources for and get a fairly clear idea about the difference – a portType is like a module of available functions, and their parameters. A binding links these operations (and defines message formats) to a transport (which is usually just SOAP over HTTP anyway!). While I appreciate that it shows a crucial concept that WSDL separates messages from the transport – it’s hardly something that takes days to understand, certainly not rocket science (actually talking about SOAP itself, it very well could be).

In this interview, if I were to apply meaning to the whole incident, I would be left feeling a bit discontented – but I won’t do that because I know my abilities better than that – I so uberly-rock *cough* :P. In the end I just did not meet their expectations, but it is their expectations, and that is all. As for me, I’ll prepare better for notions or concepts that I take for granted because I use them everyday – must be like a fish-in-the-water-not-knowing-about-the-water-syndrome (that’s a long syndrome name…).

Apr 5, 2009 - martial arts

BKC Demonstration

I initially posted this video on Facebook (doesn’t look like I can point to my status and it’s comments from here?), and received some comments to which I needed to reply in some detail. However, Facebook restricts the amount I can type in a comment box, so reproducing it here.

See post comments. FYI it’s a demonstration by my grandmaster, founder of BKC.